Skip to content

[fix][ml] Serialize a cursor's reused position info under its monitor - #26944

Merged
merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-fix-ml-position-info-race-master
Oct 16, 2026
Merged

merlimat merged 1 commit into
apache:masterfrom
lhotari:lh-fix-ml-position-info-race-master

Conversation

@lhotari

@lhotari lhotari commented Oct 16, 2026

Copy link
Copy Markdown
Member

Fixes #26943

Motivation

ManagedCursorImpl.persistPositionToLedger builds and serializes a cursor's position with one reused PositionInfo per cursor (reusablePositionInfo), without synchronization. Two persists of the same cursor can run at the same time: a cursor's close persists its position (asyncClose → persistPositionWhenClosing) on the closing thread, and that can overlap with a mark-delete persisting (internalMarkDelete) on another thread, since the close path doesn't take the pendingMarkDeleteOps monitor that orders the mark-deletes.

One persist can then clear or change the position info while the other builds or serializes it. The result is either a wrong position written to the cursor ledger, or an exception before the entry is added, so that the persist's callback never completes:

java.lang.IllegalStateException: Some required fields are missing
    at org.apache.bookkeeper.mledger.proto.PositionInfo.checkRequiredFields(PositionInfo.java:568)
    at org.apache.bookkeeper.mledger.proto.PositionInfo.toByteArray(PositionInfo.java:735)

or, when the position has properties:

java.lang.NullPointerException: Cannot invoke "org.apache.bookkeeper.mledger.proto.LongProperty._clearAndRelease()" because "this.properties[i]" is null
java.lang.NullPointerException: Cannot invoke "org.apache.bookkeeper.mledger.proto.LongProperty.getSerializedSize()" because "_item" is null

Modifications

  • The position info is filled and serialized under its monitor, in serializePositionInfo.
  • The cursor's state that goes into it (batch index acks, properties, individually deleted ranges) is read before the monitor is taken, under the cursor's read/write lock as before, so no lock is taken while the monitor is held.
  • The monitor is uncontended except when persists overlap, as during a close. The position info is still reused, so a persist doesn't allocate one.

Verifying this change

  • ManagedCursorPersistPositionConcurrencyTest persists 20,000 positions from each of two threads with the same cursor and a mocked ledger handle, each thread with its own position and properties. It checks that no persist fails, that every written position info is one thread's position with that thread's properties, and that the written positions are exactly the persisted ones, so no persist wrote another's. Without the change, it fails with the NullPointerExceptions above. With it, it passes.
  • ManagedCursorTest (216) and ManagedCursorIndividualDeletedMessagesTest pass.

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
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

This change was developed with the assistance of Claude Code.

ManagedCursorImpl.persistPositionToLedger builds and serializes a cursor's position with one reused
PositionInfo per cursor. A cursor's close persists its position on the closing thread, which can overlap with
a mark-delete persisting on another thread, since the close path doesn't take the pendingMarkDeleteOps monitor
that orders the mark-deletes. One could clear or change the position info while the other built or serialized
it, writing a wrong position to the cursor ledger or failing with an exception before the entry was added.

Fill and serialize the position info under its monitor. The cursor's state that goes into it is read before,
under the cursor's lock, so that no lock is taken while the monitor is held.

Fixes apache#26943

Assisted-by: Claude Code (claude-opus-5-5)

@void-ptr974 void-ptr974 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The synchronization scope looks appropriate, and the test covers the reported race. Thanks for the clear and focused fix.

@merlimat
merlimat merged commit ce0583b into apache:master Oct 16, 2026
44 checks passed
lhotari added a commit that referenced this pull request Oct 16, 2026
lhotari added a commit that referenced this pull request Oct 16, 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.

[Bug] Concurrent persists of a cursor's position can write a wrong position or fail, since they share one PositionInfo

3 participants