Skip to content

Avoid reordering when reading continuation state in AsyncOperation.SignalCompletion#126373

Merged
VSadov merged 1 commit intodotnet:mainfrom
VSadov:fix125015
Apr 1, 2026
Merged

Avoid reordering when reading continuation state in AsyncOperation.SignalCompletion#126373
VSadov merged 1 commit intodotnet:mainfrom
VSadov:fix125015

Conversation

@VSadov
Copy link
Copy Markdown
Member

@VSadov VSadov commented Mar 31, 2026

Fixes: #125015

Missing Read barrier.
The fix is roughly the same as in #72657, but in AsyncOperation

Copilot AI review requested due to automatic review settings March 31, 2026 19:37
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

@VSadov
Copy link
Copy Markdown
Member Author

VSadov commented Mar 31, 2026

Also fixed the same issue in Microsoft.Bcl.AsyncInterfaces/src/System/Threading/Tasks/Sources/ManualResetValueTaskSourceCore.cs

Not sure if that code runs, but it uses the same pattern.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Read of _continuation in AsyncOperation.SignalCompletion to ensure continuation dependencies are observed in the correct order before invocation.
  • Add a defensive Debug.Assert to document/enforce the expectation that _continuation is set when invoking it.
  • Apply the same Volatile.Read pattern to the Microsoft.Bcl.AsyncInterfaces copy of ManualResetValueTaskSourceCore for 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.

@VSadov VSadov requested review from jakobbotsch and mangod9 March 31, 2026 19:43
@VSadov
Copy link
Copy Markdown
Member Author

VSadov commented Mar 31, 2026

Thanks!!

@VSadov VSadov enabled auto-merge (squash) March 31, 2026 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure: Assertion failed. state is RuntimeAsyncTask<T>

4 participants