dotnet / dotnet/runtime

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

Open
#128,474 8 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-System.Diagnostics.Process
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Description

### 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:

```csharp
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:

```csharp
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

```csharp
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:**

```csharp
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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.