GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][ml] Fix ManagedCursor missing active-cursor registration during concurrent state changes by void-ptr974 · Pull Request #27107 · apache/pulsar

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Oct 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Motivation

Before this fix, ManagedCursorImpl.setActive() first registers the cursor in ManagedLedgerImpl.activeCursors, then writes isActive = true. setInactive() first removes the registration, then writes isActive = false. The ledger's container monitor protects the registration changes, but the cursor's condition checks and flag writes are outside that monitor. The complete lifecycle transitions can therefore interleave.

Starting with the same cursor inactive (isActive = false, no active-cursor registration, alwaysInactive = false), two threads can execute:

Step Thread A Thread B isActive Registered in activeCursors
1 Calls setActive(), registers the cursor, releases the container monitor, then pauses before writing the flag. false Yes
2 Paused. Calls setActive() and still sees false. The ledger finds the existing registration, so it skips adding it again. B writes isActive = true and returns. true Yes
3 Paused. Calls setInactive(), removes the registration, writes isActive = false, and returns. false No
4 Resumes, writes isActive = true, and returns. true No

Both threads have finished, but the cursor reports active while its registration is absent. The registration was successfully added and subsequently removed; the delayed flag write from A leaves the mismatch. Later setActive() calls see isActive = true and skip ledger.activateCursor(), so repeated activation requests do not repair it. The missing registration excludes the cursor from active-reader cache tracking and can increase BookKeeper reads. The reproduced failure concerns active-cursor membership; it does not establish message loss.

Production reachability depends on the callers. Consumer addition and removal hold the same subscription monitor, so those operations alone do not establish this race. With cacheEvictionByExpectedReadCount=false, the backlog-check timer provides an independent activation without that monitor: it can register a caught-up Shared subscription and pause before flag publication, while a new consumer's addition activates it again and subsequent removal of the last consumer deactivates it. The timer can then publish the stale true flag. The current default is true, which skips this backlog-check path. This is source-level reachability; the deterministic tests force the scheduling, and no naturally occurring broker-level reproduction, production incident, or occurrence rate has been established.

There is also a separate permanent-inactive race. Starting with an active cursor:

  1. Thread A calls setAlwaysInactive(). Its call to setInactive() removes the registration and writes isActive = false; A pauses before setting alwaysInactive = true.
  2. Thread B calls setActive(), sees both flags still false, re-registers the cursor, writes isActive = true, and returns.
  3. A writes alwaysInactive = true and returns, leaving a permanently inactive cursor active and registered.

Modifications

  • Reuse the existing active-cursor container monitor for the condition checks, membership changes and activity flag updates.
  • Keep volatile fast paths for already-satisfied active/inactive requests, and recheck conditions under the monitor before changing state.
  • Protect the complete permanent-inactive transition with the same monitor.
  • Add deterministic lifecycle regressions and bounded concurrent progress coverage for both cache eviction modes.
  • Add a focused JMH benchmark for repeated requests and real transitions on cursors sharing a ledger.

Verifying this change

  • Make sure that the change passes the CI checks.

Local validation passed with retries disabled:

  • The deterministic regressions force both interleavings above in each cache eviction mode. All four cases fail at the activity-state assertions on unmodified 9cbb6d9b89 and pass with the fix.
  • Four additional cases verify that already-satisfied requests finish while another thread owns the container monitor.
  • Two progress cases run lifecycle changes alongside position updates, container queries and toString(); two existing cursor activity tests also pass (12 invocations total).
  • quickCheck, managed-ledger Checkstyle, and microbench compilation/Checkstyle pass.
  • Focused JMH runs completed with one and four threads. These local short runs check the repeated-request fast path; they do not establish production throughput or absence of contention regressions.
./gradlew :managed-ledger:test \
  --tests '*ManagedCursorLifecycleStateTest' \
  --tests '*ManagedCursorLifecycleProgressTest' \
  --tests '*ManagedCursorTest.testAlwaysInactive' \
  --tests '*ManagedCursorTest.testNonDurableCursorActive' \
  -PtestRetryCount=0 -PtestMaxParallelForks=1 quickCheck

./gradlew :microbench:compileJava :microbench:checkstyleMain

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 — cursor activity transitions share the existing ledger-level monitor; already-satisfied requests retain a volatile fast path.
  • 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.

All reactions
@void-ptr974
[fix][ml] Keep cursor activity consistent during concurrent transitions
2037597
Serialize cursor activity changes with the ledger's existing active-cursor monitor.
Preserve volatile fast paths for repeated requests and cover permanent deactivation.
Add deterministic regressions, bounded progress tests and a lifecycle JMH benchmark.

Assisted-by: OpenAI Codex
@void-ptr974 void-ptr974 changed the title [fix][ml] Keep cursor activity consistent during concurrent transitions [fix][ml] Fix ManagedCursor missing active-cursor registration during concurrent state changes Oct 25, 2026
@void-ptr974
[fix][ml] Serialize cursor activity changes with a private lock
cc265d0
Guard each cursor's activity transitions with its own lock while keeping
container synchronization inside ManagedLedgerImpl. This removes the
dependency on the monitor returned by getActiveCursors and avoids extending
the shared container lock to cursor-local flag publication.

Add progress coverage for two cursors sharing a ledger in both cache
eviction modes.

Assisted-by: OpenAI Codex
codelipenghui
codelipenghui approved these changes Oct 26, 2026
View reviewed changes

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

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 cc265d0. No actionable findings identified within the reviewed scope.

Both pre-fix lifecycle races were reproduced. All 14 focused test cases passed with the fix, including both cache eviction modes, and microbenchmark compilation passed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@merlimat merlimat added this to the 5.1.0 milestone Oct 26, 2026
@poorbarcode poorbarcode assigned void-ptr974 Oct 27, 2026
@poorbarcode poorbarcode added release/4.2.6 release/4.0.15 release/5.0.2 labels Oct 27, 2026
poorbarcode
poorbarcode approved these changes Oct 27, 2026
View reviewed changes
@poorbarcode
poorbarcode merged commit b02b041 into apache:master Oct 27, 2026
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

lhotari pushed a commit that referenced this pull request Oct 28, 2026
@void-ptr974 @lhotari
[fix][ml] Fix ManagedCursor missing active-cursor registration during…
d3be222
… concurrent state changes (#27107)

(cherry picked from commit b02b041)
@lhotari lhotari added the cherry-picked/branch-5.0 label Oct 28, 2026
This was referenced Oct 28, 2026
[fix][ml] Preserve cursor position updates during activation Denovo1998/pulsar#63
Open
[fix][ml] Preserve cursor position updates during activation #27205
Merged
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 cherry-picked/branch-4.0 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@void-ptr974 @lhotari
[fix][ml] Fix ManagedCursor missing active-cursor registration during…
294b0b7
… concurrent state changes (apache#27107)

(cherry picked from commit b02b041)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@void-ptr974 @lhotari
[fix][ml] Fix ManagedCursor missing active-cursor registration during…
dd6c966
… concurrent state changes (apache#27107)

(cherry picked from commit b02b041)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes
@poorbarcode poorbarcode poorbarcode approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@void-ptr974 void-ptr974

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@void-ptr974 @codelipenghui @poorbarcode @merlimat @lhotari
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.