Skip to content

[Bug] Broker memory leak caused by unclosed BookKeeper read handles #26815

Open

Description

@void-ptr974

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 at 1046481c970067cbca9c80a25a98257b9179a3da.
  • 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:

  1. Create a managed ledger with retention enabled so its data ledgers are kept.
  2. Add an entry and close the managed ledger.
  3. Reopen it using the same BookKeeper client, tracking the read handle opened for the last ledger during initialization.
  4. 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.
  5. 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

  1. changed the title [-][Bug] Unclosed BookKeeper read handles retain ledger metadata listeners[/-] [+][Bug] Broker memory leak caused by unclosed BookKeeper read handles[/+] on Oct 3, 2026
  2. void-ptr974 commented on Oct 9, 2026

    @void-ptr974
    ContributorAuthor

    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.

  3. Asthenia0412 commented on Oct 13, 2026

    @Asthenia0412
    Contributor

    Hi! I want to help fix this memory leak. Reviewing the BookKeeper read handle close paths now.

  4. Asthenia0412 commented on Oct 13, 2026

    @Asthenia0412
    Contributor

    初步分析

    感谢提供如此详尽的根因描述!我理解了问题的全貌:

    核心机制:PulsarLedgerManager.listeners 持有 handle 强引用 → handle 没 close → listener 不注销 → 引用链不释放 → 内存泄漏。Pulsar #25199 的 keepUpdateMetadata=true 让问题更严重,但 missing-close paths 本身一直存在。

    修复优先级建议:

    1. Shutdown path(影响最大,broker 正常关闭时就会触发)— read-only managed ledger 关闭时清理 cached handles
    2. Failure paths(empty-ledger, decode-error, read-error)— finally 块兜底
    3. Temporary use paths(初始化、LAC rechecks)— try-with-resources

    修复模式(Java 标准做法):

    // 当前(缺 close):
    ReadHandle handle = ledgerManager.openReadOnly(ledgerId);
    doSomething(handle);  // 如果这里抛异常,handle 永远不 close
    
    // 修复后:
    ReadHandle handle = ledgerManager.openReadOnly(ledgerId);
    try {
        doSomething(handle);
    } finally {
        handle.close();  // 或 adopt 后交给 owner
    }

    边界情况:如果 handle 被 cursor/ledger "adopted"(接管了生命周期),就不能 close,需要在 adopt 时做转移。

    我正在定位具体的文件和行号,准备提交修复 PR。

  5. added a commit that references this issue on Oct 17, 2026
    8af6099
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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