diff --git a/src/tests/Common/CoreCLRTestLibrary/CoreclrTestWrapperLib.cs b/src/tests/Common/CoreCLRTestLibrary/CoreclrTestWrapperLib.cs index d09282fc976844..77d1465e351190 100644 --- a/src/tests/Common/CoreCLRTestLibrary/CoreclrTestWrapperLib.cs +++ b/src/tests/Common/CoreCLRTestLibrary/CoreclrTestWrapperLib.cs @@ -294,6 +294,16 @@ static bool CollectCrashDumpWithMiniDumpWriteDump(Process process, string crashD return collectedDump; } + // Kills the given process using 'sudo kill -9', which is required when the process runs + // as root (e.g. launched via 'sudo') and cannot be killed by a non-root process. + static void KillWithSudo(Process process) + { + if (process.TryGetProcessId(out int pid)) + { + Process.Start("sudo", $"-n kill -9 {pid}").WaitForExit(); + } + } + static bool CollectCrashDumpWithCreateDump(Process process, string crashDumpPath, StreamWriter outputWriter) { string? coreRoot = Environment.GetEnvironmentVariable("CORE_ROOT"); @@ -308,7 +318,6 @@ static bool CollectCrashDumpWithCreateDump(Process process, string crashDumpPath createdump.StartInfo.FileName = "sudo"; createdump.StartInfo.Arguments = $"{createdumpPath} {arguments}"; - createdump.StartInfo.UseShellExecute = false; createdump.StartInfo.RedirectStandardOutput = true; createdump.StartInfo.RedirectStandardError = true; @@ -335,7 +344,6 @@ static bool CollectCrashDumpWithCreateDump(Process process, string crashDumpPath chown.StartInfo.FileName = "sudo"; chown.StartInfo.Arguments = $"chown \"{Environment.UserName}\" \"{crashDumpPath}\""; - chown.StartInfo.UseShellExecute = false; chown.StartInfo.RedirectStandardOutput = true; chown.StartInfo.RedirectStandardError = true; @@ -354,22 +362,10 @@ static bool CollectCrashDumpWithCreateDump(Process process, string crashDumpPath } else { - // Workaround for https://github.com/dotnet/runtime/issues/93321 - const int MaxRetries = 5; - for (int i = 0; i < MaxRetries; i++) - { - try - { - createdump.Kill(entireProcessTree: true); - break; - } - catch (Exception e) when (i < MaxRetries - 1) - { - Console.WriteLine($"Process.Kill(entireProcessTree: true) failed:"); - Console.WriteLine(e); - Console.WriteLine("Retrying..."); - } - } + // createdump was launched via 'sudo', so the process and its children run as root. + // We cannot send SIGKILL to root-owned processes from a non-root process (EPERM). + // Use 'sudo kill' to terminate the timed-out process tree. + KillWithSudo(createdump); } return fSuccess && createdump.ExitCode == 0; @@ -466,7 +462,6 @@ public int RunTest(string executable, string outputFile, string errorFile, strin process.StartInfo.Arguments = executable; } - process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.EnvironmentVariables.Add("__Category", category); @@ -564,7 +559,6 @@ private static string GetAllProcessNames_wmic() process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = $"/c {command}"; process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; // Start the process and read the output