Repository navigation
Synchronize Process.Exited subscription updates - #136887
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process |
Concurrent Exited additions and removals can overwrite delegate updates, leaving WaitForExitAsync tasks pending after their process exits. Use a field-like backing event for thread-safe subscription updates. Add regression coverage for concurrent additions, removals, and async waits.
518d51d to
1c89cd9
Compare
|
/cc @SamMonoRT as area owner for triage and review. If there's someone else to consult, please let me know. Thanks! |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the change to make Process.Exited subscription updates atomic.
No open issues 🎉
What changed in this PR
This PR makes Process.Exited subscription updates atomic to prevent lost handlers during overlapping exit waits. It preserves the public API and existing event dispatch behavior without promising general Process thread safety.
Changes:
- Replaces the private delegate field with an event that provides compiler-generated thread-safe add/remove accessors.
- Adds callback-count tests for concurrent subscription additions and removals.
- Adds exit-wait tests covering one waiter and four concurrent waiters.
| File | Description |
|---|---|
src/libraries/System.Diagnostics.Process/tests/ProcessWaitingTests.cs |
Checks that independent async waits complete after process exit. |
src/libraries/System.Diagnostics.Process/tests/ProcessTests.cs |
Checks callback counts after concurrent handler additions and removals. |
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs |
Converts _onExited to a private event for atomic subscription updates. |
Review effort: Balanced
adamsitnik
left a comment
There was a problem hiding this comment.
@MattKotsenas thank you for your contribution! the fix LGTM, but PTAL at my comment and check if we can simplify the code even further. Thanks!
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM, thank you @MattKotsenas !
|
/ba-g the networking failure is unrelated |
@MattKotsenas do you need to get this backported to .NET 10 or 11? |
This PR synchronizes
Exitedsubscription updates to prevent lost handlers during overlapping exit waits.Exitedupdates a delegate field without synchronization, so overlapping additions and removals can overwrite one another. Replace that field with an event so the compiler generates thread-safe add/remove accessors, as it already does forOutputDataReceivedandErrorDataReceived. Public signatures and theOnExited/SynchronizingObjectdispatch path are unchanged.Motivating case and scope
I encountered this in an Aspire process runner that owns one
Process, captures its streams, and waits for exit. Cancellation can send several independent operations into cleanup, where each waits for the process to terminate. Reduced to the relevant control flow, with the process already started and its output redirected:The cleanup continuations can overlap, producing independent
WaitForExitAsynccalls on the sameProcess. I observed a cleanup wait remain pending after the process had exited and raisedExited, with that wait's event handler missing from the subscription list.Making subscription updates atomic prevents these independent cleanup waits from losing handlers. This change does not make
Processinstances generally thread-safe.Validation
The regression tests check callback counts after concurrent additions and removals, and require independent async waits to complete after process exit. They encourage contention but cannot guarantee the failing interleaving.
Each case ran 100 times against the unfixed and fixed libraries on Windows and Linux under WSL2. Each platform comparison used the same test assembly, without retries.
Observed failures out of 100 runs:
Subscription failures hit the final callback-count assertions. Async-wait failures hit the existing 30-second post-exit completion timeout.