Fix WithinAsync timeout not propagating to EventFilter across async boundaries#7977
Merged
Aaronontheweb merged 6 commits intoDec 29, 2025
Conversation
…oundaries Use AsyncLocal<TimeSpan?> to properly propagate WithinAsync timeout to EventFilter and other async operations. The previous implementation used a plain instance field (_testState.End) which could have memory visibility issues when accessed from different threads across await boundaries. The fix: - Adds AsyncLocal<TimeSpan?> field that flows through async contexts - WithinAsync sets/restores both instance field and AsyncLocal - Remaining and RemainingOr check AsyncLocal first, then instance field - Preserves backward compatibility with sync code paths
d8cbbd6 to
8784771
Compare
Arkatufus
suggested changes
Dec 29, 2025
Arkatufus
added a commit
to Arkatufus/akka.net
that referenced
this pull request
Jan 7, 2026
…oundaries (akkadotnet#7977) * Fix WithinAsync timeout not propagating to EventFilter across async boundaries Use AsyncLocal<TimeSpan?> to properly propagate WithinAsync timeout to EventFilter and other async operations. The previous implementation used a plain instance field (_testState.End) which could have memory visibility issues when accessed from different threads across await boundaries. The fix: - Adds AsyncLocal<TimeSpan?> field that flows through async contexts - WithinAsync sets/restores both instance field and AsyncLocal - Remaining and RemainingOr check AsyncLocal first, then instance field - Preserves backward compatibility with sync code paths * remove `static` --------- Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
Aaronontheweb
added a commit
that referenced
this pull request
Jan 8, 2026
…oundaries (#7977) * Fix WithinAsync timeout not propagating to EventFilter across async boundaries Use AsyncLocal<TimeSpan?> to properly propagate WithinAsync timeout to EventFilter and other async operations. The previous implementation used a plain instance field (_testState.End) which could have memory visibility issues when accessed from different threads across await boundaries. The fix: - Adds AsyncLocal<TimeSpan?> field that flows through async contexts - WithinAsync sets/restores both instance field and AsyncLocal - Remaining and RemainingOr check AsyncLocal first, then instance field - Preserves backward compatibility with sync code paths * remove `static` --------- Co-authored-by: Gregorius Soedharmo <arkatufus@yahoo.com>
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
WithinAsynctimeout was not reliably propagating toEventFilterand other async operationsAsyncLocal<TimeSpan?>to properly flow timeout across async boundariesRoot Cause
The
_testState.Endfield set byWithinAsyncis a plain instance property without volatile semantics. WhenEventFilterreads this value throughRemainingOrDefaulton a different thread (via async continuation), it could see a stale cache value due to CPU cache coherency delays. This causedEventFilterto occasionally fall back to the default 3000ms timeout instead of the timeout set byWithinAsync.Fix
AsyncLocal<TimeSpan?>field that automatically flows through async contexts viaExecutionContextWithinAsyncnow sets/restores both the instance field (for sync code paths) and the AsyncLocal (for async code paths)Remainingproperty andRemainingOrmethod check AsyncLocal first, then fall back to instance fieldTest plan
WithinAsync_timeout_should_propagate_to_EventFilterpasses consistently (10/10 runs)Akka.TestKit.Testssuite passes (293 passed, 1 skipped, 0 failed)