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][broker] Fix Shared and Key_Shared consumer stalls during FLOW and unblock races by void-ptr974 · Pull Request #26797 · apache/pulsar

Conversation

@void-ptr974

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

Uh oh!

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

Copy link
Copy Markdown
Contributor

Motivation

A Shared or Key_Shared consumer on a persistent topic can stop receiving messages indefinitely when the per-consumer unacknowledged-message limit is enabled, even though the client has already sent flow-control permits.

This is not a Java Semaphore leak. Individual atomic or volatile field accesses did not provide a common synchronization boundary for the multi-field decisions and commits. The broker could choose where to route permits or decide to block a consumer, then commit that decision after another operation had changed the state on which it was based.

Race 1: permits are added after the deferred counter was drained

The following example uses a flow-control command carrying 100 permits. The race window is between reading the blocked state and adding the permits to the counter selected by that read.

Step Connection event-loop thread processing the flow-control command Acknowledgement-completion or redelivery thread Broker state after the step
Initial — — blockedConsumerOnUnackedMsgs = true
permitsReceivedWhileConsumerBlocked = 0
messagePermits = 0
pendingDispatcherFlowPermits = 0
1 Reads blockedConsumerOnUnackedMsgs = true, enters the blocked branch, and pauses before adding the 100 permits. — State is unchanged. The destination counter has already been chosen from the old blocked state.
2 — Makes the consumer eligible to resume, writes blockedConsumerOnUnackedMsgs = false, and drains permitsReceivedWhileConsumerBlocked. The drain returns 0, so no active permit is added. blockedConsumerOnUnackedMsgs = false
permitsReceivedWhileConsumerBlocked = 0
messagePermits = 0
pendingDispatcherFlowPermits = 0
3 Resumes in the blocked branch selected in step 1 and adds 100 to permitsReceivedWhileConsumerBlocked. That branch does not notify the dispatcher. — blockedConsumerOnUnackedMsgs = false
permitsReceivedWhileConsumerBlocked = 100
messagePermits = 0
pendingDispatcherFlowPermits = 0
Failure Later flow-control commands see an unblocked consumer and add their permits to messagePermits; they do not revisit the deferred counter. — The accepted 100 permits remain stranded. Neither messagePermits nor pendingDispatcherFlowPermits includes them, and the dispatcher was never notified about them.

Race 2: a stale decision publishes a blocked state

The race window is between deciding to block after increasing unackedMessages and publishing blockedConsumerOnUnackedMsgs = true.

Step Dispatch thread increasing the unacknowledged-message count Acknowledgement-completion or policy-update thread Broker state after the step
Initial — — unackedMessages is below the configured maximum.
blockedConsumerOnUnackedMsgs = false
1 Atomically increases unackedMessages to or above the maximum. The blocking condition evaluates to true, then the thread pauses before publishing the blocked state. — unackedMessages >= maximum
blockedConsumerOnUnackedMsgs = false
2a: acknowledgement variant — Atomically reduces unackedMessages to or below maximum / 2. The old reconciliation code reads blockedConsumerOnUnackedMsgs = false, concludes that there is nothing to unblock, and returns. The count now allows delivery.
blockedConsumerOnUnackedMsgs = false
2b: policy variant — Instead of step 2a, a policy update disables the maximum-unacknowledged-message limit. Before this change, no consumer-state reconciliation is performed before dispatch resumes. The policy now allows delivery.
blockedConsumerOnUnackedMsgs = false
3 Resumes and publishes blockedConsumerOnUnackedMsgs = true using the decision completed in step 1. — The consumer is marked blocked even though the current count or policy allows delivery.
Failure Delivery stops. If no later acknowledgement or redelivery occurs, nothing corrects the stale blocked state. — The consumer can remain blocked indefinitely.

Modifications

This change uses the existing per-consumer flowPermitAccountingLock to close both windows:

  • Flow-control routing and unblock transfer now hold the same lock. If flow-control routing runs first, unblock drains the newly deferred permits. If unblock runs first, flow-control routing re-evaluates the current state and routes the permits using that state instead of its earlier observation.
  • Block publication rechecks the current count and policy while holding the lock. An acknowledgement that crosses the resume threshold and every policy reconciliation must acquire that same lock even if the blocked flag still appears false. If publication runs first, reconciliation observes and clears it; if reconciliation runs first, publication rechecks the new count or policy and does not publish a stale blocked state.
  • Unblock moves deferred permits into both messagePermits and pendingDispatcherFlowPermits before publishing the unblocked state.
  • The critical sections mutate only consumer-local state. Dispatcher, subscription, and redelivery callbacks, blocking I/O, and future waits remain outside the lock. This preserves the existing dispatcher-monitor to flowPermitAccountingLock order without introducing the reverse order.
  • Every acknowledgement path reconciles the consumer that owns the pending acknowledgement, including transactional and cross-consumer acknowledgements.

An acknowledgement that remains above the resume threshold does not acquire flowPermitAccountingLock. Exclusive, Failover, and non-persistent flow-control paths are unchanged. There are no client-facing API, wire-protocol, configuration, dependency, or default-value changes.

Verification

The tests use deterministic pause points at both race boundaries instead of relying on probabilistic stress loops. They cover:

  • Flow-control routing racing with acknowledgement, full redelivery, and selective redelivery for both Shared and Key_Shared.
  • Resume-threshold acknowledgement and policy disablement racing with block publication.
  • An acknowledgement that remains above the resume threshold completing without waiting for concurrent block publication.
  • Transactional batch-index and cross-consumer acknowledgement.
  • Zero-permit notification, integer wrap, and dispatcher-removal accounting.
  • An admin policy update that resumes delivery without another acknowledgement.

The following checks passed:

./gradlew --no-parallel :pulsar-broker:test -PtestRetryCount=0 \
  --tests ConsumerBlockedPermitAccountingTest \
  --tests SharedDispatcherPermitAccountingTest \
  --tests ConsumerTest \
  --tests AdminApiMaxUnackedMessagesTest.testNamespacePolicy

./gradlew --no-parallel quickCheck

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
  • 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 void-ptr974 changed the title [fix][broker] Prevent stranded flow permits for blocked consumers [fix][broker] Fix consumers that can stop receiving messages after a FLOW/unblock race Oct 1, 2026
@void-ptr974
void-ptr974 marked this pull request as draft October 1, 2026 05:09
@void-ptr974
[fix][broker] Fix consumers that can stop receiving messages after a …
5ad35c6
…FLOW/unblock race

Serialize blocked credit routing and unblock transfer with the existing per-consumer accounting lock, and revalidate stale block transitions before publication.

Assisted-by: Codex (GPT-5)
@void-ptr974
void-ptr974 force-pushed the fix-consumer-blocked-flow-permit-race branch from e018a58 to 5ad35c6 Compare October 1, 2026 05:38
@void-ptr974
[fix][broker] Order unacked threshold crossing with block publication
c3ab3fa
Synchronize the ACK that crosses the consumer resume threshold with blocked-state publication, while keeping ACKs above the threshold on the lock-free path.

Assisted-by: Codex (GPT-5)
@void-ptr974 void-ptr974 changed the title [fix][broker] Fix consumers that can stop receiving messages after a FLOW/unblock race [fix][broker] Prevent flow-control races from indefinitely stalling Shared and Key_Shared consumers Oct 1, 2026
@void-ptr974
[fix][broker] Document unacked reconciliation contract
342919d
Clarify that low-level pending-ack and unacked counter operations do not reconcile blocked consumer state, so future ACK paths keep using the unified reconciliation helper.

Assisted-by: Codex (GPT-5)
@void-ptr974 void-ptr974 changed the title [fix][broker] Prevent flow-control races from indefinitely stalling Shared and Key_Shared consumers [fix][broker] Fix Shared and Key_Shared consumer stalls during FLOW and unblock races Oct 1, 2026
@void-ptr974
void-ptr974 marked this pull request as ready for review October 1, 2026 08:52
@void-ptr974 void-ptr974 mentioned this pull request Oct 1, 2026
[Bug] Shared and Key_Shared consumers can stop receiving when per-consumer unacked limits are enabled #26801
Open
3 tasks done
@lhotari

lhotari commented Oct 1, 2026

Copy link
Copy Markdown
Member

Thanks! Looks great at first sight!

All reactions

Sorry, something went wrong.

Uh oh!

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

lhotari
lhotari reviewed Oct 1, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

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.

Thank you for the detailed write-up and for turning both races into deterministic tests; the pause-point approach makes the failure scenarios easy to follow.

I traced every acquisition of flowPermitAccountingLock (Consumer.java:493, 1000, 1077, 1115, 1137, 1398) and found no reverse order: the dispatcher-side callers (internalConsumerFlow, removeConsumer, sendMessages) take the dispatcher monitor first, nothing inside the lock takes another monitor or calls back into the dispatcher or subscription (only plain policy-field reads), and consumerFlow is always invoked after release. An ack that stays above the resume threshold only does lock-free reads. Race 1 and Race 2 both look closed to me, the permit transfer adds equally to messagePermits and pendingDispatcherFlowPermits so the removal invariant and the signed wrap are preserved, and the Exclusive/Failover/non-persistent paths keep their lock-free behavior.

I also ran ConsumerBlockedPermitAccountingTest (13 tests, a few seconds) and mutated the fix away piece by piece: removing the Flow-side lock, the lock around block publication, the recheck under the lock, the threshold-crossing detection, and the PersistentTopic policy reconcile each makes a test fail. The one gap is the non-transactional partial-ack branch, which survives reverting (see the inline comment). The remaining comments are small cleanups.

One scoping note for the description: this does not touch the separate negative per-consumer messagePermits wedge tracked in #24926, so that issue should stay open.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java Resolved

Uh oh!

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

Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java Resolved

Uh oh!

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

Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java Resolved

Uh oh!

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

Comment thread pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java Resolved

Uh oh!

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

@lhotari lhotari added the release/5.0.1 label Oct 1, 2026
void-ptr974 added 2 commits October 2, 2026 14:48
@void-ptr974
Merge upstream master into PR 26797 review branch
18b44e6
@void-ptr974
[fix][broker] Address blocked FLOW accounting review feedback
ae0790d
Cover non-transactional partial ACK completion racing with block publication, remove the unused unblock wrapper, and avoid redundant blocked-state publication and zero-permit ACK notifications. Preserve dispatcher wakeup on real unblock and verify deferred permit accounting during removal.

Assisted-by: Codex
lhotari
lhotari approved these changes Oct 6, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

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. All four earlier points are addressed in ae0790d, with tests that pin the behavior.

I re-ran ConsumerBlockedPermitAccountingTest at this head and it passes. I then reverted the non-transactional partial-ack branch in applyPendingAckCompletions to an unsynchronized decrement plus reconcileBlockedConsumerState(false), and testNonTransactionalPartialAckWaitsForBlockStatePublication fails, so that path is now pinned. The remaining delta (test setup now going through real pending-ack state, the extra SharedDispatcherPermitAccountingTest removal case) reads as sound and I found no new problems.

Sorry, something went wrong.

Uh oh!

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

All reactions
@lhotari lhotari added release/4.2.6 release/4.0.15 labels Oct 6, 2026
@lhotari
lhotari merged commit 006431a into apache:master Oct 6, 2026
43 checks passed

Uh oh!

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

lhotari pushed a commit that referenced this pull request Oct 7, 2026
@void-ptr974 @lhotari
[fix][broker] Fix Shared and Key_Shared consumer stalls during FLOW a…
237d697
…nd unblock races (#26797)

(cherry picked from commit 006431a)
lhotari pushed a commit that referenced this pull request Oct 7, 2026
@void-ptr974 @lhotari
[fix][broker] Fix Shared and Key_Shared consumer stalls during FLOW a…
1f0f813
…nd unblock races (#26797)

(cherry picked from commit 006431a)
@lhotari lhotari added the cherry-picked/branch-4.2 label Oct 7, 2026
lhotari pushed a commit that referenced this pull request Oct 7, 2026
@void-ptr974 @lhotari
[fix][broker] Fix Shared and Key_Shared consumer stalls during FLOW a…
a302a5f
…nd unblock races (#26797)

(cherry picked from commit 006431a)
@lhotari lhotari added cherry-picked/branch-4.0 cherry-picked/branch-5.0 labels Oct 7, 2026
@nodece nodece mentioned this pull request Oct 13, 2026
[feat][ml] Port batch read and managed-ledger cache improvements (PIP-430) on BK 4.17.4.1 (ml-only subset) ascentstream/pulsar#106
Closed
This was referenced Oct 14, 2026
[fix][broker] Preserve unacked backpressure for stale redelivery requests Denovo1998/pulsar#35
Closed
[fix][broker] Preserve unacked backpressure for stale redelivery requests #26910
Merged
@nodece nodece mentioned this pull request Nov 5, 2026
[fix][ml][broker][client] Sync branch-as-4.0 with apache branch-4.0 (2026-09 to 2026-11, 159 commits) ascentstream/pulsar#109
Merged
4 of 5 tasks
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Nov 16, 2026
@void-ptr974 @nodece
[fix][broker] Fix Shared and Key_Shared consumer stalls during FLOW a…
33fcde1
…nd unblock races (apache#26797)

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

Reviewers

@lhotari lhotari lhotari 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.1

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

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

2 participants

@void-ptr974 @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.