[Backport v1.5] fix(streams): prevent race condition in ChannelSource on channel completion#7951
Merged
Aaronontheweb merged 1 commit intoakkadotnet:v1.5from Nov 25, 2025
Conversation
36c9f1b to
008e451
Compare
…letion (akkadotnet#7941) * fix(streams): prevent race condition in ChannelSource on channel completion (akkadotnet#7940) Fixed a race condition in ChannelSourceLogic that caused intermittent NullReferenceException when completing a ChannelWriter while the stream was waiting for data. The issue occurred because two async callbacks could fire simultaneously when the channel writer completed: 1. The _reader.Completion continuation → OnReaderComplete → CompleteStage 2. The WaitToReadAsync continuation → OnValueRead(false) → CompleteStage Both paths could pass the IsStageCompleted check before either completed the stage, leading to concurrent access of stage internals. The fix adds an atomic flag (_completing) using Interlocked.Exchange to ensure only one completion path ever executes. This is applied to: - OnReaderComplete - channel completion callback - OnValueRead - when data is not available - OnValueReadFailure - when read fails - OnPull - synchronous completion path * refactor: use CompareExchange instead of Exchange for atomic flag CompareExchange is more semantically correct - it only sets the value if it's currently 0, rather than unconditionally setting it.
008e451 to
3720c05
Compare
This was referenced Nov 26, 2025
This was referenced Dec 11, 2025
This was referenced Jan 7, 2026
This was referenced Jan 12, 2026
This was referenced Feb 20, 2026
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.
Summary
Backport of #7941 to v1.5 branch.
Original PR: #7941