Skip to content

[API Proposal]: Add Signal and ProcessExitStatus members to Process #128322

Closed

Description

@tmds

Background and motivation

In .NET 11, several new APIs are added to SafeProcessHandle including sending signals, and retrieving exit status.

This proposal is for making these available on Process too.

API Proposal

namespace System.Diagnostics
{
    public partial class Process
    {
        /// <summary>
        /// Sends the specified signal to the process.
        /// </summary>
        /// <returns>true if the signal was sent successfully; false if the process has already exited.</returns>
        public bool Signal(PosixSignal signal);
            // => SafeHandle.Signal(signal);

        /// <summary>
        /// Waits for the process to exit and returns the exit status.
        /// </summary>
        public ProcessExitStatus WaitForExitStatus();
            // => SafeHandle.WaitForExit();

        /// <summary>
        /// Waits for the process to exit within the specified timeout and returns the exit status.
        /// </summary>
        /// <returns>true if the process exited within the timeout; otherwise, false.</returns>
        public bool TryWaitForExitStatus(TimeSpan timeout, [NotNullWhen(true)] out ProcessExitStatus? exitStatus);
            // => SafeHandle.TryWaitForExit(timeout, out exitStatus);

        /// <summary>
        /// Asynchronously waits for the process to exit and returns the exit status.
        /// </summary>
        public Task<ProcessExitStatus> WaitForExitStatusAsync(CancellationToken cancellationToken = default);
            // => SafeHandle.WaitForExitAsync(cancellationToken);

/* Existing methods:
        public void WaitForExit();
        public bool WaitForExit(int milliseconds);
        public bool WaitForExit(System.TimeSpan timeout);
        public Task WaitForExitAsync(CancellationToken cancellationToken = default);
*/
    }
}

Optionally, rename the (new .NET 11) methods on SafeProcessHandle that deal with ProcessExitStatus to also include ExitStatus in their name.

class SafeProcessHandle
{
...
-        public bool TryWaitForExit(TimeSpan timeout, out ProcessExitStatus? exitStatus);
-        public ProcessExitStatus WaitForExit();
-        public Task<ProcessExitStatus> WaitForExitAsync(CancellationToken cancellationToken = default(CancellationToken));
+        public bool TryWaitForExitStatus(TimeSpan timeout, out ProcessExitStatus? exitStatus);
+        public ProcessExitStatus WaitForExitStatus();
+        public Task<ProcessExitStatus> WaitForExitStatusAsync(CancellationToken cancellationToken = default(CancellationToken));
-        public Task<ProcessExitStatus> WaitForExitOrKillOnCancellationAsync(CancellationToken cancellationToken);
+        public Task<ProcessExitStatus> WaitForExitStatusOrKillOnCancellationAsync(CancellationToken cancellationToken);
-        public ProcessExitStatus WaitForExitOrKillOnTimeout(TimeSpan timeout);
+        public ProcessExitStatus WaitForExitStatusOrKillOnTimeout(TimeSpan timeout);

API Usage

Sending a signal to a process

using var process = Process.Start("myapp");

// Gracefully request termination.
process.Signal(PosixSignal.SIGTERM);

Waiting for exit status

using var process = Process.Start("myapp");

ProcessExitStatus status = process.WaitForExitStatus();
Console.WriteLine($"Exited with status: {status}");

Waiting with a timeout

using var process = Process.Start("myapp");

if (process.TryWaitForExitStatus(TimeSpan.FromSeconds(30), out ProcessExitStatus? status))
{
    Console.WriteLine($"Process exited: {status}");
}
else
{
    Console.WriteLine("Timed out, killing process.");
    process.Kill();
}

Async wait

using var process = Process.Start("myapp");

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
ProcessExitStatus status = await process.WaitForExitStatusAsync(cts.Token);
Console.WriteLine($"Process exited: {status}");

Notes

  • The names include ExitStatus to avoid conflicts with existing members.
  • Like the SafeHandleProcess methods (and unlike the existing Process.WaitForExit methods) these methods do not wait for the EOF to have been received on a standard output and error when those are redirected.
  • Similar members could be added for ExitCode. Or, without these members, the user can get the ExitCode from the ProcessExitStatus.

Activity

  1. added
    untriaged
    on May 18, 2026
  2. dotnet-policy-service commented on May 18, 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. tmds commented on May 18, 2026

    @tmds
    MemberAuthor

    These were discussed a bit in #125838. I changed the proposed member names to use WaitFor instead of Get.

    cc @adamsitnik

  4. removed
    untriaged
    on May 22, 2026
  5. added
    api-suggestion
    on May 25, 2026
  6. adamsitnik commented on May 25, 2026

    @adamsitnik
    Member

    @tmds I like how you have solved the naming conflict problem by using WaitForExitStatus instead of WaitForExit 👍 At the same time, I am pretty sure the API design review board would complain about lack of consistency. But since we are still in preview mode, I believe we could most likely rename SafeProcessHandle.*WaitForExit* to SafeProcessHandle.*WaitForExitStatus*.

    Waiting with a timeout

    using var process = Process.Start("myapp");

    if (process.TryWaitForExitStatus(TimeSpan.FromSeconds(30), out ProcessExitStatus? status))
    {
    Console.WriteLine($"Process exited: {status}");
    }
    else
    {
    Console.WriteLine("Timed out, killing process.");
    process.Kill();
    }

    That is exactly why we have introduced SafeProcessHandle.WaitForExitOrKillOnTimeout and SafeProcessHandle.WaitForExitOrKillOnCancellationAsync. I am wondering if we should do the opposite: expose Process.WaitForExitOrKillOnTimeout and Process.WaitForExitOrKillOnCancellationAsync to guide the users towards using them as it would also reduce the likelihood of resource leaks due to zombie processes? And if for some reasons users don't want it, they could just use the methods exposed by SafeProcessHandle?

  7. tmds commented on May 25, 2026

    @tmds
    MemberAuthor

    I am wondering if we should do the opposite: expose Process.WaitForExitOrKillOnTimeout and Process.WaitForExitOrKillOnCancellationAsync to guide the users towards using them as it would also reduce the likelihood of resource leaks due to zombie processes?

    With the new process APIs, the main reason a user has a process instance is when they want to work with redirected streams. If you're working with redirected streams the WaitForExitOrKillXxx methods are not very useful because typically there is a Kill in a broader catch block which catches exceptions thrown from dealing with the redirected streams.

  8. tmds commented on May 26, 2026

    @tmds
    MemberAuthor

    Typically there is a Kill in a broader catch block which catches exceptions thrown from dealing with the redirected streams.

    See the example in #125838 (comment).

  9. tmds commented on May 29, 2026

    @tmds
    MemberAuthor

    I am pretty sure the API design review board would complain about lack of consistency. But since we are still in preview mode, I believe we could most likely rename SafeProcessHandle.WaitForExit to SafeProcessHandle.WaitForExitStatus.

    I've included this in an optional section in the API proposal.

  10. tmds commented on Jun 4, 2026

    @tmds
    MemberAuthor

    @adamsitnik @jkotas I'm going to set .NET 11 milestone to the API proposals I've created. The proposals are meant to address gaps in the new APIs or to be suggestions to improve them. The milestone is for either including the proposed API or to close the issue as I don't think we'll be considering these past .NET 11.

    @adamsitnik can you mark the APIs that you consider ready for review? You can also close the proposals that won't be considered.

  11. added this to the 11.0.0 milestone on Jun 4, 2026
  12. tmds commented on Jun 11, 2026

    @tmds
    MemberAuthor

    @adamsitnik it would be preferable if the higher-level Process exposes the proposed methods which SafeProcessHandle is adding. Is this ready for review?

  13. tmds commented on Jun 17, 2026

    @tmds
    MemberAuthor

    @adamsitnik this issue also includes some suggestions for renames (based on your feedback). If you want those to be possible, they should happen for .NET 11.

  14. tmds commented on Jul 13, 2026

    @tmds
    MemberAuthor

    @adamsitnik ptal whether you still want to address something for .NET 11.

  15. added
    api-ready-for-review
    blocking
    and removed
    api-suggestion
    on Jul 17, 2026
  16. adamsitnik commented on Jul 17, 2026

    @adamsitnik
    Member

    I've marked it as ready for review and added the blocking label.

    Rationale for blocking: we may need to rename few methods introduced to SafeProcessHandle for the sake of consistency, so it would be good to do it for .NET 11.

  17. bartonjs commented on Jul 21, 2026

    @bartonjs
    Member

    The proposed additions to Process look good as proposed. We don't feel it worth the hassle to rename the members on SafeProcessHandle.

    namespace System.Diagnostics
    {
        public partial class Process
        {
            public bool Signal(PosixSignal signal);
            public ProcessExitStatus WaitForExitStatus();
            public bool TryWaitForExitStatus(TimeSpan timeout, [NotNullWhen(true)] out ProcessExitStatus? exitStatus);
            public Task<ProcessExitStatus> WaitForExitStatusAsync(CancellationToken cancellationToken = default);
        }
    }
  18. added
    api-approved
    and removed
    blocking
    api-ready-for-review
    on Jul 21, 2026
  19. self-assigned this
    on Jul 21, 2026
  20. added a commit that references this issue on Jul 24, 2026
    9461aa0
  21. added a commit that references this issue on Jul 26, 2026
    2618b9e
  22. locked and limited conversation to collaborators on Aug 24, 2026
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