Skip to content

Commit 0ee8cba

Browse files
authored
Fix CoreclrTestWrapperLib to be robust against existed processes (#113937)
Fixes #112846
1 parent 4957991 commit 0ee8cba

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

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

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,36 @@ public static unsafe bool ListChildPids(int ppid, out int[] buffer)
111111

112112
internal static class ProcessExtensions
113113
{
114+
public static bool TryGetProcessId(this Process process, out int processId)
115+
{
116+
try
117+
{
118+
processId = process.Id;
119+
return true;
120+
}
121+
catch
122+
{
123+
// Process exited
124+
processId = default;
125+
return false;
126+
}
127+
}
128+
129+
public static bool TryGetProcessName(this Process process, out string processName)
130+
{
131+
try
132+
{
133+
processName = process.ProcessName;
134+
return true;
135+
}
136+
catch
137+
{
138+
// Process exited
139+
processName = default;
140+
return false;
141+
}
142+
}
143+
114144
public unsafe static IEnumerable<Process> GetChildren(this Process process)
115145
{
116146
var children = new List<Process>();
@@ -353,7 +383,7 @@ static bool RunProcess(string fileName, string arguments, TextWriter outputWrite
353383

354384
Task<string> stdOut = proc.StandardOutput.ReadToEndAsync();
355385
Task<string> stdErr = proc.StandardError.ReadToEndAsync();
356-
if(!proc.WaitForExit(DEFAULT_TIMEOUT_MS))
386+
if (!proc.WaitForExit(DEFAULT_TIMEOUT_MS))
357387
{
358388
proc.Kill(true);
359389
outputWriter.WriteLine($"Timedout: '{fileName} {arguments}");
@@ -394,7 +424,7 @@ public static bool TryPrintStackTraceFromCrashReport(string crashReportJsonFile,
394424
string? userName = Environment.GetEnvironmentVariable("USER");
395425
if (string.IsNullOrEmpty(userName))
396426
{
397-
userName="helixbot";
427+
userName = "helixbot";
398428
}
399429

400430
if (!RunProcess("sudo", $"chmod a+rw {crashReportJsonFile}", Console.Out))
@@ -569,7 +599,9 @@ public static bool TryPrintStackTraceFromCrashReport(string crashReportJsonFile,
569599

570600
symbolizerOutput = stdout.Result;
571601

572-
} catch (Exception e) {
602+
}
603+
catch (Exception e)
604+
{
573605
outputWriter.WriteLine("Errors while running llvm-symbolizer --pretty-print");
574606
outputWriter.WriteLine(e.ToString());
575607
return false;
@@ -662,16 +694,23 @@ static unsafe IEnumerable<Process> FindChildProcessesByName(Process process, str
662694
while (childrenToCheck.Count != 0)
663695
{
664696
Process child = childrenToCheck.Dequeue();
665-
if (seen.Contains(child.Id))
697+
698+
if (!child.TryGetProcessId(out int processId))
699+
continue;
700+
701+
if (seen.Contains(processId))
666702
continue;
667703

668-
Console.WriteLine($"Checking child process: '{child.ProcessName}' (ID: {child.Id})");
669-
seen.Add(child.Id);
704+
if (!child.TryGetProcessName(out string processName))
705+
continue;
706+
707+
Console.WriteLine($"Checking child process: '{processName}' (ID: {processId})");
708+
seen.Add(processId);
670709

671710
foreach (var grandchild in child.GetChildren())
672711
childrenToCheck.Enqueue(grandchild);
673712

674-
if (child.ProcessName.Equals(childName, StringComparison.OrdinalIgnoreCase))
713+
if (processName.Equals(childName, StringComparison.OrdinalIgnoreCase))
675714
{
676715
children.Push(child);
677716
}
@@ -789,7 +828,7 @@ public int RunTest(string executable, string outputFile, string errorFile, strin
789828
{
790829
cts.Cancel();
791830
}
792-
catch {}
831+
catch { }
793832

794833
outputWriter.WriteLine("\ncmdLine:{0} Timed Out (timeout in milliseconds: {1}{2}{3}, start: {4}, end: {5})",
795834
executable, timeout, (environmentVar != null) ? " from variable " : "", (environmentVar != null) ? TIMEOUT_ENVIRONMENT_VAR : "",
@@ -851,7 +890,7 @@ private static string GetAllProcessNames_wmic()
851890
{
852891
// The command to execute
853892
string command = "wmic process get Name, ProcessId, ParentProcessId";
854-
893+
855894
// Start the process and capture the output
856895
Process process = new Process();
857896
process.StartInfo.FileName = "cmd.exe";

0 commit comments

Comments
 (0)