fix(stream): notify BroadcastHub consumers on materializer shutdown#3346
Open
He-Pin wants to merge 3 commits into
Open
fix(stream): notify BroadcastHub consumers on materializer shutdown#3346He-Pin wants to merge 3 commits into
He-Pin wants to merge 3 commits into
Conversation
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
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
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
When the materializer running a
BroadcastHub.sinkis 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 — neitherCompletednorErroris received. See #3345.Modification
consumerWheel) notification fromonUpstreamFailuretopostStopso thatpostStopis the single notification point for all consumers.postStopnow handles bothOpen(normal termination) andClosed(after upstream failure) states, ensuring registered consumers in the wheel always receive the appropriate signal.Before:
onUpstreamFailureiterated theconsumerWheelto notify registered consumers, butpostStopdid not. This left a gap where late-registering consumers could miss notification during materializer shutdown.After:
postStopis the sole notification point for registered consumers, using the failure reason from theClosedstate 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"notify consumers when hub materializer is shut down"References
Fixes #3345