Repository navigation
Description
Search before reporting
- I searched in the issues and found no existing report of these missing-close paths.
Read release policy
- I have read the release policy and understand that unsupported versions do not receive bug fixes.
User environment
- Pulsar: source build of
master(5.1.0-SNAPSHOT), affected code at1046481c970067cbca9c80a25a98257b9179a3da. - BookKeeper:
4.18.1. - Verification used managed-ledger/schema-storage tests and a separate check with a real BookKeeper client.
Issue Description
Pulsar brokers can leak heap memory when managed-ledger and schema-storage operations leave BookKeeper read handles open after their final use. Repeating these operations can accumulate abandoned handles and associated state, including on successful operation paths.
What changed: Pulsar #25199 (merged 2026-02-05) enabled keepUpdateMetadata=true on recovery opens, using the API added by BookKeeper #4613 to keep reads working after bookie decommissioning and data re-replication. Previously, these opens used keepUpdateMetadata=false and did not register metadata listeners. The existing missing-close paths remained unchanged.
With watches enabled, PulsarLedgerManager.listeners strongly references the abandoned handles after operations finish. Repeated opens can now accumulate handles through this additional reference path. Closing each handle unregisters its listener and releases that reference. Non-recovery opens already registered listeners; their missing-close paths are also affected and are not attributed to this change.
A check with a real BookKeeper client reproduced retained-handle accumulation across managed-ledger close/reopen cycles, with writes between cycles and older ledgers retained. Closing the temporary initialization handles prevented accumulation in the same check.
The missing cleanup falls into three groups:
- Temporary use: writable/read-only managed-ledger initialization, LAC rechecks after concurrent metadata modification, and offline ledger/cursor statistics leave temporary handles open.
- Failure paths: cursor recovery leaves unclaimed handles open on empty-ledger, read-error/rollback, and decode-error paths; schema storage skips handle cleanup when the entry read fails.
- Shutdown: closing a read-only managed ledger skips cached read-handle cleanup because there is no current write handle.
Expected: close temporary or abandoned handles after their final asynchronous use, on both success and failure, and release cached handles during read-only shutdown. Handles adopted by a recovered cursor or a terminated managed ledger must remain open until their owner releases them.
Error messages
No broker exception is required: successful operations can omit cleanup. Lifecycle regression tests detect the missing close call:
Wanted but not invoked:
pulsarMockReadHandle.closeAsync();
Reproducing the issue
For the managed-ledger initialization path:
- Create a managed ledger with retention enabled so its data ledgers are kept.
- Add an entry and close the managed ledger.
- Reopen it using the same BookKeeper client, tracking the read handle opened for the last ledger during initialization.
- Wait for initialization and any interceptor work to finish. The temporary handle has not been closed or transferred to an owner, although a new current write ledger has been created.
- Repeat with writes between close/reopen cycles. Abandoned watch-enabled handles remain registered and can accumulate while the listener registry is alive and the ledgers are retained.
For schema storage, let the ledger open succeed and complete the entry read with a BookKeeper read error. The read future fails, but the opened handle does not receive asyncClose.
Additional information
- Earlier schema cleanup gap: Pulsar #1363 already closed schema read handles only after successful reads, leaving the read-failure path unclosed.
- Earlier initialization cleanup gap: the temporary-handle omission is already present in the initial managed-ledger source import. These cleanup gaps predate the watch change.
- Ledger deletion may also remove listener registrations, so retaining ledgers matters when observing accumulation.
- Released-version and standalone-cluster reproduction have not been verified. Production heap growth, OOM impact, and direct-buffer leakage have not been demonstrated.
- Scope: missing handle cleanup and retained metadata listeners. Watch registration/close ordering is a separate concern.
Are you willing to submit a PR?
- I'm willing to submit a PR!
Activity
[-][Bug] Unclosed BookKeeper read handles retain ledger metadata listeners[/-][+][Bug] Broker memory leak caused by unclosed BookKeeper read handles[/+]void-ptr974 commentedon Oct 9, 2026
Hi @poorbarcode, could you help review the linked fixes in #26816, #26817, and #26818 when you have time? They address the read-handle cleanup paths described here and include regression tests. In particular, I'd like your input on the handle ownership and cleanup timing. Thanks.
Asthenia0412 commentedon Oct 13, 2026
Hi! I want to help fix this memory leak. Reviewing the BookKeeper read handle close paths now.
Asthenia0412 commentedon Oct 13, 2026
初步分析
感谢提供如此详尽的根因描述!我理解了问题的全貌:
核心机制:PulsarLedgerManager.listeners 持有 handle 强引用 → handle 没 close → listener 不注销 → 引用链不释放 → 内存泄漏。Pulsar #25199 的
keepUpdateMetadata=true让问题更严重,但 missing-close paths 本身一直存在。修复优先级建议:
修复模式(Java 标准做法):
边界情况:如果 handle 被 cursor/ledger "adopted"(接管了生命周期),就不能 close,需要在 adopt 时做转移。
我正在定位具体的文件和行号,准备提交修复 PR。
[fix][ml] Close cached read handles when read-only managed ledger shu…