Skip to content

Process.WaitForExit(Int32) sometimes throws AggregateException on macOS #85717

Closed

Description

@Metalnem

Description

Process.WaitForExit(Int32) overload sometimes unexpectedly throws AggregateException (with TaskCanceledException inside). The issue can be reliably reproduced on macOS, but it doesn't seem to be happening on Windows.

Reproduction Steps

The following code snippet will trigger the issue on macOS (usually after several seconds, but sometimes it will take 1-2 minutes):

for (int i = 0; ; ++i)
{
    try
    {
        var process = Process.GetCurrentProcess();
        process.WaitForExit(10);
    }
    catch
    {
        Console.WriteLine($"{nameof(Process.WaitForExit)} failed in iteration {i}");
        throw;
    }
}

It doesn't matter which process instance was used (that is, you can use any active process and call Process.GetProcessById).

Expected behavior

Process.WaitForExit(Int32) shouldn't throw AggregateException.

Actual behavior

Process.WaitForExit(Int32) throws AggregateException on macOS (but not on Windows):

WaitForExit failed in iteration 979
Unhandled exception. System.AggregateException: One or more errors occurred. (A task was canceled.)
 ---> System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at System.Threading.Tasks.Task.GetExceptions(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Diagnostics.ProcessWaitState.WaitForExit(Int32 millisecondsTimeout)
   at System.Diagnostics.Process.WaitForExitCore(Int32 milliseconds)
   at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)
   at WaitForExit.Program.Main(String[] args) in /Users/Metalnem/Temp/WaitForExit/Program.cs:line 14
--- End of stack trace from previous location ---

   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Diagnostics.ProcessWaitState.WaitForExit(Int32 millisecondsTimeout)
   at System.Diagnostics.Process.WaitForExitCore(Int32 milliseconds)
   at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)
   at WaitForExit.Program.Main(String[] args) in /Users/Metalnem/Temp/WaitForExit/Program.cs:line 14

Regression?

The issue is present at least in .NET 6.0, 7.0, and 8.0.

Known Workarounds

No response

Configuration

.NET SDK:
 Version:   8.0.100-preview.3.23178.7
 Commit:    e300b0e1e6

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  10.15
 OS Platform: Darwin
 RID:         osx.10.15-x64
 Base Path:   /usr/local/share/dotnet/sdk/8.0.100-preview.3.23178.7/

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.0-preview.3.23174.8
  Architecture: x64
  Commit:       47bad717bd

.NET SDKs installed:
  6.0.302 [/usr/local/share/dotnet/sdk]
  7.0.102 [/usr/local/share/dotnet/sdk]
  8.0.100-preview.3.23178.7 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.7 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.2 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.3.23177.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.7 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.2 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.3.23174.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other information

No response

Activity

  1. ghost added
    untriaged
    on May 3, 2023
  2. skyoxZ commented on May 6, 2023

    @skyoxZ
    Contributor


    It seems that Task.Run could be unexpectedly cancelled by this token.

    In such a situation the delegate doesn't run and let the ProcessWaitState instance be in an unexpected state where _waitInProgress being not null, which may affect other Process.WaitForExit calls on the same process.

  3. danmoseley commented on May 6, 2023

    @danmoseley
    Contributor

    Either of you interested in offering a PR?

  4. skyoxZ commented on May 6, 2023

    @skyoxZ
    Contributor

    @danmoseley I'd like to have a try. Could you please assign it to me?

  5. danmoseley commented on May 6, 2023

    @danmoseley
    Contributor

    Done!

  6. ghost added
    in-pr
    on May 7, 2023
  7. removed
    untriaged
    on May 11, 2023
  8. added this to the 8.0.0 milestone on May 11, 2023
  9. ghost removed
    in-pr
    on Jun 6, 2023
  10. ghost added
    in-pr
    on Jun 6, 2023
  11. ghost removed
    in-pr
    on Jun 20, 2023
  12. ghost locked as resolved and limited conversation to collaborators on Jul 21, 2023
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