From 17fd3c7c6a304dae65aab76594906adb10049690 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Wed, 26 Mar 2025 10:11:06 -0700 Subject: [PATCH] Fix CoreclrTestWrapperLib to be robust against existed processes Fixes #112846 --- .../CoreclrTestWrapperLib.cs | 57 ++++++++++++++++--- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs index 470d7574f9a3f3..47a0dcad269817 100644 --- a/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs +++ b/src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs @@ -111,6 +111,36 @@ public static unsafe bool ListChildPids(int ppid, out int[] buffer) internal static class ProcessExtensions { + public static bool TryGetProcessId(this Process process, out int processId) + { + try + { + processId = process.Id; + return true; + } + catch + { + // Process exited + processId = default; + return false; + } + } + + public static bool TryGetProcessName(this Process process, out string processName) + { + try + { + processName = process.ProcessName; + return true; + } + catch + { + // Process exited + processName = default; + return false; + } + } + public unsafe static IEnumerable GetChildren(this Process process) { var children = new List(); @@ -353,7 +383,7 @@ static bool RunProcess(string fileName, string arguments, TextWriter outputWrite Task stdOut = proc.StandardOutput.ReadToEndAsync(); Task stdErr = proc.StandardError.ReadToEndAsync(); - if(!proc.WaitForExit(DEFAULT_TIMEOUT_MS)) + if (!proc.WaitForExit(DEFAULT_TIMEOUT_MS)) { proc.Kill(true); outputWriter.WriteLine($"Timedout: '{fileName} {arguments}"); @@ -394,7 +424,7 @@ public static bool TryPrintStackTraceFromCrashReport(string crashReportJsonFile, string? userName = Environment.GetEnvironmentVariable("USER"); if (string.IsNullOrEmpty(userName)) { - userName="helixbot"; + userName = "helixbot"; } if (!RunProcess("sudo", $"chmod a+rw {crashReportJsonFile}", Console.Out)) @@ -569,7 +599,9 @@ public static bool TryPrintStackTraceFromCrashReport(string crashReportJsonFile, symbolizerOutput = stdout.Result; - } catch (Exception e) { + } + catch (Exception e) + { outputWriter.WriteLine("Errors while running llvm-symbolizer --pretty-print"); outputWriter.WriteLine(e.ToString()); return false; @@ -662,16 +694,23 @@ static unsafe IEnumerable FindChildProcessesByName(Process process, str while (childrenToCheck.Count != 0) { Process child = childrenToCheck.Dequeue(); - if (seen.Contains(child.Id)) + + if (!child.TryGetProcessId(out int processId)) + continue; + + if (seen.Contains(processId)) continue; - Console.WriteLine($"Checking child process: '{child.ProcessName}' (ID: {child.Id})"); - seen.Add(child.Id); + if (!child.TryGetProcessName(out string processName)) + continue; + + Console.WriteLine($"Checking child process: '{processName}' (ID: {processId})"); + seen.Add(processId); foreach (var grandchild in child.GetChildren()) childrenToCheck.Enqueue(grandchild); - if (child.ProcessName.Equals(childName, StringComparison.OrdinalIgnoreCase)) + if (processName.Equals(childName, StringComparison.OrdinalIgnoreCase)) { children.Push(child); } @@ -789,7 +828,7 @@ public int RunTest(string executable, string outputFile, string errorFile, strin { cts.Cancel(); } - catch {} + catch { } outputWriter.WriteLine("\ncmdLine:{0} Timed Out (timeout in milliseconds: {1}{2}{3}, start: {4}, end: {5})", executable, timeout, (environmentVar != null) ? " from variable " : "", (environmentVar != null) ? TIMEOUT_ENVIRONMENT_VAR : "", @@ -851,7 +890,7 @@ private static string GetAllProcessNames_wmic() { // The command to execute string command = "wmic process get Name, ProcessId, ParentProcessId"; - + // Start the process and capture the output Process process = new Process(); process.StartInfo.FileName = "cmd.exe";