GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][ml] Fix metadata listener leaks caused by unclosed temporary read handles by void-ptr974 · Pull Request #26817 · apache/pulsar

Conversation

@void-ptr974

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

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@void-ptr974
[fix][ml] Close temporary read handles after use
6e6760d
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
@void-ptr974
Expand temporary read handle cleanup regression coverage
e1e64c5
Cover synchronous cleanup and LAC failures, offline cursor empty and malformed entries, and successful cursor ownership cleanup.

Assisted-by: OpenAI Codex
@void-ptr974 void-ptr974 mentioned this pull request Oct 2, 2026
[fix][ml] Fix cached read handle leaks when closing read-only managed ledgers #26816
Open
3 of 11 tasks
@void-ptr974
void-ptr974 marked this pull request as ready for review October 2, 2026 15:58
@void-ptr974 void-ptr974 mentioned this pull request Oct 9, 2026
[Bug] Broker memory leak caused by unclosed BookKeeper read handles #26815
Open
3 tasks done
Denovo1998
Denovo1998 requested changes Oct 9, 2026
View reviewed changes
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

lhotari
lhotari reviewed Oct 13, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerFactoryImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/TemporaryReadHandleTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

void-ptr974 added 2 commits October 14, 2026 16:14
@void-ptr974
[fix][ml] Complete temporary read handle cleanup
2341cbb
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
@void-ptr974
[fix][ml] Handle cursor cleanup and offline statistics failures
31c5bdd
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
Denovo1998
Denovo1998 approved these changes Oct 14, 2026
View reviewed changes
@void-ptr974
[fix][ml] Complete asynchronous read handle cleanup
de8d977
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

void-ptr974 commented Oct 14, 2026

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Denovo1998
Denovo1998 reviewed Oct 15, 2026
View reviewed changes
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@void-ptr974 void-ptr974 mentioned this pull request Oct 18, 2026
[fix][broker] Fix managed-ledger scheduler thread stalls when estimating offline backlog #26972
Merged
3 of 11 tasks
@Denovo1998 Denovo1998 mentioned this pull request Oct 18, 2026
[fix][ml] Close recovered cursor read handles after fencing Denovo1998/pulsar#53
Open
2 of 11 tasks
@void-ptr974
[fix][ml] Reconcile temporary read handle cleanup with master
6934f13
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
This was referenced Oct 28, 2026
[fix][ml] Close abandoned managed ledger initialization resources Denovo1998/pulsar#58
Open
[fix][ml] Close abandoned managed ledger initialization resources #27212
Merged
lhotari
lhotari reviewed Nov 16, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

void-ptr974 added 3 commits November 18, 2026 13:07
@void-ptr974
[fix][ml] Address read handle lifecycle review
dc1eb8b
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)
@void-ptr974
[fix][ml] Keep read handle leak changes focused
e53960e
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)
@void-ptr974
[fix][ml] Release read handles after failed cursor deletion and eager…
ae6b40e
… 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 approved these changes Nov 18, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedLedgerImpl.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@void-ptr974 void-ptr974 mentioned this pull request Nov 19, 2026
[Bug] Fenced managed ledger unload can leak recovered cursor read handles #27670
Closed
@lhotari
lhotari merged commit 3577758 into apache:master Nov 20, 2026
44 checks passed

Uh oh!

There was an error while loading. Please reload this page.

lhotari pushed a commit that referenced this pull request Nov 20, 2026
@void-ptr974 @lhotari
[fix][ml] Fix metadata listener leaks caused by unclosed temporary re…
135f4bf
…ad handles (#26817)

(cherry picked from commit 3577758)
@lhotari lhotari added release/5.0.2 cherry-picked/branch-5.0 cherry-picked/branch-4.2 labels Nov 20, 2026
@lhotari lhotari added release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 24, 2026
ascentstream-bot pushed a commit to ascentstream/pulsar that referenced this pull request Nov 24, 2026
@void-ptr974 @lhotari
[fix][ml] Fix metadata listener leaks caused by unclosed temporary re…
8dede0b
…ad handles (apache#26817)

(cherry picked from commit 3577758)
codelipenghui added a commit to codelipenghui/incubator-pulsar that referenced this pull request Nov 24, 2026
@codelipenghui
Merge apache/master into penghui/fix-cursor-publication-20261105
b729ebf
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
@void-ptr974 @lhotari
[fix][ml] Fix metadata listener leaks caused by unclosed temporary re…
d3c2d7e
…ad handles (apache#26817)

(cherry picked from commit 3577758)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari approved these changes
@Denovo1998 Denovo1998 Denovo1998 approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@void-ptr974 @lhotari @Denovo1998
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.