GeezerVison is showing github.com — [fix][txn] Fix PendingAckHandleImpl potential thread safety issue by dao-jun · Pull Request #22166 · apache/pulsar
Conversation
dao-jun
commented
Feb 29, 2024
•
edited by merlimat
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #21304
Motivation
PendingAckHandleImpl keeps its pending-ack state in maps that aren't thread safe (individualAckOfTransaction is a commons-collections LinkedMap, and its values are HashMaps), plus the plain field cumulativeAckOfTransaction. The intent is to change them only on the handle's internalPinnedExecutor. But the callbacks of the pending-ack store and the cursor run on whichever thread completes the future. Some of them lock the handle while doing so (blocking BookKeeper and metadata-store threads), and some don't lock at all.
#25528 made this worse. Commit now waits for the cursor ack, and the committed transaction is removed when that ack completes, on the cursor's mark-delete thread and without the lock:
return persistentSubscription.acknowledgeMessageAsync(new ArrayList<>(currentTxn.values()),
AckType.Individual, properties).thenRun(() -> {
individualAckOfTransaction.remove(txnID);
});
Here's the interleaving:
- T1 commits, and the cursor ack advances mark-delete asynchronously.
- T2's append callback holds the lock and runs
individualAckOfTransaction.computeIfAbsent(T2, ...).
- At the same time, the cursor thread runs
remove(T1) without the lock.
The LinkedMap can lose T2's entry. When T2 commits, containsKey(T2) is false, so the commit completes without acking the cursor: the committed acks are lost and the messages get redelivered. The metadata-store pending-ack store from PIP-473 goes through the same handle, so it has the same problem.
Modifications
- Every store and cursor callback that reads or changes the pending-ack state now runs on
internalPinnedExecutor. This covers individual and cumulative acks, commit and abort marks, and the work after the cursor ack, including the removal of the committed transaction and the low-watermark check. The existing synchronized blocks stay, because syncBatchPositionAckSetForTransaction and friends are still called from other threads.
getStats is synchronized, since it reads individualAckOfTransaction from admin threads.
individualAckPositions is volatile, because the dispatcher and admin threads read it without the lock.
- Cumulative abort checks that
cumulativeAckOfTransaction is non-null before using it. Another commit or abort can clear it while the abort mark is being written.
internalIndividualAcknowledgeMessage and internalAbortTxn are now private.
This adopts @dao-jun's original change, merged with current master and extended to the paths added since.
Verifying this change
PendingAckHandleImplThreadingTest occupies the pinned executor, then completes the store's append future and the cursor's ack future from another thread. It asserts that the state changes only once the pinned executor runs again. Both tests fail without the fix. The existing org.apache.pulsar.broker.transaction.pendingack.* tests pass.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.57%. Comparing base (bbc6224) to head (f9fc88b).
⚠️ Report is 2820 commits behind head on master.
Additional details and impacted files

@@ Coverage Diff @@
## master #22166 +/- ##
============================================
- Coverage 73.57% 73.57% -0.01%
- Complexity 32624 32638 +14
============================================
Files 1877 1877
Lines 139502 139577 +75
Branches 15299 15316 +17
============================================
+ Hits 102638 102690 +52
+ Misses 28908 28906 -2
- Partials 7956 7981 +25
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 56 files with indirect coverage changes
🚀 New features to boost your workflow:
- ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…pending-ack-thread-safety
# Conflicts:
# pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/pendingack/impl/PendingAckHandleImpl.java
Re-apply the change on top of master and extend it to the paths added
since: the pending-ack store, commit/abort mark and cursor ack callbacks
now hop back to internalPinnedExecutor before touching
individualAckOfTransaction or cumulativeAckOfTransaction. In particular,
the removal of a committed transaction after the cursor ack (apache#25528) ran
on the cursor's thread without the lock, racing with other transactions'
updates of the non-thread-safe LinkedMap.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 21, 2026
Reviewers
merlimat
merlimat approved these changes
lhotari
Awaiting requested review from lhotari
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|