Skip to content

[Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues #22116

Open

Description

@lhotari

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:

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


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
    MemberAuthor

    related to #21304 and #22119

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

    @nikam14
    Contributor

    @lhotari can you review above mentioned PR.

  4. lhotari commented on Jul 19, 2025

    @lhotari
    MemberAuthor

    Some more issues here:

    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. removed their assignment
    on Jul 20, 2025
  6. merlimat commented on Oct 22, 2026

    @merlimat
    Contributor

    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