diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.NonUap.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.NonUap.cs index d2c05302f719f5..0dff87802214e9 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.NonUap.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.NonUap.cs @@ -33,7 +33,7 @@ public void Kill(bool entireProcessTree) private bool IsSelfOrDescendantOf(Process processOfInterest) { - if (SafePredicateTest(() => Equals(processOfInterest))) + if (Equals(processOfInterest)) return true; Process[] allProcesses = GetProcesses(); @@ -47,7 +47,7 @@ private bool IsSelfOrDescendantOf(Process processOfInterest) { foreach (Process candidate in current.GetChildProcesses(allProcesses)) { - if (SafePredicateTest(() => processOfInterest.Equals(candidate))) + if (processOfInterest.Equals(candidate)) return true; descendantProcesses.Enqueue(candidate); @@ -82,7 +82,7 @@ private IReadOnlyList GetChildProcesses(Process[]? processes = null) try { - if (SafePredicateTest(() => IsParentOf(possibleChildProcess))) + if (IsParentOf(possibleChildProcess)) { childProcesses.Add(possibleChildProcess); dispose = false; @@ -98,19 +98,10 @@ private IReadOnlyList GetChildProcesses(Process[]? processes = null) return childProcesses; } - private bool SafePredicateTest(Func predicate) - { - try - { - return predicate(); - } - catch (Exception e) when (e is InvalidOperationException || e is Win32Exception) - { - // InvalidOperationException signifies conditions such as the process already being dead. - // Win32Exception signifies issues such as insufficient permissions to get details on the process. - // In either case, the predicate couldn't be applied so return the fallback result. - return false; - } - } + private static bool IsProcessInvalidException(Exception e) => + // InvalidOperationException signifies conditions such as the process already being dead. + // Win32Exception signifies issues such as insufficient permissions to get details on the process. + // In either case, the predicate couldn't be applied so return the fallback result. + e is InvalidOperationException || e is Win32Exception; } } diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs index 05eee69f480a6d..7ae9eeec8d614f 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs @@ -308,11 +308,29 @@ private ProcessPriorityClass PriorityClassCore } /// Checks whether the argument is a direct child of this process. - private bool IsParentOf(Process possibleChildProcess) => - Id == possibleChildProcess.ParentProcessId; + private bool IsParentOf(Process possibleChildProcess) + { + try + { + return Id == possibleChildProcess.ParentProcessId; + } + catch (Exception e) when (IsProcessInvalidException(e)) + { + return false; + } + } - private bool Equals(Process process) => - Id == process.Id; + private bool Equals(Process process) + { + try + { + return Id == process.Id; + } + catch (Exception e) when (IsProcessInvalidException(e)) + { + return false; + } + } partial void ThrowIfExited(bool refresh) { diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs index f5e4aa38e51f1b..18aff1fb172384 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs @@ -330,9 +330,17 @@ private bool WaitForInputIdleCore(int milliseconds) /// /// A child process is a process which has this process's id as its parent process id and which started after this process did. /// - private bool IsParentOf(Process possibleChild) => - StartTime < possibleChild.StartTime - && Id == possibleChild.ParentProcessId; + private bool IsParentOf(Process possibleChild) + { + try + { + return StartTime < possibleChild.StartTime && Id == possibleChild.ParentProcessId; + } + catch (Exception e) when (IsProcessInvalidException(e)) + { + return false; + } + } /// /// Get the process's parent process id. @@ -353,9 +361,17 @@ private unsafe int ParentProcessId } } - private bool Equals(Process process) => - Id == process.Id - && StartTime == process.StartTime; + private bool Equals(Process process) + { + try + { + return Id == process.Id && StartTime == process.StartTime; + } + catch (Exception e) when (IsProcessInvalidException(e)) + { + return false; + } + } private List? KillTree() { @@ -389,7 +405,7 @@ private bool Equals(Process process) => (exceptions ??= new List()).Add(e); } - List<(Process Process, SafeProcessHandle Handle)> children = GetProcessHandlePairs(p => SafePredicateTest(() => IsParentOf(p))); + List<(Process Process, SafeProcessHandle Handle)> children = GetProcessHandlePairs((thisProcess, otherProcess) => thisProcess.IsParentOf(otherProcess)); try { foreach ((Process Process, SafeProcessHandle Handle) child in children) @@ -413,7 +429,7 @@ private bool Equals(Process process) => return exceptions; } - private List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func predicate) + private List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func predicate) { var results = new List<(Process Process, SafeProcessHandle Handle)>(); @@ -422,7 +438,7 @@ private bool Equals(Process process) => SafeProcessHandle h = SafeGetHandle(p); if (!h.IsInvalid) { - if (predicate(p)) + if (predicate(this, p)) { results.Add((p, h)); }