Skip to content

fix(stream): notify BroadcastHub consumers on materializer shutdown#3346

Open
He-Pin wants to merge 3 commits into
mainfrom
fix/broadcast-hub-poststop
Open

fix(stream): notify BroadcastHub consumers on materializer shutdown#3346
He-Pin wants to merge 3 commits into
mainfrom
fix/broadcast-hub-poststop

Conversation

@He-Pin

@He-Pin He-Pin commented Jul 14, 2026

Copy link
Copy Markdown
Member

Motivation

When the materializer running a BroadcastHub.sink is shut down (e.g. the hosting actor stops or crashes), consumers materialized on a different materializer are not reliably notified of the hub's termination. The consumer source hangs indefinitely — neither Completed nor Error is received. See #3345.

Modification

  • Move registered-consumer (consumerWheel) notification from onUpstreamFailure to postStop so that postStop is the single notification point for all consumers.
  • postStop now handles both Open (normal termination) and Closed (after upstream failure) states, ensuring registered consumers in the wheel always receive the appropriate signal.

Before: onUpstreamFailure iterated the consumerWheel to notify registered consumers, but postStop did not. This left a gap where late-registering consumers could miss notification during materializer shutdown.

After: postStop is the sole notification point for registered consumers, using the failure reason from the Closed state when applicable.

Result

BroadcastHub consumers are reliably notified when the hub's materializer shuts down, preventing consumers from hanging indefinitely.

Tests

  • sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.HubSpec" — 51 passed, 0 failed
  • Added new test: "notify consumers when hub materializer is shut down"

References

Fixes #3345

Motivation:
When the materializer running a BroadcastHub.sink is shut down (e.g. the
hosting actor stops), consumers materialized on a different materializer
may not be notified of the hub's termination (see #3345).

Modification:
Move registered-consumer (consumerWheel) notification from
onUpstreamFailure to postStop so postStop is the single notification
point. postStop now handles both Open (normal termination) and Closed
(after upstream failure) states, ensuring registered consumers always
receive the appropriate signal.

Result:
BroadcastHub consumers are reliably notified when the hub's materializer
shuts down, preventing consumers from hanging indefinitely.

Tests:
- sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.HubSpec"
  51 passed, 0 failed

References:
Fixes #3345
@He-Pin He-Pin added this to the 2.0.0-M4 milestone Jul 14, 2026
@He-Pin He-Pin added the t:stream Pekko Streams label Jul 14, 2026
He-Pin added 2 commits July 14, 2026 19:15
Motivation:
If one consumer's callback throws during hub shutdown, the remaining
consumers must still be notified.

Modification:
Wrap each callback.invoke in onUpstreamFailure and postStop with
try/catch NonFatal so a single consumer failure does not short-circuit
the notification loop.

Result:
All reachable consumers are notified even when one consumer's callback
throws.

Tests:
- sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.HubSpec"
  51 passed, 0 failed

References:
Fixes #3345
…determinism

Motivation:
The postStop method's Closed case comment incorrectly said "fall through"
but Scala match cases never fall through. The test used Thread.sleep which
violates the project's determinism guidelines.

Modification:
- Fix misleading comment in postStop's Closed case to say "notify
  registered consumers directly" instead of "fall through to notify
  wheel below"
- Replace Thread.sleep(100) in the materializer shutdown test with
  upstream.expectRequest() for deterministic demand propagation

Result:
Comment accurately describes the code behavior. Test no longer relies
on Thread.sleep for setup timing.

Tests:
- sbt "stream-tests / Test / testOnly org.apache.pekko.stream.scaladsl.HubSpec" — 51/51 passed

References:
Fixes #3345

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

t:stream Pekko Streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BroadcastHub: published source not completed when materializing actor dies/stops

2 participants