Skip to content

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

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.

Activity

  1. added
    api-suggestion
    on May 7, 2024
  2. added
    untriaged
    on May 7, 2024
  3. dotnet-policy-service commented on May 7, 2024

    @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.

  4. elijahr2411 commented on May 7, 2024

    @elijahr2411
    Author

    Implements #96470

  5. SteveSyfuhs commented on May 7, 2024

    @SteveSyfuhs

    On Windows, the parent process would need to spawn the child process through a Job: https://learn.microsoft.com/en-us/windows/win32/procthread/job-objects. I've never understood why this functionality hasn't been exposed in .NET. It's certainly very useful, albeit somewhat niche -- though I think that's actually a chicken and egg problem.

  6. KalleOlaviNiemitalo commented on May 13, 2024

    @KalleOlaviNiemitalo

    Could be a property of System.Diagnostics.ProcessStartInfo instead. That way, the getter of the property would just read a field and never need any interop calls.

  7. KalleOlaviNiemitalo commented on May 13, 2024

    @KalleOlaviNiemitalo

    On Windows, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE looks like a way to implement this. Have the parent process create a job object with that limit (and JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK?), never close the job handle, and don't make the handle inheritable. Place child processes in that job whenever KillOnParentDeath is true.

  8. adamsitnik commented on Jul 17, 2024

    @adamsitnik
    Member

    Currently, on Windows .NET is using following sys-calls to start a process:

    if (!(_succeeded = Interop.Shell32.ShellExecuteExW(_executeInfo)))

    retVal = Interop.Advapi32.CreateProcessWithLogonW(

    Using JOB APIs makes sense, but we would need to ensure that all properties exposed by ProcessStartInfo are respected and supported when this API is used. Before we make any progress with this proposal, somebody needs to verify whether this is possible. I currently don't have any free cycles and the best I can do right now is to mark it as "help wanted" hoping that somebody will provide a working prototype. If this happens, I am going to change the API proposal to extend ProcessStartInfo with a new property and present it to the API review board.

  9. added
    help wanted
    and removed
    untriaged
    on Jul 17, 2024
  10. added this to the Future milestone on Jul 17, 2024
  11. KalleOlaviNiemitalo commented on Jul 17, 2024

    @KalleOlaviNiemitalo

    .NET also uses CreateProcess, when ProcessStartInfo.UserName is empty:

    For CreateProcessWithLogonW or CreateProcess on Windows 10, I believe the new process can be atomically made a member of a job, via STARTUPINFOEXW::lpAttributeList and UpdateProcThreadAttribute PROC_THREAD_ATTRIBUTE_JOB_LIST.

    ShellExecuteExW doesn't take a STARTUPINFOEXW structure, and SHELLEXECUTEINFOW doesn't have a similar feature. One can set the SEE_MASK_FLAG_HINST_IS_SITE flag and provide an ICreatingProcess implementation, but ICreateProcessInputs doesn't support setting a job handle.

    It would be possible to read SHELLEXECUTEINFOW::hProcess after ShellExecuteExW finishes, and call AssignProcessToJobObject then. However:

    • If the child process already started grandchild processes, then those would not be assigned to the job. That could perhaps be prevented with ICreateProcessInputs::AddCreateFlags(CREATE_SUSPENDED) but that seems likely to cause worse problems, e.g. if ShellExecuteEx waits for the process to do something.
    • If Windows already assigned the child process to a job for application compatibility purposes, then AssignProcessToJobObject would fail. What would .NET do then -- terminate the child process right away?
    • If ShellExecuteExW notified an existing process instead of starting a new one, then no process handle is available. That can't be helped.

    A more robust solution might be to start a shim child process with CreateProcess, assign it to a job, and have it call ShellExecuteExW to start the program specified in ProcessStartInfo. That would however require an executable for the shim process, likely also an MSBuild property for configuring whether to publish that shim with the application.

    I feel it would be better to make Process.Start throw if ProcessStartInfo requests both UseShellExecute and KillOnParentDeath.

  12. elijahr2411 commented on Dec 24, 2024

    @elijahr2411
    Author

    I've modified the proposal to reflect that ProcessStartInfo should be the class extended.

  13. taspeotis commented on May 20, 2025

    @taspeotis

    I had a similar problem - but only for Windows - and I've made a library that does the necessary PInvoke bits to achieve it.

    https://github.com/taspeotis/ProcThreadAttributeJobListDotNet

    The intended usage is JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE combined with PROC_THREAD_ATTRIBUTE_JOB_LIST to atomically create the process in the job.

  14. 6 remaining items

  15. removed
    api-ready-for-review
    on Mar 27, 2026
  16. adamsitnik commented on Mar 27, 2026

    @adamsitnik
    Member

    The API got approved as part of the bigger proposal: #125838 (comment)

    I am going to include it in .NET 11, my plan is to get #13943 implemented first (it's going to require us to start using STARTUPINFOEX on Windows) and take the Windows implementation from #124375 (got approved but not marged due to the fact that we decided to give up on having another option bag for Process).

    For Linux, @tmds has pointed one important limitation: #124979 (comment) namely PR_SET_PDEATHSIG is raised when the thread that created the process exits. So we will need to take care of that.

    On macOS there seems to be no built-in mechanism to achieve that that would not come with plenty of side-effects.

  17. adamsitnik commented on Apr 15, 2026

    @adamsitnik
    Member

    The API got implemented for Windows in #126699 and will be shipped as part of .NET 11 Preview 4.

    Now the tricky thing: the Unix implementation.

    It can't match Windows, where the setting is derived by child processes and ensures it's killed by the OS on any kind of process exit (normal exit, forced/graceful shutdown, crash). None of the UNIX systems provide a similar mechanism. So "best effort" is the best we can do.

    Linux: as pointed by @tmds in #124979 (comment) and @dmitry-azaraev in #96470 (comment) PR_SET_PDEATHSIG sends signal when parent thread dies. Thread Pool threads are not guaranteed to live forever, we have actually run into bugs in the past where async IO request were cancelled because the thread that issued them has exited. The fix was to check for any pending IO before killing the thread (#82245). AFAIK on Linux there is no such mechanism to check if given thread was used with PR_SET_PDEATHSIG. So if we want to support this API for crashes and forced termination on Linux, we have to maintain a dedicated thread that would be responsible for spawning such processes.

    @jkotas is it acceptable? It would be spawned only when this optional feature would be enabled.

    For other Unixes, the best we can do is to handle process exit and graceful termination and simply kill all child processes that were spawned with this setting enabled. I was thinking about extending ProcessWaitState.Unix.cs responsibility with this, as it already contains the information about all spawned processes and handles one signal (SIGCHILD). @tmds what do you think?

    I am opened to any suggestions and feedback. I want to make sure C# devs don't need to implement it on their own and invent things like ProcessReaper.

  18. jkotas commented on Apr 16, 2026

    @jkotas
    Member

    @jkotas is it acceptable? It would be spawned only when this optional feature would be enabled.

    Sounds reasonable, as long as the RPC between the threads is kept lightweight (in terms of code that it depends on).

  19. adamsitnik commented on Jun 12, 2026

    @adamsitnik
    Member

    In Preview 4 and 5, we have included the Windows and Linux (and Android) implementations that provide strong guarantee that the process is killed no matter if parent exits gracefully, is forced to exit or crashes.

    But there are other operating systems like macOS which don't provide any built-in mechanism that we could use to achieve same guarantees.

    Our customers workaround it by implementing utilities like this one, where they sign up for the AppDomain.CurrentDomain.ProcessExit event and try to kill the child process. I would like to avoid that and make KillOnParentExit a best effort API:

    • handle all kinds of exits on Windows, Linux and Android,
    • handle graceful termination and normal exit on other OSes like macOS (and of course document it as such).

    @jkotas please let me know if you are supportive of such approach

  20. jkotas commented on Jun 12, 2026

    @jkotas
    Member

    The best effort implementation is effectively going to be KillBeforeParentGracefulExit. The code running in the parent process may observe that the child process was killed - WaitForExit will return, broken pipes will start triggering, etc.

    The utility that you have shared requires a handshake with the caller to deal with this race condition. It blocks in Dispose if the process started shutting down.

    How is the built-in best effort implementation going to handle this to be a replacement for this utility?

  21. adamsitnik commented on Jun 24, 2026

    @adamsitnik
    Member

    How is the built-in best effort implementation going to handle this to be a replacement for this utility?

    The way I was thinking about it:

    • Broken pipes should not be a problem, as killing a process should close the pipe and simply report EOF.
    • Unregister the SIGCHILD handler that is responsible for reaping processes, so none of the WaitForExit calls returns.
    • Kill all children processes in a loop started with KillOnParentExit
    • Reap all children processes (IIRC this should be just a single sys-call)
  22. jkotas commented on Jun 25, 2026

    @jkotas
    Member

    Unregister the SIGCHILD handler that is responsible for reaping processes, so none of the WaitForExit calls returns.

    This can cause dead locks. I do not think you want to do that. There may be other unrelated other threads in the process running that may be waiting for WaitForExit to make progress.

    Using KillOnParentExit in one place should not negatively impact unrelated Process API usages.

  23. adamsitnik commented on Jun 29, 2026

    @adamsitnik
    Member

    OK, in such case I am closing the issue. Windows support was included in .NET 11 Preview 4, Linux and Android in Preview 5.

  24. locked and limited conversation to collaborators on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions