Skip to content

[fix][ml] Fix metadata listener leaks caused by unclosed temporary read handles - #26817

Merged
lhotari merged 9 commits into
apache:masterfrom
void-ptr974:fix/temporary-read-handle-leaks
Nov 20, 2026
Merged

lhotari merged 9 commits into
apache:masterfrom
void-ptr974:fix/temporary-read-handle-leaks

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Main Issue: #26815

Motivation

Managed-ledger initialization and cursor recovery open BookKeeper read handles that can keep metadata listeners registered after Pulsar stops using them. This can also happen when cursor metadata deletion fails or when a cursor finishes recovering after its managed ledger has closed.

Modifications

  • Release temporary initialization handles when the interceptor or LAC read finishes, including synchronous and asynchronous failures. A terminated ledger retains the handle it uses for reads.
  • Release unclaimed cursor recovery handles on failure or fallback. Release cursor-owned read handles on close, ledger replacement, deletion, and writer-close failure. A failed cursor metadata deletion releases the local handle while retaining the cursor metadata and its BookKeeper ledger.
  • Serialize eager and lazy cursor registration with managed-ledger close. Keep pending recoveries out of the close wait and release a late recovery's local handle in its callback. Preserve Closed when an in-flight metadata write later reports BadVersion.

Verifying this change

  • Make sure that the change passes the CI checks.

  • 82 focused tests passed locally, along with managed-ledger Spotless and test-source Checkstyle.

  • The new regressions verify listener deregistration when a cursor metadata deletion fails after unload and when eager recovery succeeds after initialization fails and closes the managed ledger. They also check that the persisted cursor metadata and BookKeeper ledger are preserved.

  • The lazy recovery regression submits a stale metadata write after a new owner opens. It verifies that the old listener is released, the old open reports that its ledger is closed, and the new owner can still acknowledge entries.

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Close temporary handles after initialization, LAC rechecks, offline statistics, and abandoned cursor recovery. Keep handles open until their asynchronous users finish and preserve ownership transfers.

Main Issue: apache#26815

Assisted-by: OpenAI Codex
@void-ptr974 void-ptr974 changed the title [fix][ml] Close temporary read handles after use [fix][ml] Fix metadata listener leaks caused by unclosed temporary read handles Oct 2, 2026
Cover synchronous cleanup and LAC failures, offline cursor empty and malformed entries, and successful cursor ownership cleanup.

Assisted-by: OpenAI Codex

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking down these leaked read handles and for the thorough test class.

I traced each changed path and found no missed close or double close on the paths this PR touches. Each countDown() still runs once. The interceptor chain is equivalent to before. The terminated ledger keeps its handle. Cursor recovery keeps the handle it hands to the recovered cursor.

I agree with the open point about failures after parseFrom() in cursor recovery. Two smaller notes are inline: one adjacent offline-statistics path that still leaves a handle open, and what the new tests can and cannot show.

Handle cursor snapshot recovery failures after decoding, close unclaimed
handles, and report the original failure without re-entering callbacks.
Own and close the offline backlog retry handle explicitly, releasing each
read entry while preserving the iterator's LAC and failure behavior.

Add regression coverage for recovery failures, offline retry boundaries,
and listener registration and deregistration with real BookKeeper handles.
Validation: 48 scoped tests and quickCheck pass. Reverting the cursor fix
makes the two new recovery-failure cases time out on the PR head.

Assisted-by: Codex
Release recovered read-only cursor ledger handles when the cursor closes,
switches ledgers, or is deleted, independently of ledger metadata deletion.
Preserve the existing writable-handle lifecycle and deletion retry behavior.

Complete offline backlog estimation when opening a cursor ledger fails,
without dereferencing the absent handle.

Add regression coverage for failed cursor opens and real metadata listener
cleanup when deletion fails or remains pending.

Assisted-by: Codex
Keep offline cursor metadata callbacks nonblocking and aggregate backlog
results on the calling thread. Release late read handles and ledger entries
when the accurate-statistics fallback is interrupted.

Release recovered cursor read handles when closing cannot persist metadata,
while retaining the ledger for recovery. Include pending cursor recovery in
managed-ledger close and reject cursor opens that complete after closure.

Add controlled regression tests for metadata scheduling, close failures,
late recovery, and interrupted open/read results.

Assisted-by: Codex
@void-ptr974

Copy link
Copy Markdown
Contributor Author

I've pushed de8d977 with four follow-up fixes:

  • Offline backlog estimation now gathers cursor metadata asynchronously, avoiding a deadlock on the metadata executor.
  • If persisting cursor metadata fails during close, the recovered read handle is still closed, while the cursor ledger is retained for recovery.
  • Managed-ledger close now waits for pending cursor recovery or creation to be cleaned up, so late completions cannot leave cursors open after close completes.
  • The accurate backlog fallback now releases handles and read results that arrive after the waiting thread is interrupted.

Added tests covering actual metadata callback scheduling, listener deregistration, and buffer reference counts. All 95 scoped tests passed locally with retries disabled, along with quickCheck. Selected regression tests also reproduced the original failures with the fixes reverted.

Merge upstream master while retaining temporary-handle ownership and
pending cursor cleanup. Keep the upstream asynchronous offline backlog
implementation and consolidate its best-effort recovery handle cleanup.

Preserve cursor idle-state recovery and reuse an in-flight lazy recovery
when concurrent opens retry a previous failure. Update regression tests
for asynchronous backlog reads and atomic statistics publication.

Assisted-by: Codex

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two earlier points are addressed, and the read-handle ownership paths I traced all close the handle once on success, failure and interruption. Two smaller notes on the new close and cursor-open logic are inline.

Merge upstream master. Release handles from late lazy cursor recovery without waiting in managed-ledger close or modifying cursor metadata. Release recovered read handles when writer close fails, preserve the entry fencing check, and share concurrent recovery retries.

Assisted-by: Codex (GPT-6.2 Sol)
Keep initialization exception handling aligned with master. Remove extra coverage of upstream offline backlog and LAC recheck logic, and use the listener lifecycle tests for overlapping close scenarios.

Assisted-by: Codex (GPT-6.2)
… recovery

Release local read handles when cursor metadata deletion fails. Guard eager recovery registration with the same close check as lazy recovery, and preserve Closed when a delayed metadata update fences the old ledger.

Assisted-by: Codex (GPT-6.2)

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Close no longer depends on pending cursor recovery, and the late-recovery paths release their handle instead. The completion-time fence check is gone. I mutation-tested each new closeReadOnlyCursorLedger() call site (removing any one makes a lifecycle test fail), so the tests do pin the behavior. One optional note on the fenced-unload path is inline.

@lhotari
lhotari merged commit 3577758 into apache:master Nov 20, 2026
44 checks passed
lhotari pushed a commit that referenced this pull request Nov 20, 2026
ascentstream-bot pushed a commit to ascentstream/pulsar that referenced this pull request Nov 24, 2026
codelipenghui added a commit to codelipenghui/incubator-pulsar that referenced this pull request Nov 24, 2026
Resolve the conflicts in ManagedCursorImpl and adapt the publication path to
the cursor changes on master:

- apache#27703 rebuilds the cursor properties in updateCursorLedgerStat, so drop
  adoptStoredCursorInfo and keep only the deletion of retained ledgers there.
  Sample the version and the properties as one pair in publishCursorLedger as
  well as in persistPositionMetaStore, and pass them to buildManagedCursorInfo.
- apache#27652 assigns a publication ID in MetaStoreImpl during the write. Assign it
  in publishCursorLedger before taking the attempted bytes, so that
  reconciliation still recognizes a committed publication.
- apache#27671 makes a closing cursor wait for an in-flight ledger switch, so allow
  that switch to publish while the cursor is Closing.
- apache#26817 closes the read-only cursor ledger in asyncDeleteCursorLedger; keep
  that next to the deletion of retained ledgers.

MetaStoreImpl now calls the put overload with options, so the fault-injection
stubs in ManagedCursorTest target that overload.

Assisted-by: Claude Code (Claude Opus 5.5)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants