Skip to content

[fix][ml] Close abandoned write ledger handle to prevent leak - #26585

Merged
merlimat merged 2 commits into
apache:masterfrom
nodece:fix-ledger-handle-leak-write-error
Sep 16, 2026
Merged

merlimat merged 2 commits into
apache:masterfrom
nodece:fix-ledger-handle-leak-write-error

Conversation

@nodece

@nodece nodece commented Sep 15, 2026 •

Copy link
Copy Markdown
Member

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 LedgerHandle objects (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, 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.

Evidence

Two heap dumps of the same broker process (5 minutes apart) both show the leak:

Object dump-1 dump-2
LedgerHandle (write) 280,671 280,683
ExplicitLacFlushPolicyImpl 280,671 280,683
ScheduledFutureTask (period = 1s) 281,031 281,048
ManagedLedgerInfo.LedgerInfo (ML bookkeeping) 1,766 1,768

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:suspects reports one scheduler DelayedWorkQueue retaining 997MB (67.8% of heap) with the scheduler thread caught in siftDown.

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: a LedgerFencedException (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 ManagedLedgerErrorsTest pass; :managed-ledger:checkstyleMain, :managed-ledger:checkstyleTest and :testmocks:checkstyleMain pass.

…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 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 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.

@nodece nodece changed the title [fix][managed-ledger] Close abandoned write ledger handle to prevent leak [fix][ml] Close abandoned write ledger handle to prevent leak Sep 15, 2026
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.
@nodece
nodece requested a review from lhotari September 16, 2026 02:08
@nodece nodece self-assigned this Sep 16, 2026
@nodece nodece added this to the 5.0.0 milestone Sep 16, 2026
@merlimat
merlimat merged commit 1234205 into apache:master Sep 16, 2026
43 checks passed
@nodece
nodece deleted the fix-ledger-handle-leak-write-error branch September 17, 2026 01:46
Technoboy- added a commit that referenced this pull request Sep 17, 2026
Technoboy- added a commit that referenced this pull request Sep 17, 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