Skip to content

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

Closed

Description

@lhotari

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

  • Broker: master (5.1.0-SNAPSHOT); the same code is in 5.0.x and 4.2.x (4.2.0 and later). 4.1.x and 4.0.x aren't affected.
  • Any OS and Java version: the race is in the managed ledger's cursor code.

Issue Description

Since the managed ledger moved to LightProto (4.2.0), ManagedCursorImpl.persistPositionToLedger builds and serializes a cursor's position in one PositionInfo per cursor, reusablePositionInfo, instead of a new one for each persist. It clears the instance, sets the position, adds the batch index acks, properties and individually deleted ranges, and serializes it with toByteArray(), without synchronization.

Two persists of the same cursor can run at the same time:

  • A mark-delete persists the position from internalMarkDelete on the thread that flushes the mark-deletes.
  • Closing the cursor (asyncClose → persistPositionWhenClosing) persists the last mark-delete position on the closing thread. The close path doesn't take the pendingMarkDeleteOps monitor that orders the mark-deletes, so it isn't ordered with a mark-delete in progress.

When they overlap, one can clear or change the shared PositionInfo while the other is building or serializing it. Then either:

  • A wrong position is written to the cursor ledger: the position or the individually deleted ranges of one persist, mixed with or replaced by those of the other. After the topic is loaded again, the cursor recovers from that entry. That can mean the acked messages are delivered again, or another cursor state than the last one written.
  • The serialization fails: toByteArray() throws IllegalStateException (below). The exception propagates out of persistPositionToLedger before the entry is added to the cursor ledger, so that persist's callback is never completed: the mark-delete or the close doesn't complete.

What should happen: each persist writes exactly the position it was asked to persist, and concurrent persists don't affect each other.

Error messages

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)
    at org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.persistPositionToLedger(ManagedCursorImpl.java)

Reproducing the issue

The overlap of a close and a mark-delete is a narrow window, so it's easiest to reproduce in a unit test that calls persistPositionToLedger from two threads with the same cursor:

  1. Create a ManagedCursorImpl and pass a mocked LedgerHandle whose asyncAddEntry records the bytes and completes the callback.
  2. From each of two threads, call persistPositionToLedger 20,000 times, each thread with its own position (for example ledger 1 and ledger 2) and its own properties.
  3. Parse every recorded entry as PositionInfo and check that it is one thread's position with that thread's properties.

Without synchronization, the test fails with the IllegalStateException above.

Additional information

A fix: build and serialize the reused PositionInfo under its monitor, taken before the cursor's read/write lock that building the ranges takes. No caller of persistPositionToLedger holds that lock, so the order is safe, and the monitor is uncontended except during a close. This keeps the reuse, which avoids allocating a PositionInfo for every persist.

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. added
    type/bug
    on Oct 16, 2026
  2. self-assigned this
    on Oct 16, 2026
  3. added a commit that references this issue on Oct 16, 2026
    a73bb75
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions