Skip to content

[fix][ml] Run the add-entry timeout check on the managed ledger executor - #27056

Merged
merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-fix-ml-add-timeout-check
Oct 22, 2026
Merged

merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-fix-ml-add-timeout-check

Conversation

@lhotari

@lhotari lhotari commented Oct 22, 2026

Copy link
Copy Markdown
Member

Motivation

The managed ledger's add-entry timeout check (checkAddTimeout) runs periodically on a scheduler thread. It peeks the pooled OpAddEntry at the head of pendingAddEntries, reads its lastInitTime, ledger and entryId, and then calls handleAddFailure.

Meanwhile the add completes on the managed ledger executor: OpAddEntry.run() polls the op from the queue, completes it and recycles it. Recycling nulls ml and ledger, and the Netty Recycler can then hand the same object to another add, possibly one for another managed ledger. The window is narrow, but it is exactly when the timeout matters: a slow bookie add completing around the deadline. In that window the check can:

  • fail with a NullPointerException in handleAddFailure (this.ml is null). The task is scheduled with scheduleAtFixedRate without an exception guard, so the exception cancels all later runs and the managed ledger silently loses add-timeout detection;
  • or apply the timeout to an op that has already completed or has been reused by another add.

When the bug was introduced: the check has run on the scheduler thread, peeking pendingAddEntries, since the add-entry timeout was added in #3347 (a704afd, 2019-01-15) and #4111 (5e96a9c, 2019-04-23). OpAddEntry.recycle() has nulled ml since the initial import. The exception guard on the periodic task was lost in #20060 (bafecb2, 2023-04-10, "Removed usages of SafeRun"). The same code is on branch-3.0, branch-3.3, branch-4.0, branch-4.1 and branch-4.2, so this is long-standing rather than a recent regression. On those branches the periodic task also runs the read-timeout check, so an exception there stops read-timeout detection for that managed ledger as well.

Modifications

  • The periodic task now hands the check over to the managed ledger executor, where adds complete and get recycled. The check is serialized with those completions and can never read a recycled op. checkAddTimeout itself is unchanged.
  • Both the periodic task and the check are wrapped with catchingAndLoggingThrowables, the helper already used for other periodic managed-ledger tasks, so an exception can't cancel the periodic task.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

  • Added ManagedLedgerTest.testAddTimeoutCheckRunsAfterQueuedAddCompletion. An add is made older than the timeout, and its successful completion is queued on a blocked managed ledger executor. Triggering the timeout check must not apply the timeout. Without the fix, the check applies the timeout to the completed add and the ledger gets switched. The existing add-timeout tests (testManagedLedgerWithAddEntryTimeOut, testAddEntryResponseTimeout, testLedgerInfoMetaCorrectIfAddEntryTimeOut) still pass.

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

If the box was checked, please highlight the changes

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model
    • The periodic add-entry timeout check now runs on the managed ledger's executor instead of the scheduler thread.
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

The periodic add-entry timeout check ran on the scheduler thread and read the
pooled OpAddEntry at the head of the pending queue while the managed ledger
executor could complete and recycle it. It could then fail with an NPE, which
cancels the periodic check for good, or apply the timeout to an op reused by
another add. Hop to the managed ledger executor, where the adds complete, and
keep any exception from cancelling the periodic task.

Assisted-by: Claude Code (claude-opus-5-5)
@merlimat merlimat added this to the 5.1.0 milestone Oct 22, 2026
@merlimat
merlimat merged commit aeaf47e into apache:master Oct 22, 2026
44 checks passed
lhotari added a commit that referenced this pull request Oct 23, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
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.

2 participants