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][client] Fix ACK timeout backoff deadlocks during receive and redelivery by void-ptr974 · Pull Request #27065 · apache/pulsar

Conversation

@void-ptr974

@void-ptr974 void-ptr974 commented Oct 23, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Motivation

When ACK timeout is enabled and ackTimeoutRedeliveryBackoff is configured, consumers of persistent topics using Exclusive or Failover subscriptions can deadlock during ordinary message receive processing or explicit redelivery.

Two locks are involved:

  • T: the write lock of UnAckedMessageRedeliveryTracker.
  • C: the intrinsic monitor of the corresponding ConsumerImpl.

Before this change, once a message is due for redelivery after its ACK timeout and backoff delay, the timer invokes consumer callbacks while holding T. For Exclusive and Failover subscriptions, redeliverUnacknowledgedMessages(Set<MessageId>) delegates to full redelivery, which acquires C.

Consumer operations acquire the same locks in the opposite order:

  • Without a message listener, the receive() / receiveAsync() message-processing path runs through synchronized messageProcessed(). It holds C while trackMessage() reaches tracker.add(), which acquires T.
  • Explicit redeliverUnacknowledgedMessages() holds C while calling tracker.clear(), which also acquires T.

The deadlock forms when these operations overlap on the same consumer:

Step ACK timeout timer thread Consumer operation thread
1 Acquires T and selects due messages
2 Holds T Acquires C in messageProcessed() or explicit redelivery
3 Holds T Calls tracker.add() or tracker.clear() and waits for T, retaining C
4 Enters full consumer redelivery and waits for C, retaining T Holds C and waits for T

Both threads retain their first lock while waiting for the lock owned by the other, so neither can progress. This cycle can occur in a normal receive loop as well as during explicit redelivery. It is the same consumer/tracker lock inversion previously addressed in the non-backoff tracker by #10768.

Modifications

  • Collect and remove due message IDs under the tracker write lock.
  • Keep successor timer registration under the same lock so tracker shutdown can cancel the published timeout.
  • Release the tracker write lock before invoking both onAckTimeoutSend() and consumer redelivery, matching UnAckedMessageTracker.
  • Preserve the consumer monitor that protects reconnect and consumer-epoch consistency.

With this ordering, a timer thread waiting for C no longer holds T. The consumer operation can acquire T, finish its tracker update, and release C, allowing timeout redelivery to continue.

Verifying this change

  • Make sure that the change passes the CI checks.

Added deterministic regression coverage for both consumer callbacks and for receive processing / explicit redelivery on both Exclusive and Failover subscriptions.

ThreadMXBean confirms that timeout redelivery is actually waiting for the consumer monitor. While owning that monitor, the test probes the tracker lock with tryLock() before executing the consumer operation. The unfixed implementation fails at this bounded probe without leaving deadlocked test threads. The receive-processing cases call the real synchronized messageProcessed() method shared by receive() and receiveAsync(). Subsequent timer ticks must still progress.

Local validation with test retries disabled:

  • Full quickCheck passed.
  • All 22 targeted test invocations passed: 11 tracker invocations, 7 consumer deduplication invocations, and 4 broker ACK-timeout backoff invocations.
  • Against the unfixed tracker, all 5 new regression invocations failed at the expected held-lock assertions.
./gradlew :pulsar-client-original:test \
  --tests org.apache.pulsar.client.impl.UnAckedMessageRedeliveryTrackerTest \
  --tests org.apache.pulsar.client.impl.UnAckedMessageTrackerTest \
  --tests org.apache.pulsar.client.impl.UnAckedTopicMessageRedeliveryTrackerTest \
  quickCheck -PtestRetryCount=0 -PtestFailFast=false

./gradlew :pulsar-broker:test \
  --tests org.apache.pulsar.client.impl.ConsumerDedupPermitsUpdateTest \
  --tests org.apache.pulsar.client.impl.UnAcknowledgedMessagesTimeoutTest.testCheckUnAcknowledgedMessageRedeliveryTimer \
  -PtestRetryCount=0 -PtestFailFast=false

Does this pull request potentially affect one of the following parts:

  • Dependencies (add or upgrade a dependency)
  • The public API
  • The schema
  • The default values of configurations
  • The threading model — consumer callbacks run after releasing the tracker write lock; execution remains on the timer thread.
  • The binary protocol
  • The REST endpoints
  • The admin CLI options
  • The metrics
  • Anything that affects deployment

Sorry, something went wrong.

Uh oh!

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

All reactions
@void-ptr974
[fix][client] Prevent deadlock when ACK timeout backoff races with ex…
c101ca2
…plicit redelivery

Invoke ACK timeout callbacks after releasing the backoff tracker write lock
so timeout and explicit redelivery cannot invert tracker and consumer locks.

Add deterministic Exclusive and Failover monitor-contention tests and
verify that both consumer callbacks run outside the tracker write lock.

Assisted-by: OpenAI Codex
codelipenghui
codelipenghui approved these changes Oct 23, 2026
View reviewed changes

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Releasing the tracker write lock before onAckTimeoutSend() / redeliverUnacknowledgedMessages() makes the backoff tracker follow the same pattern UnAckedMessageTracker has used since #10768, which fixed the same lock-order inversion with the consumer monitor. Due IDs are still collected/removed and the next tick is still scheduled under the lock, so tracker state and close() cancellation stay consistent. The new interleaving (ack/clear between collection and redelivery) is the same one the non-backoff tracker already allows.

Verified locally on this head: the 3 focused test classes pass (7/7). Against the pre-PR tracker, testConcurrentExplicitRedelivery[Exclusive] and testConsumerCallbacksRunWithoutTrackerLock fail at the intended assertions without hanging, so the new tests do cover the regression.

A couple of non-blocking comments inline. Also, CI - Unit - Brokers - Client Impl failed on this run; please check/rerun it to confirm it's unrelated before merging.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-client/src/main/java/org/apache/pulsar/client/impl/UnAckedMessageRedeliveryTracker.java Resolved

Uh oh!

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

Comment thread ...-client/src/test/java/org/apache/pulsar/client/impl/UnAckedMessageRedeliveryTrackerTest.java Resolved

Uh oh!

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

void-ptr974 added 2 commits October 24, 2026 08:53
@void-ptr974
Merge origin/master into fix-ack-timeout-backoff-redelivery-deadlock
c71ce27
@void-ptr974
[test][client] Cover receive processing in ACK timeout deadlock regre…
5a820a3
…ssion

Exercise Exclusive and Failover consumers with both receive processing and
explicit redelivery while timeout redelivery waits for the consumer monitor.
Use persistent topics and the consumer tracking path so the regression models
reachable ACK-timeout behavior after merging master.

Document the common lock-order rule and use threadId in the Java 21 tests.

Assisted-by: Codex
@void-ptr974 void-ptr974 changed the title [fix][client] Prevent deadlock when ACK timeout backoff races with explicit redelivery [fix][client] Fix ACK timeout backoff deadlocks during receive and redelivery Oct 24, 2026
@void-ptr974

void-ptr974 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review and for pointing out the receive path.

The backoff tracker now releases its write lock before both consumer callbacks, applying #10768’s non-backoff fix to this path. Due-message updates and timer registration remain under the lock, preserving shutdown coordination.

I also added Exclusive/Failover receive-processing coverage, clarified the deadlock cycle in the title/description, and replaced both getId() calls with threadId().

Merged origin/master to include #27062, which sends pending ACKs before FLOW on reconnect and fixes the deduplication regression introduced by #21928. The merge also preserves #19602’s client-side redelivery counts for Exclusive/Failover.

quickCheck and 22 targeted test invocations pass; all 5 new regressions fail at the expected lock assertions against the unfixed tracker.

All reactions

Sorry, something went wrong.

Uh oh!

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

codelipenghui
codelipenghui approved these changes Oct 24, 2026
View reviewed changes

@codelipenghui codelipenghui left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on 5a820a3. Both previous comments are addressed, and the added receive-processing coverage exercises the real synchronized messageProcessed() path for Exclusive and Failover consumers.

Verified locally with retries disabled: all 22 focused cases passed (11 tracker, 7 deduplication/reconnect, and 4 broker ACK-timeout backoff cases). The unfixed tracker failed the new Exclusive/RECEIVE case at the expected lock-availability assertion; after restoring the fix, all four concurrency combinations passed again. This checks the unsafe locking condition without deliberately leaving threads deadlocked.

No further findings from the follow-up review. Please wait for the required CI checks before merging.

Sorry, something went wrong.

Uh oh!

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

All reactions
Denovo1998
Denovo1998 approved these changes Oct 27, 2026
View reviewed changes
@merlimat merlimat added this to the 5.1.0 milestone Nov 5, 2026
@merlimat merlimat added the release/5.0.2 label Nov 5, 2026
@merlimat
merlimat merged commit a72adf7 into apache:master Nov 5, 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 Nov 6, 2026
@void-ptr974 @lhotari
[fix][client] Fix ACK timeout backoff deadlocks during receive and re…
db6509d
…delivery (#27065)

(cherry picked from commit a72adf7)
@lhotari lhotari added the cherry-picked/branch-5.0 label Nov 6, 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 release/4.2.6 cherry-picked/branch-4.0 release/4.0.15 labels Nov 21, 2026
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@void-ptr974 @lhotari
[fix][client] Fix ACK timeout backoff deadlocks during receive and re…
521c902
…delivery (apache#27065)

(cherry picked from commit a72adf7)
poorbarcode pushed a commit to poorbarcode/pulsar that referenced this pull request Nov 26, 2026
@void-ptr974 @lhotari
[fix][client] Fix ACK timeout backoff deadlocks during receive and re…
7c06457
…delivery (apache#27065)

(cherry picked from commit a72adf7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@codelipenghui codelipenghui codelipenghui approved these changes
@Denovo1998 Denovo1998 Denovo1998 approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 cherry-picked/branch-5.0 release/4.0.15 release/4.2.6 release/5.0.2

Projects

None yet

Milestone

5.1.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

5 participants

@void-ptr974 @codelipenghui @Denovo1998 @merlimat @lhotari
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.