fix(streams): prevent race condition in ChannelSource on channel completion#7941
Merged
Aaronontheweb merged 5 commits intoNov 25, 2025
Merged
Conversation
…letion (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
CompareExchange is more semantically correct - it only sets the value if it's currently 0, rather than unconditionally setting it.
Aaronontheweb
commented
Nov 24, 2025
Aaronontheweb
left a comment
Member
Author
There was a problem hiding this comment.
Key to this PR is using Interlocked.CompareExchange to prevent one of two competing sources of shutdown signaling from overriding each other:
- Internal Akka.Streams stage completion
- External
ChannelReadercompletion
This change makes that thread-safe by using an internal shutdown flag and Interlocked to read + set it.
Aaronontheweb
added a commit
to Aaronontheweb/akka.net
that referenced
this pull request
Nov 25, 2025
…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.
Aaronontheweb
added a commit
to Aaronontheweb/akka.net
that referenced
this pull request
Nov 25, 2025
…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.
Aaronontheweb
added a commit
to Aaronontheweb/akka.net
that referenced
this pull request
Nov 25, 2025
…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.
Aaronontheweb
added a commit
that referenced
this pull request
Nov 25, 2025
…letion (#7941) (#7951) * fix(streams): prevent race condition in ChannelSource on channel completion (#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.
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
Source.ChannelReaderNullReferenceExceptionupon dispose #7940 -NullReferenceExceptionwhen completing aChannelWriterwhile the stream is waiting for dataOnReaderCompleteandOnValueReadcallbacksRoot Cause
When
channel.Writer.Complete()is called while the stream is blocked onWaitToReadAsync(), two async callbacks can fire simultaneously:_reader.Completioncontinuation →OnReaderComplete(null)→CompleteStage()WaitToReadAsync()continuation →OnValueRead(false)→CompleteStage()Both paths could pass the
IsStageCompletedcheck before either completed the stage, leading to concurrent access of stage internals and an NRE.Fix
Added an atomic flag (
_completing) usingInterlocked.Exchangeto ensure only one completion path ever executes. Both paths ultimately callCompleteStage(), so it doesn't matter which one wins - we just need to ensure only one runs.Test plan
ChannelSource_should_not_throw_NRE_when_completing_channel_while_waiting_for_dataChannelSourceSpectests pass