Repository navigation
[fix][txn] Fix PendingAckHandleImpl potential thread safety issue - #22166
Merged
merlimat merged 3 commits intoOct 23, 2026
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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.
🚀 New features to boost your workflow:
|
…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.
merlimat
approved these changes
Oct 22, 2026
merlimat
approved these changes
Oct 22, 2026
lhotari
pushed a commit
that referenced
this pull request
Oct 23, 2026
…2166) Co-authored-by: Matteo Merli <[email protected]> (cherry picked from commit 01b0c0a)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #21304
Motivation
PendingAckHandleImplkeeps its pending-ack state in maps that aren't thread safe (individualAckOfTransactionis a commons-collectionsLinkedMap, and its values areHashMaps), plus the plain fieldcumulativeAckOfTransaction. The intent is to change them only on the handle'sinternalPinnedExecutor. 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:
Here's the interleaving:
individualAckOfTransaction.computeIfAbsent(T2, ...).remove(T1)without the lock.The
LinkedMapcan 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
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 existingsynchronizedblocks stay, becausesyncBatchPositionAckSetForTransactionand friends are still called from other threads.getStatsissynchronized, since it readsindividualAckOfTransactionfrom admin threads.individualAckPositionsisvolatile, because the dispatcher and admin threads read it without the lock.cumulativeAckOfTransactionis non-null before using it. Another commit or abort can clear it while the abort mark is being written.internalIndividualAcknowledgeMessageandinternalAbortTxnare now private.This adopts @dao-jun's original change, merged with current master and extended to the paths added since.
Verifying this change
PendingAckHandleImplThreadingTestoccupies 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 existingorg.apache.pulsar.broker.transaction.pendingack.*tests pass.