-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix StreamOfStreams with atomic CompareExchange
#7796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Aaronontheweb
merged 11 commits into
akkadotnet:dev
from
Aaronontheweb:fix/racy-flowprefixandtail-spec
Sep 9, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0d0fd32
Fix race condition in FlowPrefixAndTailSpec by ensuring atomic detect…
Aaronontheweb db9a855
Add CompareExchange method to AtomicReference and fix race condition …
Aaronontheweb eeb2d3b
added API approvals
Aaronontheweb cac344e
fix `StreamOfStreams` handling
Aaronontheweb d8ba57d
fixed bug
Aaronontheweb 1ce1ef4
make `FlowIdleInjectSpec` deterministic
Aaronontheweb 91ce48f
Merge branch 'dev' into fix/racy-flowprefixandtail-spec
Aaronontheweb 80d67ae
Merge branch 'dev' into fix/racy-flowprefixandtail-spec
Aaronontheweb be8086f
Merge branch 'dev' into fix/racy-flowprefixandtail-spec
Aaronontheweb 5f24080
Merge branch 'dev' into fix/racy-flowprefixandtail-spec
Arkatufus 7fbcdaa
Merge branch 'dev' into fix/racy-flowprefixandtail-spec
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1242,31 +1242,44 @@ public Logic(SubSource<T> stage) : base(stage.Shape) | |
|
|
||
| private void SetCallback(Action<IActorSubscriberMessage> callback) | ||
| { | ||
| var status = _stage._status.Value; | ||
|
|
||
| if (status == null) | ||
| // Single atomic operation that both attempts the change AND returns the previous value | ||
| var previous = _stage._status.CompareExchange(null, callback); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the real fix - only read the value once, as the result of the |
||
|
|
||
| switch (previous) | ||
| { | ||
| if (!_stage._status.CompareAndSet(null, callback)) | ||
| SetCallback(callback); | ||
| case null: | ||
| return; // Success - we set the callback, previous was null | ||
| // CompareExchange failed - handle the different states based on what was actually there | ||
| case OnComplete: | ||
| CompleteStage(); | ||
| break; | ||
| case OnError error: | ||
| FailStage(error.Cause); | ||
| break; | ||
| case Action<IActorSubscriberMessage>: | ||
| throw new IllegalStateException("Substream Source cannot be materialized more than once"); | ||
| default: | ||
| // Unexpected state - should not happen but be safe | ||
| throw new IllegalStateException($"Substream Source cannot be materialized more than once - found [{previous}]"); | ||
| } | ||
| else if (status is OnComplete) | ||
| CompleteStage(); | ||
| else if (status is OnError error) | ||
| FailStage(error.Cause); | ||
| else if (status is Action<IActorSubscriberMessage>) | ||
| throw new IllegalStateException("Substream Source cannot be materialized more than once"); | ||
| } | ||
|
|
||
| public override void PreStart() | ||
| { | ||
| var ourOwnCallback = GetAsyncCallback<IActorSubscriberMessage>(msg => | ||
| { | ||
| if (msg is OnComplete) | ||
| CompleteStage(); | ||
| else if (msg is OnError error) | ||
| FailStage(error.Cause); | ||
| else if (msg is OnNext next) | ||
| Push(_stage._out, (T) next.Element); | ||
| switch (msg) | ||
| { | ||
| case OnComplete: | ||
| CompleteStage(); | ||
| break; | ||
| case OnError error: | ||
| FailStage(error.Cause); | ||
| break; | ||
| case OnNext next: | ||
| Push(_stage._out, (T) next.Element); | ||
| break; | ||
| } | ||
| }); | ||
| SetCallback(ourOwnCallback); | ||
| } | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the ordering deterministics