Skip to content

Commit bc7cf1c

Browse files
authored
Avoid unnecessary closures/delegates in Process (#50496)
1 parent a7ed1fd commit bc7cf1c

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.NonUap.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Kill(bool entireProcessTree)
3333

3434
private bool IsSelfOrDescendantOf(Process processOfInterest)
3535
{
36-
if (SafePredicateTest(() => Equals(processOfInterest)))
36+
if (Equals(processOfInterest))
3737
return true;
3838

3939
Process[] allProcesses = GetProcesses();
@@ -47,7 +47,7 @@ private bool IsSelfOrDescendantOf(Process processOfInterest)
4747
{
4848
foreach (Process candidate in current.GetChildProcesses(allProcesses))
4949
{
50-
if (SafePredicateTest(() => processOfInterest.Equals(candidate)))
50+
if (processOfInterest.Equals(candidate))
5151
return true;
5252

5353
descendantProcesses.Enqueue(candidate);
@@ -82,7 +82,7 @@ private IReadOnlyList<Process> GetChildProcesses(Process[]? processes = null)
8282

8383
try
8484
{
85-
if (SafePredicateTest(() => IsParentOf(possibleChildProcess)))
85+
if (IsParentOf(possibleChildProcess))
8686
{
8787
childProcesses.Add(possibleChildProcess);
8888
dispose = false;
@@ -98,19 +98,10 @@ private IReadOnlyList<Process> GetChildProcesses(Process[]? processes = null)
9898
return childProcesses;
9999
}
100100

101-
private bool SafePredicateTest(Func<bool> predicate)
102-
{
103-
try
104-
{
105-
return predicate();
106-
}
107-
catch (Exception e) when (e is InvalidOperationException || e is Win32Exception)
108-
{
109-
// InvalidOperationException signifies conditions such as the process already being dead.
110-
// Win32Exception signifies issues such as insufficient permissions to get details on the process.
111-
// In either case, the predicate couldn't be applied so return the fallback result.
112-
return false;
113-
}
114-
}
101+
private static bool IsProcessInvalidException(Exception e) =>
102+
// InvalidOperationException signifies conditions such as the process already being dead.
103+
// Win32Exception signifies issues such as insufficient permissions to get details on the process.
104+
// In either case, the predicate couldn't be applied so return the fallback result.
105+
e is InvalidOperationException || e is Win32Exception;
115106
}
116107
}

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Unix.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,29 @@ private ProcessPriorityClass PriorityClassCore
308308
}
309309

310310
/// <summary>Checks whether the argument is a direct child of this process.</summary>
311-
private bool IsParentOf(Process possibleChildProcess) =>
312-
Id == possibleChildProcess.ParentProcessId;
311+
private bool IsParentOf(Process possibleChildProcess)
312+
{
313+
try
314+
{
315+
return Id == possibleChildProcess.ParentProcessId;
316+
}
317+
catch (Exception e) when (IsProcessInvalidException(e))
318+
{
319+
return false;
320+
}
321+
}
313322

314-
private bool Equals(Process process) =>
315-
Id == process.Id;
323+
private bool Equals(Process process)
324+
{
325+
try
326+
{
327+
return Id == process.Id;
328+
}
329+
catch (Exception e) when (IsProcessInvalidException(e))
330+
{
331+
return false;
332+
}
333+
}
316334

317335
partial void ThrowIfExited(bool refresh)
318336
{

src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.Win32.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,17 @@ private bool WaitForInputIdleCore(int milliseconds)
330330
/// <remarks>
331331
/// A child process is a process which has this process's id as its parent process id and which started after this process did.
332332
/// </remarks>
333-
private bool IsParentOf(Process possibleChild) =>
334-
StartTime < possibleChild.StartTime
335-
&& Id == possibleChild.ParentProcessId;
333+
private bool IsParentOf(Process possibleChild)
334+
{
335+
try
336+
{
337+
return StartTime < possibleChild.StartTime && Id == possibleChild.ParentProcessId;
338+
}
339+
catch (Exception e) when (IsProcessInvalidException(e))
340+
{
341+
return false;
342+
}
343+
}
336344

337345
/// <summary>
338346
/// Get the process's parent process id.
@@ -353,9 +361,17 @@ private unsafe int ParentProcessId
353361
}
354362
}
355363

356-
private bool Equals(Process process) =>
357-
Id == process.Id
358-
&& StartTime == process.StartTime;
364+
private bool Equals(Process process)
365+
{
366+
try
367+
{
368+
return Id == process.Id && StartTime == process.StartTime;
369+
}
370+
catch (Exception e) when (IsProcessInvalidException(e))
371+
{
372+
return false;
373+
}
374+
}
359375

360376
private List<Exception>? KillTree()
361377
{
@@ -389,7 +405,7 @@ private bool Equals(Process process) =>
389405
(exceptions ??= new List<Exception>()).Add(e);
390406
}
391407

392-
List<(Process Process, SafeProcessHandle Handle)> children = GetProcessHandlePairs(p => SafePredicateTest(() => IsParentOf(p)));
408+
List<(Process Process, SafeProcessHandle Handle)> children = GetProcessHandlePairs((thisProcess, otherProcess) => thisProcess.IsParentOf(otherProcess));
393409
try
394410
{
395411
foreach ((Process Process, SafeProcessHandle Handle) child in children)
@@ -413,7 +429,7 @@ private bool Equals(Process process) =>
413429
return exceptions;
414430
}
415431

416-
private List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func<Process, bool> predicate)
432+
private List<(Process Process, SafeProcessHandle Handle)> GetProcessHandlePairs(Func<Process, Process, bool> predicate)
417433
{
418434
var results = new List<(Process Process, SafeProcessHandle Handle)>();
419435

@@ -422,7 +438,7 @@ private bool Equals(Process process) =>
422438
SafeProcessHandle h = SafeGetHandle(p);
423439
if (!h.IsInvalid)
424440
{
425-
if (predicate(p))
441+
if (predicate(this, p))
426442
{
427443
results.Add((p, h));
428444
}

0 commit comments

Comments
 (0)