Repository navigation
[fix][ml] Close abandoned write ledger handle to prevent leak - #26585
Merged
merlimat merged 2 commits intoSep 16, 2026
Merged
Conversation
…leak Motivation When an add-entry fails or a ledger is detected as concurrently modified, the managed ledger switches to a new ledger but never closes the old write handle. An open write handle keeps its periodic explicit-LAC flush task scheduled on the BookKeeper client scheduler, so every such ledger leaks the handle and the task for the lifetime of the broker. In a long-running broker this accumulates hundreds of thousands of handles and scheduled tasks (observed: ~280k LedgerHandles retaining ~1GB, 67.8% of the heap in a single scheduler delay queue), and the LAC flush storm starves the scheduler thread, stalling reads. Modifications - OpAddEntry#handleAddFailure: close the failed ledger before switching to a new one, mirroring rollCurrentLedgerIfFull, and only then trigger ledgerClosed. - ManagedLedgerImpl#addEntryFailedDueToConcurrentlyModified: close the abandoned write handle after switching to a new ledger. - PulsarMockLedgerHandle: track asyncClose() invocations so tests can assert handles are closed instead of merely abandoned. - Add/update positive tests verifying the failed/concurrently modified handle is closed and writes recover on a new ledger.
lhotari
reviewed
Sep 15, 2026
lhotari
left a comment
Member
There was a problem hiding this comment.
Thanks for tracking down the leaked handles. The existing timeout tests need their close callbacks updated for this change (details inline). Please also use [fix][ml] in the title; the title check rejects managed-ledger.
Motivation The add-entry timeout tests stub the current ledger with a Mockito mock of a PulsarMockLedgerHandle subclass. The change to close the failed ledger before switching (mirroring the rollover path) waits for the asyncClose callback, but Mockito suppresses the subclass's asyncClose override, so the callback never fires and the tests time out / fail their success assertion. Modifications - ManagedLedgerTest#testManagedLedgerWithAddEntryTimeOut: stub asyncClose on the mock to invoke the close callback with OK. - BrokerClientIntegrationTest#testAddEntryOperationTimeout: same.
merlimat
approved these changes
Sep 16, 2026
merlimat
pushed a commit
that referenced
this pull request
Sep 16, 2026
merlimat
pushed a commit
that referenced
this pull request
Sep 16, 2026
Technoboy-
added a commit
that referenced
this pull request
Sep 17, 2026
Technoboy-
added a commit
that referenced
this pull request
Sep 17, 2026
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
When an add-entry fails or a ledger is detected as concurrently modified, the managed ledger switches to a new ledger but never closes the old write handle. An open write handle keeps its periodic explicit-LAC flush task scheduled on the BookKeeper client scheduler, so every such ledger leaks the handle and the task for the lifetime of the broker.
In a long-running broker this accumulates: heap-dump analysis showed ~280k
LedgerHandleobjects (vs. ~1.7k ledgers tracked by the managed ledgers), each with a 1s-period explicit-LAC flush task pinned in a single scheduler delay queue that retained ~997MB (67.8% of the heap). The resulting LAC flush storm keeps the scheduler thread busy re-heapifying the delay queue, starving BookKeeper read callbacks and stalling consumer dispatch.Modifications
OpAddEntry#handleAddFailure: close the failed ledger before switching to a new one, mirroringrollCurrentLedgerIfFull, and only then triggerledgerClosed.ManagedLedgerImpl#addEntryFailedDueToConcurrentlyModified: close the abandoned write handle after switching to a new ledger.PulsarMockLedgerHandle: trackasyncClose()invocations so tests can assert handles are closed instead of merely abandoned.Evidence
Two heap dumps of the same broker process (5 minutes apart) both show the leak:
LedgerHandle(write)ExplicitLacFlushPolicyImplScheduledFutureTask(period = 1s)ManagedLedgerInfo.LedgerInfo(ML bookkeeping)The managed ledgers only track ~1.7k ledgers in metadata, so ~279k write handles are orphaned (switched away from but never closed). Every leaked handle keeps a 1-second explicit-LAC flush task alive;
org.eclipse.mat.api:suspectsreports one schedulerDelayedWorkQueueretaining 997MB (67.8% of heap) with the scheduler thread caught insiftDown.Verifying this change
This change added tests and can be verified as follows:
ManagedLedgerErrorsTest#writeErrorClosesFailedLedgerHandle: a write error on the current ledger must close the failed handle exactly once, switch to a new ledger and keep writes working.ManagedLedgerErrorsTest#concurrentlyModifiedLedgerHandleIsClosedAndWritesRecover: aLedgerFencedException(concurrently modified ledger) must close the abandoned handle and recover on a new ledger.ManagedLedgerErrorsTest#recoverLongTimeAfterMultipleWriteErrors: extended to assert the failed handle is closed after multiple write errors.All 22 tests in
ManagedLedgerErrorsTestpass;:managed-ledger:checkstyleMain,:managed-ledger:checkstyleTestand:testmocks:checkstyleMainpass.