Skip to content

fix: keep lazy dispatch state on VarHandle#3116

Merged
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:fix/lazy-dispatch-state-field-retention
Jun 21, 2026
Merged

fix: keep lazy dispatch state on VarHandle#3116
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:fix/lazy-dispatch-state-field-retention

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 20, 2026

Copy link
Copy Markdown
Member

Motivation

PR #3035 introduced a VarHandle-backed @volatile var state for 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 Int state, but use explicit getVolatile / setVolatile accessors with compareAndSet instead of plain VarHandle get / set.

This avoids per-instance AtomicInteger allocation 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-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 #3035

@He-Pin He-Pin requested a review from pjfanning June 20, 2026 21:19
@He-Pin He-Pin force-pushed the fix/lazy-dispatch-state-field-retention branch from 775e67e to e223ae4 Compare June 20, 2026 21:30
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
@He-Pin He-Pin force-pushed the fix/lazy-dispatch-state-field-retention branch from e223ae4 to f9abea4 Compare June 20, 2026 21:38
@He-Pin He-Pin changed the title fix: simplify lazy stage actor dispatch state fix: keep lazy dispatch state on VarHandle Jun 20, 2026

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@He-Pin He-Pin merged commit d75bf95 into apache:main Jun 21, 2026
9 checks passed
@He-Pin He-Pin deleted the fix/lazy-dispatch-state-field-retention branch June 21, 2026 03:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants