Skip to content

Implement SafeProcessHandle.Start #126107

Closed

Description

@adamsitnik
  • The API got re-approved in New Process APIs #125838 (comment)
  • We can just move Process.StartCore to SafeProcessHandle.Start.
  • We should consider using process file descriptors when available on Unix (modern Linux and FreeBSD as long as FreeBSD does nto kill processes on close). But this may require braking changes due to SafeWaitHandle role in SafeProcessHandle.Unix.cs

// On Windows, SafeProcessHandle represents the actual OS handle for the process.
// On Unix, there's no such concept. Instead, the implementation manufactures
// a WaitHandle that it manually sets when the process completes; SafeProcessHandle
// then just wraps that same WaitHandle instance. This allows consumers that use
// Process.{Safe}Handle to initialize and use a WaitHandle to successfully use it on
// Unix as well to wait for the process to complete.
private readonly SafeWaitHandle? _handle;
private readonly bool _releaseRef;
internal SafeProcessHandle(int processId, SafeWaitHandle handle) :
this(handle.DangerousGetHandle(), ownsHandle: true)
{
ProcessId = processId;
_handle = handle;
handle.DangerousAddRef(ref _releaseRef);
}
internal int ProcessId { get; }
protected override bool ReleaseHandle()
{
if (_releaseRef)
{
Debug.Assert(_handle != null);
_handle.DangerousRelease();
}
return true;
}

It allows to create a SafeWaitHandle out of SafeProcessHandle.handle:

mre.SetSafeWaitHandle(new SafeWaitHandle(useSafeHandle ? h.Process.SafeHandle.DangerousGetHandle() : h.Process.Handle, ownsHandle: false));

More context: dotnet/corefx#36199, #28773

cc @stephentoub (I don't have a solution right now, just letting you know that the handle could become a real handle on Linux and FreeBSD)

Activity

  1. added this to the 11.0.0 milestone on Mar 25, 2026
  2. self-assigned this
    on Mar 25, 2026
  3. dotnet-policy-service commented on Mar 25, 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.

  4. adamsitnik commented on Mar 26, 2026

    @adamsitnik
    MemberAuthor

    I don't have a solution right now

    Thinking out loud: I need to explore what it would take to simply return pidf from the SafeProcessHandle.DangerousGetHandle and get SafeWaitHandle to work with such input on Unix. It's a pollable descriptor, so it should be able to get it to work.

  5. jkotas commented on Mar 26, 2026

    @jkotas
    Member

    SafeProcessHandle.DangerousGetHandle and get SafeWaitHandle to work with such input on Unix. It's a pollable descriptor, so it should be able to get it to work.

    I do not see how it can work. SafeWaitHandle on non-Windows is our own simulator of Windows OS wait handles semantics. It does not interop with OS provided poll, etc.

  6. locked and limited conversation to collaborators on Apr 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