Repository navigation
Fix ObjectDisposedException in UnixProcessReaper during Ctrl+C shutdown - #55121
Merged
JeremyKuhne merged 4 commits intoJul 2, 2026
Merged
Conversation
On Unix, dotnet watch sends SIGTERM to dotnet run on Ctrl+C. The CLI SIGTERM handler calls Environment.Exit, which raises AppDomain.ProcessExit on one thread while the main thread disposes the ProcessReaper as the child exits. The Preview 6 refactor left HandleProcessExit calling Mutex.WaitOne on a mutex that Dispose could dispose concurrently, throwing an unhandled ObjectDisposedException (Cannot access a disposed object) that aborted the process (exit 134). Replace the thread-affine Mutex with a monitor lock plus a _shuttingDown guard flag: a monitor has no disposable wait handle to race on, and the flag makes a ProcessExit callback that starts after Dispose a no-op. Also make HandleProcessExit never throw out of the ProcessExit callback. The same refactor inverted the SIGTERM-forwarding condition: kill(pid, sig) returns nonzero on failure, but SafeProcessHandle.Signal returns true on success, so the do-not-wait early return fired on success and stopped waiting for the child and propagating its exit code. Restore the original behavior. Add Unix-only regression tests that reproduce the Dispose vs ProcessExit race. Fixes dotnet#55096
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an intermittent Unix (Linux/macOS) shutdown crash where AppDomain.ProcessExit could race ProcessReaper.Dispose() and throw ObjectDisposedException during Ctrl+C / SIGTERM-driven shutdowns (notably under dotnet watch), and restores correct SIGTERM-forwarding logic in the Unix reaper.
Changes:
- Reworked
UnixProcessReapershutdown coordination to use a monitorlockplus a_shuttingDownguard to prevent concurrent disposal races and prevent exceptions from escaping theProcessExitcallback. - Corrected Unix SIGTERM forwarding behavior by removing the inverted success/failure logic and handling
InvalidOperationException/Win32Exceptionwithin the callback. - Added Unix-only regression/stress tests covering
Dispose()vsProcessExitcallback ordering/races.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/Microsoft.DotNet.Cli.Utils.Tests/ProcessReaperTests.cs | Adds Unix-only regression + race stress tests to ensure the ProcessExit callback doesn’t throw when invoked after/during Dispose(). |
| src/Cli/Microsoft.DotNet.Cli.Utils/ProcessReaper.cs | Replaces the Mutex-based shutdown coordination with a lock + guard flag and fixes SIGTERM forwarding/waiting logic during ProcessExit. |
baronfel
reviewed
Jul 2, 2026
Addresses PR review feedback: the .NET 9+ Lock type is the modern primitive and the lock statement uses it natively. Safe here because UnixProcessReaper only compiles for the current TFM (net11.0), never net472.
On non-Windows builds the SDK cross-compiles the net472 target too, where TARGET_WINDOWS is not defined, so UnixProcessReaper compiles for net472. System.Threading.Lock is net9+, so net472 failed with CS0246. Fall back to a plain object on net472 (the Unix reaper only runs at runtime on modern .NET anyway).
net472 only ever runs on Windows, so the Unix reaper is never relevant to it. TARGET_WINDOWS reflects the build TargetOS (and gates CsWin32), not the runtime, so it is not defined when net472 is cross-compiled on non-Windows - which is why the Unix reaper was leaking into net472. Guard the Unix reaper with !TARGET_WINDOWS && NET so it compiles only for modern .NET non-Windows targets, and have Create fall back to the base reaper for the (non-shipping) net472-on-non-Windows compile. This also lets System.Threading.Lock be used unconditionally, removing the net472 object fallback.
joeloff
approved these changes
Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #55096.
Problem
On Linux/macOS, pressing Ctrl+C under
dotnet watchintermittently crashed the childdotnet runprocess with an unhandled exception (exit 134):This is a regression in Preview 6 (works in Preview 5), introduced by the
UnixProcessReaperrewrite in commitf8c229052f("Modernize Util Windows interop, enable Native AOT, refactor code").Root cause
dotnet watchsends SIGTERM todotnet runon Ctrl+C. The CLI's SIGTERM handler callsEnvironment.Exit, which raisesAppDomain.ProcessExit->HandleProcessExiton one thread, while the main thread disposes theProcessReaperas the child exits. Two bugs resulted:The crash:
HandleProcessExitcalledMutex.WaitOne()on aMutexthatDispose()could dispose concurrently. The mutex-ownership design has an inherent TOCTOU hole with no guard flag, soWaitOne()hit a disposedSafeWaitHandleand threw — unhandled, inside the ProcessExit callback, which aborts the process.Inverted signal logic:
kill(pid, SIGTERM) != 0(nonzero = failure) was replaced withSafeProcessHandle.Signal(SIGTERM)(true = success). The "couldn't send the signal, don't wait" early-return then fired on success, so the reaper stopped waiting for the child and stopped propagating its exit code.Fix
Mutexwith a monitorlockplus a_shuttingDownguard flag. A monitor has no disposable wait handle to race on, and the flag makes a ProcessExit callback that starts afterDispose()a no-op — closing the race while preserving the original intent (Dispose waits for an in-flight ProcessExit to finish propagating the exit code).HandleProcessExitnever throw out of the ProcessExit callback: forward SIGTERM only while the child is running, handleWin32Exception/InvalidOperationException, then wait and propagate the exit code — restoring Preview 5 behavior.Tests
Added
ProcessReaperTests(Unix-only, sinceUnixProcessReaperis compiled only when!TARGET_WINDOWS):ProcessExitCallbackAfterDisposeDoesNotThrow— deterministically reproduces the crash ordering (Dispose, then the ProcessExit callback). ThrowsObjectDisposedExceptionagainst the buggy code; a no-op after the fix.ProcessExitCallbackRacingDisposeDoesNotThrow— stresses the Dispose vs ProcessExit race across many iterations.Validation
TargetOS=linux,net11.0); Windows build clean (net11.0+net472); tests build and are discovered. The Unix-only tests were validated by cross-compilation and reasoning (the dev box is Windows); they run on CI.Behavior change
On Unix Ctrl+C/SIGTERM,
dotnet runagain forwards SIGTERM to the child, waits for it, and propagates its exit code — restoring Preview 5 behavior that the inverted logic had removed.