Skip to content

[API Proposal]: ProcessStartInfo: enable killing process on dispose #128474

@tmds

Description

@tmds

Background and motivation

Process.Dispose only releases associated resources (handles) without terminating the child process. This frequently leads to leaked child processes when developers use the natural using pattern:

using Process process = Process.Start("long-running-tool")!;
// ... use the process ...
// process.Dispose() is called here, but the child process keeps running

Today, to ensure a child process is killed on dispose, users must write manual try/finally blocks:

using Process process = Process.Start("long-running-tool")!;
try
{
    // ... use the process ...
}
catch
{
    process.Kill(entireProcessTree: true);
    throw;
}

A ProcessStartInfo.DisposeBehavior property would allow the using pattern to work as users intuitively expect and eliminate an entire class of leaked-process bugs.

API Proposal

namespace System.Diagnostics
{
    public enum ProcessDisposeBehavior
    {
        None = 0,
        KillProcess = 1,
        KillProcessTree = 2
    }

    public partial class ProcessStartInfo
    {
        public ProcessDisposeBehavior DisposeBehavior { get; set; }
    }
}

API Usage

Basic usage — kill a long-running process tree when done:

using Process process = Process.Start(new ProcessStartInfo("dev-server")
{
    DisposeBehavior = ProcessDisposeBehavior.KillProcessTree
})!;

// ... interact with the dev server ...

// process.Dispose() is called here, which kills the process tree first

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions