GeezerVison is showing github.com — [fix][broker] Preserve non-durable cursor ownership during cleanup by FlorentinDUBOIS · Pull Request #26925 · apache/pulsar
Conversation
Main Issue: #26924
Motivation
Non-durable removeConsumer currently removes a subscription from the topic map before a later executor continuation deletes its named cursor. During that gap a replacement subscription can obtain the cached cursor. The old cleanup then resolves and deletes by name, without proving that the topic still owns the original subscription instance.
The source ordering is visible at PersistentSubscription.removeConsumer, PersistentTopic.getNonDurableSubscription, and ManagedLedgerImpl cursor caching/deletion. This is a source-level race analysis; the contract regressions below exercise narrower retirement guarantees. The suspected lifecycle race has not yet been reproduced at runtime.
Modifications
- Keep the retiring non-durable subscription reserved until its cursor-deletion callback completes, using the ledger's existing creation lock and exact-instance map removal.
- Add a monitor-guarded terminal retirement state so a waiting consumer or a later reset cannot reopen the old subscription after a reset callback clears its ordinary fence.
- Serialize reset admission, fence CAS and publication of the in-progress reset future under the subscription monitor. A reset already admitted is allowed to finish before asynchronous retirement; no future is waited on under that monitor. Reset retirement preserves its cursor rather than deleting it.
- Reuse
asyncDeleteCursor, FutureUtil.supplySafely and the existing executor. Deletion callbacks only complete a future; map/statistics continuation runs via thenComposeAsync, outside the ledger callback's lock.
- Preserve existing name-only removal callers, durable cleanup behavior, and
closeCursor/resumeAfterFence fence operations. No dependencies, wire format or configuration changes.
Recovery limitation: an unexpected deletion error keeps the old subscription terminally fenced and reserved; no automatic recovery is added. Missing-cursor/missing-ledger completions are treated as already deleted. This behavior needs review and native failure-path tests.
Verifying this change
- Add two native contract regressions observed failing on the unpatched production code and passing after restoring the proposal, with identical tests and commands.
- Run the full
PersistentSubscriptionTest class: 6 tests passed, no failures or skips.
- Run
./gradlew quickCheck and ./gradlew sanityCheck: both passed locally with Temurin JDK 25.0.4.1+1.
- Reproduce the complete real-ledger cursor-reuse/deferred-deletion interleaving at runtime.
- Verify the remaining reset failure paths, conditional replacement and recovery after unexpected cursor-deletion errors.
The two tests cover distinct contracts through the existing Java fixture:
- A consumer queued behind an admitted reset, and a new consumer after that reset, cannot reopen the retired subscription instance.
- The topic retains the old subscription reservation until the captured cursor-deletion callback completes, then removes it. The ledger is mocked in this contract test; it does not reproduce actual cursor-cache reuse or physical cursor deletion.
Both tests failed at their contract assertions with the two production files restored to parent commit c1aaf961086380fbdd7bae734e2fed47e54f7e2a (2 tests, 2 failures, no skips). After restoring production commit 8e23ec454fe74ce2b416f6778505d8b90d1a6730, the unchanged tests passed (2 tests, no failures or skips). Both executions forced the test task to run, disabled the build cache and retries, and collected all failures:
./gradlew :pulsar-broker:test --rerun --no-build-cache \
--tests 'org.apache.pulsar.broker.service.persistent.PersistentSubscriptionTest.testNonDurableSubscription*' \
-PtestRetryCount=0 -PtestFailFast=false
./gradlew :pulsar-broker:test --rerun --no-build-cache \
--tests 'org.apache.pulsar.broker.service.persistent.PersistentSubscriptionTest' \
-PtestRetryCount=0 -PtestFailFast=false
./gradlew quickCheck
./gradlew sanityCheck
The normal pre-push hook also ran ./gradlew test successfully before publishing test commit ef7f48c34855d3293ca1f7f4037fd27922877c99.
The prior fork Pulsar CI and Flaky suite passed for production commit 8e23ec454fe74ce2b416f6778505d8b90d1a6730; those runs predate the new tests. CI for the added tests must be evaluated separately. The complete suspected lifecycle race has not yet been reproduced at runtime, and no client reconnect timeout is attributed to it by these contract tests. Recovery after an unexpected cursor-deletion failure remains an open limitation. These results do not establish merge readiness.
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 — changes asynchronous retirement ordering and serializes reset admission; no new executor or blocking future wait.
- 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.
Keep retired subscriptions reserved until cursor deletion completes,
then remove only the expected subscription instance. This prevents an
old name-based cleanup from deleting a cursor handed to a replacement.
Serialize reset admission with terminal retirement and let an admitted
reset finish asynchronously before cleanup. Reject consumers waiting
on the retired subscription without reopening it through a reset fence.
Refs: apache#26924
Signed-off-by: Florentin Dubois <[email protected]>
|
@lhotari I haven't been able to add and validate a dedicated regression test for this race because Codex and Claude have flagged and blocked those attempts through their security checks. The existing fork CI passes, but that does not replace a deterministic reproducer and a regression test that fails without the fix and passes with it.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Verify that consumers queued behind a reset cannot revive a retired
subscription, and that cursor deletion retains the subscription
reservation until its completion callback.
Exercise both contracts through the existing subscription fixture.
Refs: apache#26924
Signed-off-by: Florentin Dubois <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
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.
Reviewed head daf4104991dbd746d34aa7a58e0ee58000977f20. The previous mixed-durability ownership finding is resolved: durable admission checks the reservation under the ledger lock before opening a cursor.
Focused local validation passed the new real-ledger regression and both retirement-contract tests: 3 tests, no failures or skips. No actionable findings identified within the reviewed scope.
The unpatched baseline and complete original reconnect interleaving were not independently reproduced.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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. Thanks for working on this fix. I traced the original interleaving and the retirement now closes it: the old subscription stays in the topic map until asyncDeleteCursor has finished (under the ledger monitor that getNonDurableSubscription and asyncOpenCursor also use), so a same-name request in that window finds the retiring subscription and is rejected with a retriable SubscriptionFencedException instead of picking up the cached cursor, and the map entry is then removed by exact instance. The non-durable delete in ManagedLedgerImpl.asyncDeleteCursor completes synchronously and removes the cursor from cursors and activeCursors, addConsumerInternal checks the terminal flag before cursor.setActive(), so I could not find a path that leaks a cursor or re-activates a deleted one. The old blocking deleteCursor call on the executor is also gone. I ran the three new tests on this head and they pass; reverting the PersistentSubscription change makes all three fail, and neutralising the durable-admission guard makes the real-ledger test fail on the orphan-cursor assertion. Two small, non-blocking notes are inline.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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. The follow-up commit addresses all three earlier comments: the warning text is corrected, the exact-instance removal is now pinned by a test, and the unexpected delete-failure contract is documented and covered for both the asynchronous and synchronous failure paths.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Oct 28, 2026
This was referenced Nov 21, 2026
Reviewers
lhotari
lhotari approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|