Skip to content

Commit 9579c58

Browse files
authored
Add more protection against exited processed in CoreclrTestLib (#115642)
* Add more protection against exited processed in CoreclrTestLib * Update src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs
1 parent b8a8930 commit 9579c58

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,25 @@ public static bool TryPrintStackTraceFromDmp(string dmpFile, TextWriter outputWr
681681
// The children are sorted in the order they should be dumped
682682
static unsafe IEnumerable<Process> FindChildProcessesByName(Process process, string childName)
683683
{
684-
Console.WriteLine($"Finding all child processes of '{process.ProcessName}' (ID: {process.Id}) with name '{childName}'");
684+
process.TryGetProcessName(out string parentProcessName);
685+
process.TryGetProcessId(out int parentProcessId);
686+
Console.WriteLine($"Finding all child processes of '{parentProcessName}' (ID: {parentProcessId}) with name '{childName}'");
685687

686688
var children = new Stack<Process>();
687689
Queue<Process> childrenToCheck = new Queue<Process>();
688690
HashSet<int> seen = new HashSet<int>();
689691

690-
seen.Add(process.Id);
691-
foreach (var child in process.GetChildren())
692-
childrenToCheck.Enqueue(child);
692+
seen.Add(parentProcessId);
693+
694+
try
695+
{
696+
foreach (var child in process.GetChildren())
697+
childrenToCheck.Enqueue(child);
698+
}
699+
catch
700+
{
701+
// Process exited
702+
}
693703

694704
while (childrenToCheck.Count != 0)
695705
{
@@ -707,8 +717,15 @@ static unsafe IEnumerable<Process> FindChildProcessesByName(Process process, str
707717
Console.WriteLine($"Checking child process: '{processName}' (ID: {processId})");
708718
seen.Add(processId);
709719

710-
foreach (var grandchild in child.GetChildren())
711-
childrenToCheck.Enqueue(grandchild);
720+
try
721+
{
722+
foreach (var grandchild in child.GetChildren())
723+
childrenToCheck.Enqueue(grandchild);
724+
}
725+
catch
726+
{
727+
// Process exited
728+
}
712729

713730
if (processName.Equals(childName, StringComparison.OrdinalIgnoreCase))
714731
{
@@ -844,7 +861,9 @@ public int RunTest(string executable, string outputFile, string errorFile, strin
844861
Console.WriteLine($"\t{"ID",-6} ProcessName");
845862
foreach (var activeProcess in Process.GetProcesses())
846863
{
847-
Console.WriteLine($"\t{activeProcess.Id,-6} {activeProcess.ProcessName}");
864+
activeProcess.TryGetProcessName(out string activeProcessName);
865+
activeProcess.TryGetProcessId(out int activeProcessId);
866+
Console.WriteLine($"\t{activeProcessId,-6} {activeProcessName}");
848867
}
849868

850869
if (OperatingSystem.IsWindows())

0 commit comments

Comments
 (0)