Open
Conversation
added 2 commits March 24, 2026 17:51
Contributor
| Tagging subscribers to this area: @agocke, @VSadov |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Monitor.Wait reentrancy issue where nested waits on the same thread could share a single thread-static waiter/event, allowing one wait to consume (“steal”) another wait’s pulse, particularly when SynchronizationContext.Wait pumps messages.
Changes:
- Adjust
Condition.Waiterthread-static reuse to clear the cached waiter while a wait is in progress, ensuring overlapping waits use distinctAutoResetEventinstances. - Add a regression test that reproduces the message-pumping reentrant wait scenario via a custom
SynchronizationContext.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Threading/Condition.cs | Updates waiter caching logic so overlapping waits don’t share the same thread-static waiter/event. |
| src/libraries/System.Threading/tests/MonitorTests.cs | Adds a regression test validating reentrant waits don’t steal each other’s pulse signals. |
src/libraries/System.Private.CoreLib/src/System/Threading/Condition.cs Outdated Show resolved Hide resolved
…ition.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
jkotas approved these changes Mar 25, 2026
jkoritzinsky approved these changes Mar 25, 2026
VSadov reviewed Mar 25, 2026
src/libraries/System.Private.CoreLib/src/System/Threading/Condition.cs Outdated Show resolved Hide resolved
This was referenced Mar 25, 2026
Open
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 #123173.
There's currently a problem with Monitor using a single thread-static
Waiter(andAutoResetEvent) per thread. When using synchronization contexts that allow message pumping the following scenario can happen:Monitor.Waiton lock A.SynchronizationContext.Waitpumps a message and the main thread callsMonitor.Waiton lock B.Monitor.Pulseon lock A but lock B steals the signal since they share the same waiter.This change nulls out the thread-static
Waiterwhile a wait is in progress, so any reentrantMonitor.Waitgets its ownWaiterwith a distinctAutoResetEvent. TheWaiteris restored after the wait completes.