Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 48 additions & 9 deletions src/tests/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Process> GetChildren(this Process process)
{
var children = new List<Process>();
Expand Down Expand Up @@ -353,7 +383,7 @@ static bool RunProcess(string fileName, string arguments, TextWriter outputWrite

Task<string> stdOut = proc.StandardOutput.ReadToEndAsync();
Task<string> stdErr = proc.StandardError.ReadToEndAsync();
if(!proc.WaitForExit(DEFAULT_TIMEOUT_MS))
if (!proc.WaitForExit(DEFAULT_TIMEOUT_MS))
{
proc.Kill(true);
outputWriter.WriteLine($"Timedout: '{fileName} {arguments}");
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -569,7 +599,9 @@ public static bool TryPrintStackTraceFromCrashReport(string crashReportJsonFile,

symbolizerOutput = stdout.Result;

} catch (Exception e) {
}
catch (Exception e)

Copilot AI Mar 26, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modified exception handling in the symbolizer output code introduces an additional catch block. Consider consolidating the error handling to streamline the code and ensure that no redundant catch blocks exist.

Copilot uses AI. Check for mistakes.
{
outputWriter.WriteLine("Errors while running llvm-symbolizer --pretty-print");
outputWriter.WriteLine(e.ToString());
return false;
Expand Down Expand Up @@ -662,16 +694,23 @@ static unsafe IEnumerable<Process> FindChildProcessesByName(Process process, str
while (childrenToCheck.Count != 0)
{
Process child = childrenToCheck.Dequeue();
if (seen.Contains(child.Id))

if (!child.TryGetProcessId(out int processId))

Copilot AI Mar 26, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While using the new TryGetProcessId extension method enhances robustness, consider adding logging of the failure cases within the extension method to aid in debugging when a process has already exited.

Copilot uses AI. Check for mistakes.
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);
}
Expand Down Expand Up @@ -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 : "",
Expand Down Expand Up @@ -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";
Expand Down