From 7c1e5a3cba90083467e6a5b4df724a29541cab3b Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 5 Jan 2025 17:12:51 +0800 Subject: [PATCH] Fixed possible crash issues --- v2rayN/ServiceLib/Common/ProcUtils.cs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/v2rayN/ServiceLib/Common/ProcUtils.cs b/v2rayN/ServiceLib/Common/ProcUtils.cs index 495167cb4b0..653d94af273 100644 --- a/v2rayN/ServiceLib/Common/ProcUtils.cs +++ b/v2rayN/ServiceLib/Common/ProcUtils.cs @@ -81,6 +81,7 @@ public static async Task ProcessKill(Process? proc, bool review) return; } + var procId = review ? proc?.Id : null; var fileName = review ? proc?.MainModule?.FileName : null; var processName = review ? proc?.ProcessName : null; @@ -90,15 +91,27 @@ public static async Task ProcessKill(Process? proc, bool review) try { proc?.Dispose(); } catch (Exception ex) { Logging.SaveLog(_tag, ex); } await Task.Delay(300); - if (review && fileName != null) + if (review && procId != null && fileName != null) { - var proc2 = Process.GetProcessesByName(processName) - .FirstOrDefault(t => t.MainModule?.FileName == fileName); - if (proc2 != null) + try { - Logging.SaveLog($"{_tag}, KillProcess not completing the job"); - await ProcessKill(proc2, false); - proc2 = null; + var lstProc = Process.GetProcessesByName(processName); + foreach (var proc2 in lstProc) + { + if (proc2.Id == procId) + { + Logging.SaveLog($"{_tag}, KillProcess not completing the job, procId"); + await ProcessKill(proc2, false); + } + if (proc2.MainModule != null && proc2.MainModule?.FileName == fileName) + { + Logging.SaveLog($"{_tag}, KillProcess not completing the job, fileName"); + } + } + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); } } }