Repository navigation
Description
Search before reporting
- I searched in the issues and found nothing similar.
Read release policy
- I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.
User environment
This is a code-analysis report, not an observed incident. There is no production failure behind it, no reproducer has been written or run, and no test was executed. Everything below was established by reading the code on master and by git log/git blame on the commit that introduced the behaviour change. The reachability discussion at the end is deliberately explicit about what is structurally proven versus what remains untested.
This was noticed while reviewing PR #26299 (an unrelated internalReadFromLedger optimization). It is independent of that PR — the defect described here predates it and is not affected by it either way. The original concern raised during that review (a stale read-position hop applied across a reset) did not hold up: for durable cursors the PENDING_READ_OPS deferral at ManagedCursorImpl.java:2394 orders the reset last. Following that thread is what surfaced the issue below.
- Code examined:
apache/pulsarmasterat commitcb90c12a14d. All line numbers below are from that tree. - Affected component:
managed-ledger(ManagedCursorImpl), reached from brokerseekand adminreset-cursor. - Affected branches:
master,branch-5.0-M1,branch-4.2,branch-4.1,branch-4.0. - Affected releases:
4.0.9+,4.1.3+,4.2.0+,5.0.0-M1. Not affected:branch-3.0,branch-3.3, and 4.0/4.1 releases predating the backports (see Regression provenance). - Not affected: non-durable cursors /
Readerseek —NonDurableCursorImpl.internalAsyncMarkDeleterunsmdEntry.alignAcknowledgeStatus()synchronously (NonDurableCursorImpl.java:116) and has no pending queue.
Issue Description
A durable cursor reset — pulsar-admin topics reset-cursor, or a client consumer.seek(...) — can have its entire state mutation silently dropped while the operation reports success to the caller, if another mark-delete entry is enqueued behind it while the cursor's mark-delete queue is buffering.
Why the reset can be dropped
Since #25047, the reset's whole state mutation lives in a per-MarkDeleteEntry runnable, not in the reset's completion callback:
ManagedCursorImpl.java:1627-1679—alignAcknowledgeStatusAfterPersistedfor the reset. It correctsmessagesConsumedCounter(:1631-1651), callsindividualDeletedMessages.removeAtMost(:1639), assignsmarkDeletePosition(:1652) andlastMarkDeleteEntry(:1653), doesindividualDeletedMessages.clear()(:1655), clears and re-seedsbatchDeletedIndexes(:1656-1664), emits the two confirming log lines (:1671,:1676), and assignsreadPosition(:1678). It is handed tointernalAsyncMarkDeleteat:1721-1732.ManagedCursorImpl.java:1681-1702— the reset's own completion path (finalCallback.operationComplete()) mutates no cursor state. It callsledger.onCursorReadPositionUpdated(:1688), clearspendingMarkDeleteOps(:1693), CASesRESET_CURSOR_IN_PROGRESSback toFALSE(:1694), and callscallback.resetComplete(newReadPosition)(:1701).ManagedCursorImpl.java:2363-2409—internalAsyncMarkDelete, undersynchronized (pendingMarkDeleteOps)(:2368), queues the entry instead of executing it in three cases:NoLedger(:2384),SwitchingLedger(:2390), andOpenwithPENDING_READ_OPS_UPDATER.get(this) > 0(:2394-2396). Nothing in that block inspects whether a reset entry is already queued;peekLast()(:2370) is read only to inheritproperties.ManagedCursorImpl.java:3275-3281—internalFlushPendingMarkDeletespersists onlypendingMarkDeleteOps.getLast()(:3276), after setting that one entry'scallbackGroupto the whole queue (:3277).ManagedCursorImpl.java:2456-2478— on persist completion, onlymdEntry.alignAcknowledgeStatus()runs (:2468), i.e. the last entry's runnable, whilemdEntry.triggerComplete()(:2477) fires the callback of every entry incallbackGroup(:277-290).alignAcknowledgeStatus()has exactly two invocation sites in the tree:ManagedCursorImpl.java:2468andNonDurableCursorImpl.java:116. There is no second path that replays a displaced runnable.
The interleaving
- A read is in flight, so
PENDING_READ_OPS > 0(incremented atManagedCursorImpl.java:946,:1174,:3692). - The reset enqueues entry R carrying the reset runnable (
:1721-1732→:2394-2396). - Another mark-delete — in practice an individual acknowledgement via
asyncDelete→internalAsyncMarkDelete(..., null)(:2709-2720) — enqueues entry A behind it. Itsnullrunnable is replaced in theMarkDeleteEntryconstructor by the default at:260-268. - The read completes →
readOperationCompleted()(:3733-3746) →flushPendingMarkDeletes()(:3738) →internalFlushPendingMarkDeletes()picksgetLast() == Aand setsA.callbackGroup = [R, A]. - On persist completion, only A's runnable runs (
:2468). R's reset runnable is dropped.triggerComplete()(:2477) nevertheless fires R's callback →finalCallback.operationComplete()→callback.resetComplete(newReadPosition).
The mirror ordering (ack queued first, reset last) is benign: the reset is then getLast(), its runnable wins, and only the ack's default runnable is lost.
User-visible consequence
PersistentSubscription's resetComplete (PersistentSubscription.java:1026-1036) unfences the subscription and completes the future, so the admin REST call returns 2xx and the client seek() future completes normally, echoing the requested position — while the cursor never moved:
- Replay from a timestamp never happens.
reset-cursor --time 24horseek(timestamp)reports success;readPositionis unchanged, so a caught-up consumer receives nothing and the team concludes retention deleted the data or the timestamp was wrong. - A forward skip past a poison message does not take. Consumers reconnect after the disconnect at
PersistentSubscription.java:978-984and are immediately redelivered the same message, resuming the crash loop, with no error to point at. - The resulting state is worse than a no-op — it is ack-advanced and durable.
asyncDeletemutatesindividualDeletedMessagesand callssetAcknowledgedPosition(which assignsmarkDeletePositionat:2225and can pushreadPositionforward) synchronously before queueing, andLAST_MARK_DELETE_ENTRY_UPDATER(:2447-2454) keeps the later position, solastMarkDeleteEntrybecomes A.persistPositionToLedgerwritesmdEntry.newPosition(:3493) plus the live, never-clearedindividualDeletedMessages(:3505/:3515). A broker restart therefore reproduces the un-reset state rather than healing it. - Cursor properties are collaterally wiped.
asyncDeletepassesproperties == null(:2709), so A inherits the last queued entry's properties (:2370-2372) — the reset'sCollections.emptyMap()(:1721) — and that empty map is what gets persisted. - Transient ledger/cursor divergence.
ledger.onCursorReadPositionUpdated(this, newReadPosition)still fires at:1688, soManagedLedgerImpl.updateActiveCursor(ManagedLedgerImpl.java:2694-2698) records a read position the cursor never adopted.
A second, related silent-success path
internalMarkDelete has two skip branches that call mdEntry.triggerComplete() without ever calling alignAcknowledgeStatus(): the persistentMarkDeletePosition backward guard (:2412-2421) and the INPROGRESS_MARKDELETE_PERSIST_POSITION guard (:2432-2440). internalResetCursor nulls both fields at :1719-1720, but the default ack runnable re-arms persistentMarkDeletePosition (:267). So even when R is the flushed entry, a concurrent ack completing in between can make a backward reset take the skip branch — resetComplete(SUCCESS) with zero state change and nothing persisted. Any fix should cover both paths.
Error messages
No exception, no failed future, and no ERROR/WARN log is produced: the reset takes the
success branch end to end.
The one usable forensic marker is an asymmetry in the INFO log. internalResetCursor logs
on entry, outside the dropped runnable:
ManagedCursorImpl.java:1601-1604 "Initiate reset readPosition"
while both confirming lines live INSIDE the dropped runnable:
ManagedCursorImpl.java:1668-1671 "Reset readPosition to before current readPosition"
ManagedCursorImpl.java:1673-1676 "Reset readPosition to, skipping from current readPosition"
A dropped reset therefore shows "Initiate reset readPosition" with no matching completion
line for that cursor.
Reproducing the issue
No reproducer has been written or run. The window is timing-dependent, so a natural-traffic reproduction is unlikely; a deterministic unit test in managed-ledger is the practical route. Sketch, using hooks that already exist in the tree:
factory.open(...),openCursor("c1"),cursor.setInactive()before adding entries (so the entry cache does not bypass the mock bookie — precedent atManagedCursorTest.java:6283), then add 10 entriesp0..p9andmarkDelete(p4)so the state isOpenwith an existing cursor ledger.- Hold a read open:
bkc.setReadHandleInterceptor(...)(PulsarMockReadHandleInterceptor, used already atManagedCursorTest.java:6253-6280) returning an uncompleted future. Wait untilcursor.getPendingReadOpsCount() == 1(ManagedCursorImpl.java:3878). cursor.asyncResetCursor(p0, false, cb)— it hops toledger.getExecutor()(:1740), so wait oncursor.pendingMarkDeleteOps.size() == 1(the field isprotected,:307, so a same-package test needs no reflection).cursor.asyncDelete(p5, cb, null)withp5strictly aftermarkDeletePositionso it is not short-circuited; wait forsize() == 2and assertgetLast().newPosition == p5to pin the precondition.- Complete the interceptor future.
readOperationCompleted()→ flush →getLast() == A. - Assert the reset actually applied:
getReadPosition() == p0,getMarkDeletedPosition() == ledger.getPreviousPosition(p0),isMessageDeleted(p5) == false. These should fail today whileresetCompletestill fires. - Control arm: same test with the ack enqueued first and the reset second — the same assertions should pass, isolating displacement as the defect.
- Optional durability arm: close and reopen the ledger/cursor and re-assert, showing a restart does not heal it.
A second, read-free variant exercises the independent flush path: force NoLedger/SwitchingLedger (:2384-2390) via delayed cursor-ledger creation and enqueue R then A — the flush at :3229/:3248 uses the same getLast() logic.
Additional information
Regression provenance
This is a regression, not long-standing behaviour. git log -S"alignAcknowledgeStatusAfterPersisted" yields exactly one commit:
81aff30c4b461b2c630f413c407ba014e7a5d571— "[fix][broker]Fix incorrect backlog if use multiple acknowledge types on the same subscription (#25047)", 2025-12-15.
Its diff moves the reset's state mutation out of finalCallback.operationComplete() and into the per-entry runnable. That matters because operationComplete() is reached via MarkDeleteEntry.triggerComplete(), which runs for every entry in the callbackGroup — so before #25047 the same displacement was harmless and the reset still applied to in-memory state. After #25047 only getLast()'s runnable executes, so displacement loses the reset.
The getLast()-only persistence and the callbackGroup fan-out themselves date to the 2016 initial import (9f849c32496); they were safe for cursor reset only because the reset rode the fan-out.
Containment: 81aff30c4b4 is in origin/master, origin/branch-4.2, origin/branch-5.0-M1; backported as 08f6c337a7f (branch-4.0) and bfa6de4c221 (branch-4.1). alignAcknowledgeStatusAfterPersisted does not appear in branch-3.0 or branch-3.3.
Ruled out — things checked that do not protect against this
RESET_CURSOR_IN_PROGRESS. Read in exactly one place on the write path,asyncMarkDelete(:2277); other occurrences are the declaration (:169), init (:383) and the reset's own CAS (:1607,:1694,:1707). Cumulative acks are correctly rejected; individual acks enter viaasyncDelete(:2575, onlyisClosed()guarded) and never touch the gate.synchronized (pendingMarkDeleteOps). Makes eachaddatomic; it establishes no exclusion of later adds while an earlier entry is queued, and there is no dedup, merge, or "reset wins" branch.- Subscription fencing (
IS_FENCED). Only read inaddConsumerInternal(PersistentSubscription.java:271); it blocks re-attaching consumers.acknowledgeMessageAsync(:463-516) has no fence check at all and reachescursor.asyncDeleteat:509. - Consumer disconnect before the reset.
resetCursorInternaldisconnects consumers (:978-984) beforecursor.asyncResetCursor(:1024), but this is not a drain:Consumer.disconnectcloses synchronously, so the future is already complete when returned. cancelPendingRead().ManagedCursorImpl.cancelPendingReadRequest()(:1196-1208) only clearsWAITING_READ_OP— a read already submitted to bookies cannot be cancelled, and the two states are disjoint. The tree documents exactly this atPersistentReplicator.java:385-392.- A later flush re-running R. The queue is cleared at
:3278(and again at:1693); R survives only inside A'scallbackGroup, andtriggerComplete()never callsalignAcknowledgeStatus(). - Recovery from persisted metadata.
persistPositionToLedgerwrites A's position (:3493) with the liveindividualDeletedMessages, so recovery reproduces the damage. - Ledger switch.
createNewMetadataLedgerpersistslastMarkDeleteEntry, which:2447-2454set to A, never R. rewind()/onCursorReadPositionUpdated.rewind()(:2836-2858) derivesreadPositionfrom the stalemarkDeletePosition, re-affirming the wrong state;onCursorReadPositionUpdatedis cache-eviction bookkeeping only (ManagedLedgerImpl.java:2694-2698).- Dispatcher re-derivation after reset.
dispatcher.cursorIsReset()only nullslastIndividualDeletedRangeFromCursorRecovery; nothing recomputes cursor state.
Honest assessment of reachability
The mechanism is structurally proven; the frequency is not, and I want to be explicit about the narrowing factors:
resetCursorInternalfences and disconnects consumers before the reset, removing the obvious source of concurrentCommandAcktraffic.throttleMarkDeletedefaults to1.0/s (ServiceConfiguration.java:2634→BrokerService.java:2272-2274), somarkDeleteLimiter(ManagedCursorImpl.java:2701) drops most individual acks before they can enqueue an entry at all.
Ack sources that fencing does not cover, and that need no connected consumer:
- Transaction pending-ack commit:
ServerCnx.handleEndTxnOnSubscription→PersistentSubscription.endTxn→PendingAckHandleImpl.commitTxn(:531, on its owninternalPinnedExecutor) →individualAckCommitCommon(:795-806) →acknowledgeMessageAsync(..., Individual)→cursor.asyncDelete. ManagedCursorImpl.skipNonRecoverableLedger(:3132-3155) /skipNonRecoverableEntries(:3165+) underautoSkipNonRecoverableData, invoked fromOpReadEntry.internalReadEntriesFailed— i.e. during a read.- Entry-filter-driven acks in
AbstractBaseDispatcher(:300-334). - A
CommandAckalready queued on the topic's ordered executor when the disconnect loop runs.
Additionally, the NoLedger/SwitchingLedger queueing branches mean a cursor-ledger rollover concurrent with a seek reproduces the displacement with no read in flight at all.
Existing test coverage
None covers this. Nearest neighbours, and why they miss it:
ManagedCursorTest.testCompactionCursorResetNeverLoseMarkDeleteProperties(:6630-6711) — the only test interleaving a reset with a mark-delete, but it drives the mark-delete first (the benign ordering), never establishesPENDING_READ_OPS > 0, and uses cumulativeasyncMarkDelete, so nothing is ever displaced.ManagedCursorTest.testConcurrentResetCursor(:1047-1142) — concurrency across different cursors, no acks.SubscriptionSeekTest.testConcurrentResetCursor(:412) /testConcurrentResetCursorByTimestamp(:462) — reset-vs-reset, exercising fencing only.HybridTypesAcknowledgeTest(added by [fix][broker]Fix incorrect backlog if use multiple acknowledge types on the same subscription #25047 itself;:139,:266-275) — ack-then-seek strictly sequentially. It validates the runnable's contents, never whether the runnable runs.- Repo-wide,
**/src/test/**has zero hits forcallbackGroup,alignAcknowledgeStatus, orRESET_CURSOR_IN_PROGRESS.
Out of scope, noted while reading (pre-dates #25047)
finalCallback.operationComplete() does an unconditional pendingMarkDeleteOps.clear() at :1693 without firing callbacks. Any entry queued between the flush and the reset's persist completion is discarded with its MarkDeleteCallback never invoked, leaving the corresponding broker-side ack future hung. Probably worth a separate look.
Are you willing to submit a PR?
- I'm willing to submit a PR!
Activity
[fix][ml] Prevent durable cursor reset from being silently discarded …
[fix][ml] Serialize durable cursor reset against concurrent acks unde…
[fix][ml] Serialize durable cursor reset against concurrent acks unde…
[fix][ml] Serialize durable cursor reset against concurrent acks unde…
[fix][ml] Serialize durable cursor reset against concurrent acks unde…
[fix][ml] Persist a queued cursor reset instead of the ack queued beh…