From e59f9133e3ed1a2024e5ea5972603150ac2306f1 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Thu, 15 May 2025 23:47:51 -0700 Subject: [PATCH 1/2] Add more protection against exited processed in CoreclrTestLib --- .../CoreclrTestWrapperLib.cs | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs index 47a0dcad269817..673b53884c43e9 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 int 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()) From 736cc8d288ff06561ca783f03a4925cfc6287e3b Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 16 May 2025 09:27:51 -0700 Subject: [PATCH 2/2] Update src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs --- src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs index 673b53884c43e9..951c557df71c51 100644 --- a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs +++ b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs @@ -681,7 +681,7 @@ 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) { - process.TryGetProcessName(out int parentProcessName); + process.TryGetProcessName(out string parentProcessName); process.TryGetProcessId(out int parentProcessId); Console.WriteLine($"Finding all child processes of '{parentProcessName}' (ID: {parentProcessId}) with name '{childName}'");