Skip to content

[Bug] Broker runs out of heap when Key_Shared consumers reconnect after their connections vanish silently #26862

Closed

Description

@lhotari

Search before reporting

  • I searched in the issues and found nothing similar.

Read release policy

  • I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.

User environment

Issue Description

When the consumers' connections disappear without the broker knowing, as when a NAT gateway or a firewall in front of the consumers forgets its connections without resetting them, the consumers reconnect at once on new connections, while the broker keeps the old consumers registered until its keep-alive finds the old connections dead, 30–60 s later. For Key_Shared subscriptions, the broker then holds the selector state of both the old and the new consumers, and that state is large: in this run, it made the broker run out of heap.

The scenario: iot-telemetry-small, 1,000 msg/s, 30 topics, 20 Key_Shared subscriptions per topic, each consumed by 10 pods: 6,000 consumers in 200 clients, on one broker with a 512 MB heap. 30 s into the measurement, the consumers' 371 TCP connections are silently disconnected: their packets are dropped both ways, and the sockets on the consumers' side are destroyed, so the consumers reconnect while their resets never reach the broker.

About 15 s later, in two runs out of two:

java.lang.OutOfMemoryError: Java heap space
Terminating due to java.lang.OutOfMemoryError: Java heap space

The broker restarted, every client reconnected, and the publish latency reached 20–34 s; every message was still delivered, in order.

The heap dump at the OutOfMemoryError (571 MB):

Count Retained
org.apache.pulsar.broker.service.Consumer 11,971 100 MB
PersistentStickyKeyDispatcherMultipleConsumers 600 297 MB
ConsistentHashingStickyKeyConsumerSelector 600 282 MB
ConsumerHashAssignmentsSnapshot 600 105 MB
ConsistentHashingStickyKeyConsumerSelector$HashRingPointEntry 1,177,680 49 MB
TreeMap$Entry 1,179,713
HashRangeAssignment / Range 1,121,310 / 1,121,910 45 MB / 36 MB
ChannelOutboundBuffer (the connections' unsent writes) 856 37 MB

(Retained sizes are the dump tool's approximations; the dispatchers' and the selectors' include each other.)

  • The broker has 11,971 Consumer objects for the workload's 6,000 consumers: the old ones on the half-open connections, and the new ones.
  • Each Key_Shared consumer costs about 25 KB of selector state: 100 hash ring points (keySharedConsistentHashingReplicaPoints), each a TreeMap entry with a boxed Integer key and a HashRingPointEntry, and the subscription's assignments snapshot, a Range and a HashRangeAssignment per ring point.
  • The connections' unsent writes are small in comparison.

So the broker's heap needs room for twice the Key_Shared consumers whenever clients reconnect before the broker has dropped their old connections, which a NAT gateway or a firewall dropping connections silently makes last for the broker's keep-alive.

Expected: a silent loss of the clients' connections doesn't make the broker run out of memory with a workload that fits it otherwise, and the Key_Shared selector's state per consumer is small enough that a broker can hold its consumers twice.

Error messages

java.lang.OutOfMemoryError: Java heap space
Terminating due to java.lang.OutOfMemoryError: Java heap space

Reproducing the issue

With the network conditions of the Pulsar Performance Testing Framework in #26859:

./gradlew :tests:performance:launcher:run -Pperformance.keepLauncherLog \
  --args='--scenario tests/performance/scenarios/iot-telemetry-small.yaml --extends network/silent-client-disconnect --extends configs/heap-dumps-broker'

The overlay silently disconnects the gateways' connections 30 s into the measurement and the applications' at 100 s; the applications' disconnection is the one that exhausts the heap. configs/heap-dumps-broker writes a heap dump at the OutOfMemoryError, besides one at the highest heap usage, into the run's heap-dumps/broker directory.

Additional information

Possible directions:

  • A smaller selector state per consumer: primitive maps for the hash ring instead of a TreeMap<Integer, HashRingPointEntry>, and assignment snapshots that don't hold an object per ring point.
  • When a consumer with the same name and the same client identity subscribes again while its old consumer is on a connection that the broker hasn't heard from, check the old connection (a ping with a short timeout) before keeping both.
  • A shorter broker keep-alive for connections with Key_Shared consumers, or a documented heap sizing for Key_Shared that includes reconnections.

Slow consumers run the same broker out of heap too. network/constrained-consumers limits the broker's traffic to the consumers to 5 Mbit/s, about half of their traffic, for 60 s. On master, the 512 MB broker ran out of heap 14 s after the limit ended, in 2 runs out of 2: the messages dispatched to the slow consumers and not yet acknowledged, 105,000–243,000 of them, on top of the Key_Shared selectors' state. The restart then stalled every producer for 20–40 s. With a 4 GB broker, it didn't run out of heap.

A prototype of the first direction, which keeps the consistent hashing ring in arrays, about 1.2 KB per Key_Shared consumer instead of about 25 KB, let the 512 MB broker survive network/silent-client-disconnect in 3 runs out of 3, with a heap dump of 306 MB at the highest heap usage, and network/constrained-consumers in 2 runs out of 2.

Related: the same selector state is rebuilt on every membership change, so when many Key_Shared consumers reconnect at once, the work also stalls unrelated producers on the broker's I/O threads.

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. self-assigned this
    on Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions