-
Notifications
You must be signed in to change notification settings - Fork 404
Stabilize the SOS test harness across platforms #6000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steveisok
merged 3 commits into
steveisok-sos-lldb-driver-layer
from
steveisok-sos-harness-stabilization-layer
Sep 7, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Runtime.InteropServices; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace SOS.TestHarness; | ||
|
|
||
| internal sealed record BoundedProcessResult(int ExitCode, string StandardOutput, string StandardError); | ||
|
|
||
| internal static partial class BoundedProcess | ||
| { | ||
| private const int KillSignal = 9; | ||
| private static readonly TimeSpan s_terminationTimeout = TimeSpan.FromSeconds(5); | ||
|
|
||
| public static BoundedProcessResult Run( | ||
| ProcessStartInfo startInfo, | ||
| TimeSpan timeout, | ||
| bool isolateLinuxProcessGroup = false, | ||
| TimeSpan? outputDrainTimeout = null) | ||
| { | ||
| if (!startInfo.RedirectStandardOutput || !startInfo.RedirectStandardError) | ||
| { | ||
| throw new ArgumentException("Standard output and standard error must both be redirected.", nameof(startInfo)); | ||
| } | ||
|
|
||
| ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(timeout, TimeSpan.Zero); | ||
|
|
||
| string command = $"{startInfo.FileName} {string.Join(' ', startInfo.ArgumentList)}".Trim(); | ||
| bool hasLinuxProcessGroup = isolateLinuxProcessGroup && OperatingSystem.IsLinux(); | ||
| if (hasLinuxProcessGroup) | ||
| { | ||
| WrapWithSetSid(startInfo); | ||
| } | ||
|
|
||
| using Process process = Process.Start(startInfo) | ||
| ?? throw new InvalidOperationException($"Failed to start '{command}'."); | ||
| int processGroupId = process.Id; | ||
| Task<string> stdoutTask = process.StandardOutput.ReadToEndAsync(); | ||
| Task<string> stderrTask = process.StandardError.ReadToEndAsync(); | ||
|
|
||
| if (!process.WaitForExit(TimeoutMilliseconds(timeout))) | ||
| { | ||
| Terminate(process, hasLinuxProcessGroup, processGroupId); | ||
| BoundedProcessResult output = DrainOutput( | ||
| process, | ||
| stdoutTask, | ||
| stderrTask, | ||
| command, | ||
| s_terminationTimeout); | ||
| throw new TimeoutException( | ||
| $"'{command}' did not exit within {timeout}.{Environment.NewLine}" + | ||
| $"stdout:{Environment.NewLine}{output.StandardOutput}{Environment.NewLine}" + | ||
| $"stderr:{Environment.NewLine}{output.StandardError}"); | ||
| } | ||
|
|
||
| // Linux single-file dump helpers can survive the target while retaining its redirected handles. | ||
| // End the isolated group before waiting for stream EOF so those descendants cannot wedge drainage. | ||
| if (hasLinuxProcessGroup) | ||
| { | ||
| KillProcessGroup(processGroupId); | ||
| } | ||
|
|
||
| return DrainOutput( | ||
| process, | ||
| stdoutTask, | ||
| stderrTask, | ||
| command, | ||
| outputDrainTimeout ?? s_terminationTimeout); | ||
| } | ||
|
|
||
| private static BoundedProcessResult DrainOutput( | ||
| Process process, | ||
| Task<string> stdoutTask, | ||
| Task<string> stderrTask, | ||
| string command, | ||
| TimeSpan timeout) | ||
| { | ||
| Task outputTask = Task.WhenAll(stdoutTask, stderrTask); | ||
| if (!outputTask.Wait(timeout)) | ||
| { | ||
| throw new TimeoutException( | ||
| $"'{command}' exited with code {process.ExitCode}, but its redirected output did not close " + | ||
| $"within {timeout}."); | ||
| } | ||
|
|
||
| return new BoundedProcessResult( | ||
| process.ExitCode, | ||
| stdoutTask.GetAwaiter().GetResult(), | ||
| stderrTask.GetAwaiter().GetResult()); | ||
| } | ||
|
|
||
| private static void Terminate(Process process, bool hasLinuxProcessGroup, int processGroupId) | ||
| { | ||
| if (hasLinuxProcessGroup) | ||
| { | ||
| KillProcessGroup(processGroupId); | ||
| } | ||
|
|
||
| if (!process.HasExited) | ||
| { | ||
| process.Kill(entireProcessTree: true); | ||
| } | ||
|
|
||
| process.WaitForExit(TimeoutMilliseconds(s_terminationTimeout)); | ||
| } | ||
|
|
||
| private static void WrapWithSetSid(ProcessStartInfo startInfo) | ||
| { | ||
| string setSid = File.Exists("/usr/bin/setsid") ? "/usr/bin/setsid" : | ||
| File.Exists("/bin/setsid") ? "/bin/setsid" : | ||
| throw new FileNotFoundException("Could not locate setsid for Linux process-group isolation."); | ||
|
|
||
| string executable = startInfo.FileName; | ||
| string[] arguments = startInfo.ArgumentList.ToArray(); | ||
| startInfo.FileName = setSid; | ||
| startInfo.ArgumentList.Clear(); | ||
| startInfo.ArgumentList.Add(executable); | ||
| foreach (string argument in arguments) | ||
| { | ||
| startInfo.ArgumentList.Add(argument); | ||
| } | ||
| } | ||
|
|
||
| private static void KillProcessGroup(int processGroupId) | ||
| { | ||
| _ = KillUnix(-processGroupId, KillSignal); | ||
| } | ||
|
|
||
| private static int TimeoutMilliseconds(TimeSpan timeout) => | ||
| (int)Math.Min(timeout.TotalMilliseconds, int.MaxValue); | ||
|
|
||
| [LibraryImport("libc", EntryPoint = "kill", SetLastError = true)] | ||
| private static partial int KillUnix(int processId, int signal); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.