Skip to content

try to fix intermittent test failure in HubSpec#2943

Open
pjfanning wants to merge 7 commits intoapache:mainfrom
pjfanning:copilot/fix-intermittent-test-failure
Open

try to fix intermittent test failure in HubSpec#2943
pjfanning wants to merge 7 commits intoapache:mainfrom
pjfanning:copilot/fix-intermittent-test-failure

Conversation

@pjfanning
Copy link
Copy Markdown
Member

@pjfanning pjfanning commented May 7, 2026

relates to #2942

HubSpec intermittently failed because the original assertion relied on a timing-sensitive consumer handoff. Depending on scheduling, the second consumer could attach before the first consumer’s shutdown had been fully observed, leading to overlapping ranges in the test.

Root cause of flakiness: the old test called cancel() on consumer 1 from inside registrationPendingCallback, which fires while RegistrationPending is being processed. Because cancel() is asynchronous in Pekko Streams, the resulting UnRegister arrives at the hub after RegistrationPending has already read a stale head, so consumer 2 started from the wrong offset.

Fix: let consumer 1 complete naturally via take(10).runWith(Sink.seq).futureValue. The ordering guarantee comes directly from GraphStageLogic.internalCompleteStage, which cancels inlets before completing outlets — so the hub's UnRegister is enqueued in the hub actor's mailbox before Sink.seq's postStop resolves the future. Consumer 2's RegistrationPending is only sent after futureValue returns, ensuring the hub always processes UnRegister (advancing head) first. No synchronized, no ReentrantLock, no production changes.

Copilot AI and others added 7 commits May 7, 2026 10:33
Agent-Logs-Url: https://github.com/pjfanning/incubator-pekko/sessions/3b8db3b8-fb93-4089-a882-327922405f8f

Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
Agent-Logs-Url: https://github.com/pjfanning/incubator-pekko/sessions/25fead82-6b9d-4995-9cdd-83a627a9c572

Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
Agent-Logs-Url: https://github.com/pjfanning/incubator-pekko/sessions/25fead82-6b9d-4995-9cdd-83a627a9c572

Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
The test 'ensure that subsequent consumers see subsequent elements without
gap' was flaky because it cancelled the first consumer via a callback that
fires INSIDE RegistrationPending processing. The cancel is asynchronous,
so UnRegister arrives at the hub AFTER RegistrationPending has already
read a stale head value, causing consumer 2 to start from the wrong offset.

Rewrite the test to let consumer 1 complete naturally via take(10). Pekko's
GraphStageLogic.internalCompleteStage processes inlets (cancel) before
outlets (complete), so the hub's UnRegister is enqueued in the hub actor's
mailbox before Sink.seq's postStop resolves the future. Consumer 2's
RegistrationPending is only sent after futureValue returns, guaranteeing
the hub processes UnRegister → head advanced before RegistrationPending.

Remove now-unused imports: AtomicInteger, AtomicReference, @nowarn.
No production code changes.

Agent-Logs-Url: https://github.com/pjfanning/incubator-pekko/sessions/0ed4c2ab-7f82-4e08-85af-f4b061725c67

Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
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