Skip to content

Let Process.Run* kill the entire process tree on timeout or cancellation #136466

Description

@adamsitnik

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

  1. added
    untriaged
    on Oct 26, 2026
  2. dotnet-policy-service commented on Oct 26, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
    See info in area-owners.md if you want to be subscribed.

  3. removed
    untriaged
    on Oct 26, 2026
  4. added this to the 12.0.0 milestone on Oct 26, 2026
  5. adamsitnik commented on Oct 26, 2026

    @adamsitnik
    MemberAuthor

    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.

  6. changed the title [-][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[/+] on Oct 27, 2026
  7. changed the title [-][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[/+] on Nov 10, 2026
  8. added
    api-ready-for-review
    on Nov 10, 2026
  9. self-assigned this
    on Nov 10, 2026
  10. adamsitnik commented on Nov 10, 2026

    @adamsitnik
    MemberAuthor

    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.

  11. MichalPetryka commented on Nov 10, 2026

    @MichalPetryka
    Contributor

    I'd use a different name than entireProcessTree as that doesn't immediately make it clear it's about timeouts.

  12. adamsitnik commented on Nov 16, 2026

    @adamsitnik
    MemberAuthor

    I'd use a different name than entireProcessTree as that doesn't immediately make it clear it's about timeouts.

    I hear you, but I currently don't have a better idea. I hope that the API Review Board helps me with that.

  13. kevincathcart-cas commented on Nov 16, 2026

    @kevincathcart-cas

    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 --wait or 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.

  14. bartonjs commented on Nov 17, 2026

    @bartonjs
    Member

    Video

    • We prefer putting the option on ProcessStartInfo.
    • To mitigate the confusion of the value when it is returned from querying running processes, we made it nullable.
    • After discussing names for a long time, we landed on KillTreeOnRunCancellation
    namespace System.Diagnostics
    {
        public class ProcessStartInfo
        {
            public bool? KillTreeOnRunCancellation { get; set; }
        } 
    }
  15. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 17, 2026
  16. Neme12 commented on Nov 18, 2026

    @Neme12

    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 with KillOnParentExit to end with a verb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Diagnostics.Process

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions