Skip to content

[fix][txn] Fix PendingAckHandleImpl potential thread safety issue - #22166

Merged
merlimat merged 3 commits into
apache:masterfrom
dao-jun:fix/pendingackhandle_thread_safety
Oct 23, 2026
Merged

merlimat merged 3 commits into
apache:masterfrom
dao-jun:fix/pendingackhandle_thread_safety

Conversation

@dao-jun

@dao-jun dao-jun commented Feb 29, 2024 •

Copy link
Copy Markdown
Member

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:

  1. T1 commits, and the cursor ack advances mark-delete asynchronously.
  2. T2's append callback holds the lock and runs individualAckOfTransaction.computeIfAbsent(T2, ...).
  3. 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.

@dao-jun
dao-jun requested a review from lhotari February 29, 2024 14:54
@dao-jun dao-jun self-assigned this Feb 29, 2024
@dao-jun dao-jun added type/bug The PR fixed a bug or issue reported a bug area/transaction labels Feb 29, 2024
@github-actions github-actions Bot added the doc-not-needed Your PR changes do not impact docs label Feb 29, 2024
@dao-jun dao-jun closed this Feb 29, 2024
@dao-jun dao-jun reopened this Feb 29, 2024
@codecov-commenter

codecov-commenter commented Mar 1, 2024 •

Copy link
Copy Markdown

Codecov Report

✅ 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

Impacted file tree graph

@@             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     
Flag Coverage Δ
inttests 26.64% <71.42%> (+2.06%) ⬆️
systests 24.26% <0.00%> (-0.06%) ⬇️
unittests 72.83% <100.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...nsaction/pendingack/impl/PendingAckHandleImpl.java 70.84% <100.00%> (ø)

... 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.

@dao-jun dao-jun added this to the 3.3.0 milestone Mar 3, 2024
@coderzc coderzc modified the milestones: 3.3.0, 3.4.0 May 8, 2024
@lhotari lhotari modified the milestones: 4.0.0, 4.1.0 Oct 11, 2024
@coderzc coderzc removed this from the 4.1.0 milestone Sep 1, 2025
…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 merlimat modified the milestones: 5.0.0-M1, 5.1.0 Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Task] Fix thread safety of PendingAckHandleImpl

5 participants