Repository navigation
Closed
Description
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
skyoxZ commentedon May 6, 2023
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessWaitState.Unix.cs
Line 551 in e61e022
It seems that Task.Run could be unexpectedly cancelled by this token.
In such a situation the delegate doesn't run and let the
ProcessWaitStateinstance be in an unexpected state where_waitInProgressbeing not null, which may affect otherProcess.WaitForExitcalls on the same process.danmoseley commentedon May 6, 2023
Either of you interested in offering a PR?
skyoxZ commentedon May 6, 2023
@danmoseley I'd like to have a try. Could you please assign it to me?
danmoseley commentedon May 6, 2023
Done!