Fix WriteOnceBlock race condition where ReceiveAsync returns null#123435
Open
Fix WriteOnceBlock race condition where ReceiveAsync returns null#123435
Conversation
…null Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix WriteOnceBlock<T> method to correctly receive posted values
Fix WriteOnceBlock race condition where ReceiveAsync returns null
Jan 21, 2026
stephentoub
reviewed
Jan 21, 2026
src/libraries/System.Threading.Tasks.Dataflow/src/Blocks/WriteOnceBlock.cs
Show resolved
Hide resolved
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
stephentoub
approved these changes
Jan 21, 2026
src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/WriteOnceBlockTests.cs
Show resolved
Hide resolved
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-threading-tasks |
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical race condition in WriteOnceBlock<T> where ReceiveAsync could intermittently return null instead of the posted value when called concurrently with Post. The root cause was that _header was being set before _value, allowing readers to observe HasValue == true while _value was still default.
Changes:
- Reordered field assignments in
OfferMessageto set_valuebefore_header - Added
Interlocked.MemoryBarrier()between the writes to enforce ordering and prevent CPU/compiler reordering - Added comprehensive regression test with 10,000 iterations to verify the fix
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| WriteOnceBlock.cs | Fixed race condition by swapping assignment order and adding memory barrier to ensure _value is visible before _header.IsValid becomes true |
| WriteOnceBlockTests.cs | Added regression test that reproduces the concurrent Post/ReceiveAsync race condition scenario |
3 tasks
stephentoub
approved these changes
Jan 22, 2026
Member
|
/azp list |
Member
|
/azp run runtime-libraries-coreclr outerloop-windows |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Member
|
/azp run runtime-libraries-coreclr outerloop-windows |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This was referenced Jan 29, 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.
Description
WriteOnceBlock<T>.ReceiveAsyncintermittently returnsnullwhen called concurrently withPost.Root cause: In
OfferMessage,_headerwas set before_value:LinkTochecksHasValue(_header.IsValid) inside a lock but reads_valueoutside it. A concurrent reader can observeHasValue == truewhile_valueis stilldefault(T).Fix: Assign
_valuebefore_header, with a memory barrier to enforce the ordering and prevent CPU/compiler reordering.Changes
_valueis set before_header.IsValidbecomes true, and addInterlocked.MemoryBarrier()between the writes to enforce the orderingTestConcurrentPostAndReceiveAsyncregression test (10K iterations), marked as[OuterLoop]since it's a stress testOriginal prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.