Skip to content

fix(stream): notify PartitionHub consumers on materializer shutdown#3347

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

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

Conversation

@He-Pin

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

Copy link
Copy Markdown
Member

Motivation

When the materializer running a PartitionHub.sink is shut down (e.g. the hosting actor stops or crashes), consumers materialized on a different materializer may not be notified of the hub's termination. This mirrors the same bug pattern found in BroadcastHub (see #3345).

Modification

  • Move registered-consumer 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 always receive the appropriate signal.

Before: onUpstreamFailure notified registered consumers directly, but postStop did not. This left a gap where 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

PartitionHub 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" (PartitionHub section)

References

Refs #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 3 commits July 14, 2026 19:19
Motivation:
When the materializer running a PartitionHub.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. This mirrors the same bug
in BroadcastHub (see #3345).

Modification:
Move registered-consumer 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:
PartitionHub 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:
Refs #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:
Refs #3345
Motivation:
The Closed(_) branch comment in postStop said "fall through to notify
registered below" but the code directly calls notifyRegisteredConsumers()
rather than falling through, making the comment misleading.

Modification:
Reword the comment to accurately describe the direct call.

Result:
Comment now matches the actual control flow in postStop.

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

References:
Refs #3345
@He-Pin He-Pin force-pushed the fix/partition-hub-poststop branch from 8ed3677 to 9ad7de6 Compare July 14, 2026 11:30
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.

1 participant