diff --git a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs index 47a0dcad269817..951c557df71c51 100644 --- a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs +++ b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs @@ -681,15 +681,25 @@ public static bool TryPrintStackTraceFromDmp(string dmpFile, TextWriter outputWr // The children are sorted in the order they should be dumped static unsafe IEnumerable FindChildProcessesByName(Process process, string childName) { - Console.WriteLine($"Finding all child processes of '{process.ProcessName}' (ID: {process.Id}) with name '{childName}'"); + process.TryGetProcessName(out string parentProcessName); + process.TryGetProcessId(out int parentProcessId); + Console.WriteLine($"Finding all child processes of '{parentProcessName}' (ID: {parentProcessId}) with name '{childName}'"); var children = new Stack(); Queue childrenToCheck = new Queue(); HashSet seen = new HashSet(); - seen.Add(process.Id); - foreach (var child in process.GetChildren()) - childrenToCheck.Enqueue(child); + seen.Add(parentProcessId); + + try + { + foreach (var child in process.GetChildren()) + childrenToCheck.Enqueue(child); + } + catch + { + // Process exited + } while (childrenToCheck.Count != 0) { @@ -707,8 +717,15 @@ static unsafe IEnumerable FindChildProcessesByName(Process process, str Console.WriteLine($"Checking child process: '{processName}' (ID: {processId})"); seen.Add(processId); - foreach (var grandchild in child.GetChildren()) - childrenToCheck.Enqueue(grandchild); + try + { + foreach (var grandchild in child.GetChildren()) + childrenToCheck.Enqueue(grandchild); + } + catch + { + // Process exited + } if (processName.Equals(childName, StringComparison.OrdinalIgnoreCase)) { @@ -844,7 +861,9 @@ public int RunTest(string executable, string outputFile, string errorFile, strin Console.WriteLine($"\t{"ID",-6} ProcessName"); foreach (var activeProcess in Process.GetProcesses()) { - Console.WriteLine($"\t{activeProcess.Id,-6} {activeProcess.ProcessName}"); + activeProcess.TryGetProcessName(out string activeProcessName); + activeProcess.TryGetProcessId(out int activeProcessId); + Console.WriteLine($"\t{activeProcessId,-6} {activeProcessName}"); } if (OperatingSystem.IsWindows())