GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][txn] Fix PendingAckHandleImpl potential thread safety issue by dao-jun · Pull Request #22166 · apache/pulsar

Conversation

@dao-jun

@dao-jun dao-jun commented Feb 29, 2024 •
edited by merlimat
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@dao-jun
fix thread safety
f9fc88b
@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 mentioned this pull request Feb 29, 2024
[fix][txn] Fix PendingAckHandleImpl potential thread safety issue dao-jun/pulsar#10
Closed
@dao-jun dao-jun added the ready-to-test 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 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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
@lhotari lhotari added the release/4.0.1 label Oct 11, 2024
@lhotari lhotari added release/4.0.2 and removed release/4.0.1 labels Dec 6, 2024
@lhotari lhotari added release/4.0.3 and removed release/4.0.2 labels Jan 6, 2025
@lhotari lhotari added release/4.0.4 and removed release/4.0.3 labels Feb 19, 2025
@lhotari lhotari added release/4.0.5 and removed release/4.0.4 labels Mar 31, 2025
@lhotari lhotari added release/4.0.6 and removed release/4.0.5 labels May 21, 2025
@lhotari lhotari added release/4.0.7 and removed release/4.0.6 labels Jul 30, 2025
@coderzc coderzc removed this from the 4.1.0 milestone Sep 1, 2025
@lhotari lhotari added release/4.2.3 release/4.0.12 and removed release/4.0.11 labels Jun 8, 2026
@lhotari lhotari added release/4.2.4 release/4.0.13 and removed release/4.2.3 release/4.0.12 labels Jun 30, 2026
@lhotari lhotari added release/4.2.5 release/4.0.14 and removed release/4.2.4 release/4.0.13 labels Jul 27, 2026
@lhotari lhotari added release/4.0.15 release/4.2.6 and removed release/4.0.14 release/4.2.5 labels Oct 14, 2026
merlimat added 2 commits October 22, 2026 16:08
@merlimat
Merge remote-tracking branch 'apache/master' into mmerli/adopt-22166-…
2043590
…pending-ack-thread-safety

# Conflicts:
#	pulsar-broker/src/main/java/org/apache/pulsar/broker/transaction/pendingack/impl/PendingAckHandleImpl.java
@merlimat
[fix][txn] Apply pending-ack state changes only on the pinned executor
425d942
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
@merlimat merlimat added the release/5.0.2 label Oct 22, 2026
merlimat
merlimat approved these changes Oct 22, 2026
View reviewed changes
merlimat
merlimat approved these changes Oct 22, 2026
View reviewed changes
@merlimat
merlimat merged commit 01b0c0a into apache:master Oct 23, 2026
44 checks passed

Uh oh!

There was an error while loading. Please reload this page.

lhotari pushed a commit that referenced this pull request Oct 23, 2026
@dao-jun @merlimat @lhotari
[fix][txn] Fix PendingAckHandleImpl potential thread safety issue (#2…
0767023
…2166)

Co-authored-by: Matteo Merli <[email protected]>
(cherry picked from commit 01b0c0a)
@lhotari lhotari added the cherry-picked/branch-5.0 label Oct 23, 2026
This was referenced Nov 21, 2026
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27680
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27681
Closed
[improve][ci] Run CI for the 4.2.6 backports to branch-4.2 (do not merge) #27684
Closed
@lhotari lhotari added cherry-picked/branch-4.2 cherry-picked/branch-4.0 labels Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@merlimat merlimat merlimat approved these changes
@lhotari lhotari Awaiting requested review from lhotari

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@dao-jun dao-jun

Labels

area/transaction cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 doc-not-needed Your PR changes do not impact docs ready-to-test release/4.0.15 release/4.2.6 release/5.0.2 type/bug The PR fixed a bug or issue reported a bug

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

[Task] Fix thread safety of PendingAckHandleImpl

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@dao-jun @codecov-commenter @merlimat @lhotari @coderzc
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.