Repository navigation
Description
Background and motivation
The .NET 11 System.Diagnostics.Process.Run* convenience methods kill only the immediate child process on timeout or cancellation. Descendants started by that child can remain running.
This matters when launching shells, scripts, build tools, or other programs that spawn subprocesses. Timing out or canceling the operation can leave work running in the background, consuming resources or holding files and redirected standard-stream handles open.
The helpers do not provide a way to request whole-process-tree cleanup. Callers needing it must abandon the convenience methods and manually combine Process.Start, output capture, timeout accounting, exit waiting, and Kill(entireProcessTree: true).
API Proposal
Introduce four new overloads that take mandatory bool entireProcessTree parameter. The name comes from the existing Process.Kill(bool entireProcessTree). The timeout and cancellation token arguments are also mandatory.
namespace System.Diagnostics
{
public class Process : System.ComponentModel.Component, System.IDisposable
{
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan timeout, bool entireProcessTree);
public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, bool entireProcessTree, CancellationToken cancellationToken);
public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, TimeSpan timeout, bool entireProcessTree);
public static Task<ProcessTextOutput> RunAndCaptureTextAsync(ProcessStartInfo startInfo, bool entireProcessTree, CancellationToken cancellationToken);
}
}For those who prefer the diff syntax or just want to get the full picture:
namespace System.Diagnostics
{
public class Process : System.ComponentModel.Component, System.IDisposable
{
public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default);
+ public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan timeout, bool entireProcessTree);
public static ProcessExitStatus Run(string fileName, IEnumerable<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
+ public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, bool entireProcessTree, CancellationToken cancellationToken);
public static Task<ProcessExitStatus> RunAsync(string fileName, IEnumerable<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, TimeSpan? timeout = default);
+ public static ProcessTextOutput RunAndCaptureText(ProcessStartInfo startInfo, TimeSpan timeout, bool entireProcessTree);
public static ProcessTextOutput RunAndCaptureText(string fileName, IEnumerable<string>? arguments = null, TimeSpan? timeout = default);
public static Task<ProcessTextOutput> RunAndCaptureTextAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
+ public static Task<ProcessTextOutput> RunAndCaptureTextAsync(ProcessStartInfo startInfo, bool entireProcessTree, CancellationToken cancellationToken);
public static Task<ProcessTextOutput> RunAndCaptureTextAsync(string fileName, IEnumerable<string>? arguments = null, CancellationToken cancellationToken = default);
}
}API Usage
ProcessTextOutput output = Process.RunAndCaptureText(startInfo, TimeSpan.FromSeconds(1), entireProcessTree: true);Alternative Designs
We could extend ProcessStartInfo with a new property that would control that. Only some APIs that take ProcessStartInfo would respect it (Process.Run*), and other APIs like Process.Start would not. This could be confusing for the end users.
namespace System.Diagnostics
{
public class ProcessStartInfo
{
public bool KillEntireProcessTree { get; set; }
}
}Risks
It's not the first time we extend the Run methods with a new argument (see #128453).
Activity
dotnet-policy-service commentedon Oct 26, 2026
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.
adamsitnik commentedon Oct 26, 2026
Note from human: both Run and RunAsync use only SafeProcessHandle, which lacks the
Kill(entireProcessTree: true)overload. It would be nice to have the best of both words: keep using SPH but also extend it.[-][Breaking change proposal] Make Process.Run* kill the entire process tree on timeout or cancellation[/-][+][Breaking change] Make Process.Run* kill the entire process tree on timeout or cancellation[/+][-][Breaking change] Make Process.Run* kill the entire process tree on timeout or cancellation[/-][+]Let Process.Run* kill the entire process tree on timeout or cancellation[/+]adamsitnik commentedon Nov 10, 2026
To people who have subscribed up for notifications for this issue: I've turned it into an API proposal, PTAL and let me know if you like it.
MichalPetryka commentedon Nov 10, 2026
I'd use a different name than
entireProcessTreeas that doesn't immediately make it clear it's about timeouts.adamsitnik commentedon Nov 16, 2026
I hear you, but I currently don't have a better idea. I hope that the API Review Board helps me with that.
kevincathcart-cas commentedon Nov 16, 2026
Does this also mean that it waits for the entire process tree before returning? A process can certainly finish running while still having children running, and the description here makes it sound like this is only about killing the processes on timeout or cancellation, but conceptually one could also wait for the full tree to finish even once the immediate child does, and doing that would have different implications.
Sometimes not waiting is certainly desirable even if killing on cancelation would be fine. For example if it created a child cache process on demand, that deliberately hangs around a while in case another command is run. Waiting here for the children would be a terrible idea, but letting it get killed on timeout can be harmless, and may even be needed if it was really a grandchild process of something run as a script, where it is important that the direct children get killed on cancellation.
But there are some other cases where you really do want to wait for the children after initial exit. Some commands are basically just a launcher that launch the real program. Ideally, they should use
exec()for the real program, or else provide some form of--waitor other blocking option, but those options are not always present, especially if a program was designed mostly for GUI usage. I'm fine with calling this an out-of-scope advanced scenario since it isn't the most common, but if the option name sounds like it could be intended for exactly this scenario, that could cause confusion.bartonjs commentedon Nov 17, 2026
Video
Replace Process.Run* tree-termination overloads with ProcessStartInfo…
Neme12 commentedon Nov 18, 2026
Why not just
KillTreeOnRunCanceled? I feel like Canceled is more idiomatic than Cancellation. An event or callback would generally be called Canceled or OnCanceled. And it aligns more withKillOnParentExitto end with a verb.