Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Metalnem opened this issue May 3, 2023 · 5 comments · Fixed by #87174
Closed

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

Metalnem opened this issue May 3, 2023 · 5 comments · Fixed by #87174

Comments

@Metalnem
Copy link

Metalnem commented May 3, 2023

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 3, 2023
@ghost
Copy link

ghost commented May 3, 2023

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: Metalnem
Assignees: -
Labels:

area-System.Diagnostics.Process, untriaged

Milestone: -

@skyoxZ
Copy link
Contributor

skyoxZ commented May 6, 2023


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.

@danmoseley
Copy link
Member

Either of you interested in offering a PR?

@skyoxZ
Copy link
Contributor

skyoxZ commented May 6, 2023

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

@danmoseley
Copy link
Member

Done!

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 7, 2023
@adamsitnik adamsitnik removed the untriaged New issue has not been triaged by the area owner label May 11, 2023
@adamsitnik adamsitnik added this to the 8.0.0 milestone May 11, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 6, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jun 6, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jun 20, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.