fix: keep lazy dispatch state on VarHandle#3116
Merged
He-Pin merged 1 commit intoJun 21, 2026
Merged
Conversation
775e67e to
e223ae4
Compare
Motivation: PR apache#3035 used a VarHandle-backed volatile Int for LazyDispatch election state. Review raised concern about the field and the VarHandle access semantics. Modification: Keep the VarHandle-backed Int state, but use explicit getVolatile/setVolatile accessors with compareAndSet. This keeps the state in the LazyDispatch object without per-instance AtomicInteger allocation while preserving conservative volatile access semantics. Result: LazyDispatch keeps the same IDLE/SCHEDULED scheduling protocol with explicit volatile state access. Tests: - scalafmt --mode diff-ref=origin/main --non-interactive - scalafmt --list --mode diff-ref=origin/main --non-interactive - sbt "+stream / compile" - sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.StageActorRefSpec" (11/11) - git diff --check References: Refs apache#3035
e223ae4 to
f9abea4
Compare
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.
Motivation
PR #3035 introduced a VarHandle-backed
@volatile var statefor LazyDispatch scheduling state. The review discussion raised concern around the field and the access mode used through the VarHandle.Modification
Keep the static VarHandle and instance
Intstate, but use explicitgetVolatile/setVolatileaccessors withcompareAndSetinstead of plain VarHandleget/set.This avoids per-instance
AtomicIntegerallocation while making the state access mode explicit and conservative.Result
LazyDispatch keeps the same IDLE/SCHEDULED scheduling protocol and uses volatile state access for the scheduling election and IDLE publish/recheck path.
Tests
scalafmt --mode diff-ref=origin/main --non-interactivescalafmt --list --mode diff-ref=origin/main --non-interactivesbt "+stream / compile"sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.StageActorRefSpec"(11/11)git diff --checkReferences
Refs #3035