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 — [Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues · Issue #22116 · apache/pulsar

[Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues #22116

New issue
Copy link
New issue
Copy link
Open
Open
[Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues#22116
Copy link

Description

@lhotari
lhotari
opened on Feb 26, 2024
Issue body actions

Search before asking

  • I searched in the issues and found nothing similar.

Version

master

Minimal reproduce step

There are several thread safety issues in SnapshotSegmentAbortedTxnProcessorImpl class.
examples:

pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java

Lines 374 to 376 in 529e1ab

if (indexes.size() != 0) {
persistentWorker.sequenceID.set(indexes.get(indexes.lastKey()).sequenceID + 1);
}

pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java

Line 383 in 529e1ab

unsealedTxnIds.forEach(txnID -> aborts.put(txnID, txnID));

pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java

Line 744 in 529e1ab

indexes.put(abortedMarkerPersistentPosition, index);

What did you expect to see?

Multi-threading code should be thread safe.

What did you see instead?

Non thread-safe code.

Anything else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

  1. lhotari commented on Feb 26, 2024

    @lhotari
    lhotari
    on Feb 26, 2024
    MemberAuthor
    More actions

    related to #21304 and #22119

  2. dao-jun
    self-assigned this
    on Feb 29, 2024
  3. nikam14 commented on Mar 28, 2024

    @nikam14
    nikam14
    on Mar 28, 2024
    Contributor
    More actions

    @lhotari can you review above mentioned PR.

  4. lhotari commented on Jul 19, 2025

    @lhotari
    lhotari
    on Jul 19, 2025
    MemberAuthor
    More actions

    Some more issues here:

    pulsar/pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/buffer/impl/SnapshotSegmentAbortedTxnProcessorImpl.java

    Lines 170 to 171 in 1b7e4a7

    persistentWorker.appendTask(PersistentWorker.OperationType.WriteSegment,
    () -> persistentWorker.takeSnapshotSegmentAsync(abortedSegment, position));

    persistentWorker.takeSnapshotSegmentAsync(abortedSegment, position) gets called on another thread (transactionExecutorProvider).

    It would be useful to have clear consistency boundaries in the SnapshotSegmentAbortedTxnProcessorImpl implementation since breaking thread safety rules will lead to hard-to-debug issues.

  5. dao-jun
    removed their assignment
    on Jul 20, 2025
  6. merlimat commented on Oct 22, 2026

    @merlimat
    merlimat
    on Oct 22, 2026
    Contributor
    More actions

    Status on current master (bcac33d), from the triage of #22384 (now closed):

    On master, every read and write of aborts, segmentIndex and unsealedTxnIds from the transaction buffer now runs under the TopicTransactionBuffer monitor: the marker callbacks, isTxnAborted, handleTransactionMessage and the timer snapshot. The recovery code linked above was rewritten into doRecoverFromSnapshot in #26335. Two races remain in SnapshotSegmentAbortedTxnProcessorImpl, which is only used with transactionBufferSegmentedSnapshotEnabled=true:

    1. TopicTransactionBuffer.getStats → generateSnapshotStats takes no lock. It iterates aborts, segmentIndex and indexes while the managed-ledger callback thread mutates them (under the buffer monitor). The likely result is a ConcurrentModificationException or inconsistent numbers from the topic stats admin call.

    2. PersistentWorker.updateIndexWhenExecuteTheLatestTask reads unsealedTxnIds on the segment writer's completion thread without the buffer monitor.

      • Interleaving: thread A runs putAbortedTxnAndPosition → unsealedTxnIds.add(T) under the buffer monitor, while thread W (completing writeSnapshotSegmentAsync) iterates the same LinkedList in convertTypeToTxnIDData. W throws a ConcurrentModificationException, the segment task fails after indexes.put, sequenceID.getAndIncrement() in takeSnapshotSegmentAsync is skipped, and the task stays at the head of the queue until the next append retries it.
      • this.unsealedTxnIds = new LinkedList<>() is a plain write that comes after taskQueue.add in putAbortedTxnAndPosition. In theory W could see the new empty list and still see taskQueue.size() == 1. It would then persist an index whose maxReadPosition is past the last sealed abort, without that sealed segment's IDs. This reordering is legal under the JMM but unlikely in practice.

    A fix should take a consistent copy of unsealedTxnIds (and of maxReadPosition) under the buffer monitor, or hand it over explicitly with the task. generateSnapshotStats should run under the same monitor. A concurrency test that runs aborts while segments are being written and stats are being polled would be needed to land it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.