Skip to content

[API Proposal]: Kill System.Diagnostics.Process on parent death #101985

@elijahr2411

Description

@elijahr2411

Background and motivation

Currently in .NET there is no way to ensure that a child process created through System.Diagnostics.Process is killed when the parent process dies, particularly unexpectedly by a SIGKILL. This proposal creates a simple property in the ProcessStartInfo class that would cause a child process to be killed by the OS in these cases.

On Linux, this could be done very easily by setting the PR_SET_PDEATHSIG flag on the fork using a prctl() syscall.

I'm not too well versed with the Windows platform but I believe something with Job Objects is possible.

I've named the property KillOnParentDeath, this can of course be changed if a better name is thought of

API Proposal

namespace System.Diagnostics;

public partial sealed class ProcessStartInfo
{
    public bool KillOnParentExit { get; set; }
}

API Usage

// Create a process
var proc = new System.Diagnostics.Process();

// Set KillOnParentDeath
proc.StartInfo.KillOnParentExit = true;

// If this C# program is then killed for some reason, the child will die with it instead of lingering.

Alternative Designs

No response

Risks

None as far as I'm aware. The default value should of course be false to emulate current behavior.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions