Avoid reordering when reading continuation state in AsyncOperation.SignalCompletion#126373
Merged
VSadov merged 1 commit intodotnet:mainfrom Apr 1, 2026
Merged
Avoid reordering when reading continuation state in AsyncOperation.SignalCompletion#126373VSadov merged 1 commit intodotnet:mainfrom
VSadov merged 1 commit intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @agocke, @VSadov |
Member
Author
|
Also fixed the same issue in Not sure if that code runs, but it uses the same pattern. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a missing acquire/read barrier in continuation-completion signaling to prevent theoretical (and observed) reordering issues when reading continuation dependencies, addressing sporadic System.Threading.Channels test failures.
Changes:
- Add
Volatile.Readof_continuationinAsyncOperation.SignalCompletionto ensure continuation dependencies are observed in the correct order before invocation. - Add a defensive
Debug.Assertto document/enforce the expectation that_continuationis set when invoking it. - Apply the same
Volatile.Readpattern to theMicrosoft.Bcl.AsyncInterfacescopy ofManualResetValueTaskSourceCorefor consistency with the established fix pattern.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Threading.Channels/src/System/Threading/Channels/AsyncOperation.cs | Adds an acquire read on _continuation before consuming _capturedContext / _continuationState, preventing reordering in SignalCompletion. |
| src/libraries/Microsoft.Bcl.AsyncInterfaces/src/System/Threading/Tasks/Sources/ManualResetValueTaskSourceCore.cs | Mirrors the acquire read pattern in SignalCompletion to avoid similar continuation-state reordering in the netstandard-compatible copy. |
stephentoub
approved these changes
Mar 31, 2026
Member
Author
|
Thanks!! |
mangod9
approved these changes
Mar 31, 2026
This was referenced Apr 1, 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: #125015
Missing Read barrier.
The fix is roughly the same as in #72657, but in
AsyncOperation