GeezerVison is showing github.com — [Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues · Issue #22116 · apache/pulsar
[Bug] SnapshotSegmentAbortedTxnProcessorImpl contains thread safety issues #22116DescriptionSearch 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); |
|
} |
|
unsealedTxnIds.forEach(txnID -> aborts.put(txnID, txnID)); |
|
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!
Activitylhotari commented on Feb 26, 2024 nikam14 commented on Mar 28, 2024 @lhotari can you review above mentioned PR. lhotari commented on Jul 19, 2025 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. merlimat commented on Oct 22, 2026 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:
-
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.
-
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.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|