GeezerVison is showing github.com — [fix][broker] Clear pending read flags when a read fails before the cursor takes ownership by nodece · Pull Request #27059 · apache/pulsar
Conversation
nodece
commented
Oct 23, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Related to #26454
Motivation
A Shared (multiple-consumers) subscription can stall forever when a synchronous Throwable escapes
readMoreEntries() after havePendingRead/havePendingReplayRead is set but before the read
is submitted to the managed cursor. The exception is only logged by the event-loop executor
(A task raised an exception), no completion callback is ever registered, and nothing clears the
flag: readMoreEntries refuses to run while it is set and checkAndUnblockIfStuck requires
!havePendingRead, leaving a topic unload as the only recovery.
Observed in production on 3.0.14: 10 out of 132 dispatchers were permanently stuck with the
signature havePendingRead=true, waitingReadOp=null, pendingReadOps=0 and a pristine read-failure
backoff (proving readEntriesFailed never ran); the stuck dispatchers clustered around consumer
re-attach batches. Reproduced on a real standalone 3.0.14 broker by injecting a one-shot
OutOfMemoryError while the read was being scheduled:
WARN io.netty.util.concurrent.AbstractEventExecutor - A task raised an exception.
Task: PersistentDispatcherMultipleConsumers$$Lambda...
java.lang.OutOfMemoryError: ...
at ...PersistentDispatcherMultipleConsumers.readMoreEntries(...)
at ...PersistentDispatcherMultipleConsumers.lambda$readMoreEntriesAsync$4(...)
...
After the fault, new consumers and new publishes deliver nothing (backlog frozen, msgOutCounter=0);
unload recovers. The same unprotected path exists on this branch and on the 3.0.x / 4.x / 5.0
branches, so a backport is recommended.
Modifications
In PersistentDispatcherMultipleConsumers:
- Wrap the normal-read and replay-read scheduling sections - from
havePendingRead/havePendingReplayRead = true until the read is submitted to the managed
cursor - in try/catch (Throwable). On failure the flag is cleared and the read is retried with
backoff instead of being left set forever.
- Extract the shared failure handling into
handleReadSchedulingFailure(Throwable, ReadType), which
also cancels a possibly registered wait-read so that the cleared flag cannot leave the cursor
waiting.
- Move the flag reset and
onReadEnded to the top of readEntriesFailed, so an exception raised
later in that callback can no longer strand the flag.
Verifying this change
- Make sure that the change passes the CI checks.
This change added tests and can be verified as follows:
New PersistentDispatcherMultipleConsumersStuckReadTest (6 tests, following the existing
PersistentDispatcherSingleActiveConsumerStuckReadTest pattern):
- a read failure callback interrupted mid-way still clears the flag;
- normal failure handling still registers the tail-wait read again and later messages are delivered;
- the interrupted failure is recovered by
checkAndUnblockIfStuck;
- a read scheduling failure is retried and the backlog is delivered;
- a replay scheduling failure is retried and the entry is redelivered;
- a scheduling failure after the wait-read was registered cancels it and the retry
re-registers it, after which publishes are delivered.
Verified: on unpatched master testReadSchedulingFailureIsRetriedAndDelivers fails (nothing is
delivered within 10s); with this patch all 6 tests pass. On the real standalone 3.0.14 broker
mentioned above, the fault now turns into a logged Failed to schedule read of messages followed by
a successful retry, and delivery continues.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for tracking this down. Clearing the flag when a read fails between setting havePendingRead/havePendingReplayRead and handing the read to the cursor fixes a real liveness problem: nothing else ever clears it, and checkAndUnblockIfStuck can't detect it. For failures that happen before the cursor takes the read, the change looks correct to me. I ran the new PersistentDispatcherMultipleConsumersStuckReadTest (4/4 pass) and the existing PersistentDispatcherMultipleConsumersTest (17/17 pass) locally.
My inline comments are mostly about what the catch can and can't guarantee once the cursor has already taken the read, plus two branches that have no tests.
About the description: Fixes #26454 would close the issue, but the 4.0.12 report and the heap-dump report there only match the stuck-state signature, and the reproduction here injects an OOM. Unless the production logs from the time of a stall show the escaped Throwable (e.g. Netty's A task raised an exception from readMoreEntries), I'd suggest Related to #26454 and keeping the issue open.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…effort recovery, extend tests
- Call onReadEnded only when onReadStarted was reached, by passing a
readStarted flag into handleReadSchedulingFailure: a failure from
updateMinReplayedPosition no longer notifies an unpaired read end,
which PersistentStickyKeyDispatcherMultipleConsumers uses to account
replay reads in flight.
- Document that the recovery is best-effort: the failure may also come
from inside the managed cursor after it already took over the read,
where cursor-side state (such as the pending read operations counter)
can still be left behind until a follow-up fix in the managed ledger.
- Add tests for the replay scheduling failure (redelivery resumes after
the retry) and for a failure that happens after the wait-read was
registered (the registered read is cancelled, the retry re-registers
it and later publishes are delivered), and assert
cursor.getPendingReadOpsCount() == 0 after recovery.
Assisted-by: Claude Code
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.
Thanks for adding the extra regression coverage! All six new tests and the 17 existing dispatcher tests passed locally with retries disabled. I have one small suggestion below to strengthen the wait-read cancellation test.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for the quick update. The production-side changes look good to me: onReadEnded is now paired with onReadStarted, the javadoc calls the recovery best-effort with the cursor-side fix tracked separately, and the description says Related to #26454. I ran PersistentDispatcherMultipleConsumersStuckReadTest locally at e946315 and all 6 tests pass.
Two of the new scheduling-failure tests don't guard the branches they target yet, though. Details inline:
- the replay test's injected failure never fires;
- the wait-read test passes without the fix.
testReadSchedulingFailureIsRetriedAndDelivers is currently the only scheduling-failure test that would fail on the unpatched dispatcher.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for addressing the review feedback. Reviewed 6cf6ce8: the replay test now exercises the injected failure and verifies retry scheduling, and the wait-read test verifies cancellation and a second read before publishing. All six focused regression tests passed locally with retries disabled. No remaining actionable findings within the reviewed scope. LGTM.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
… fail without the fix
Disable delayed delivery in the fixture so the replay path goes through
asyncReplayEntries, the method the test fails, instead of
asyncReplayEntriesInOrder which bypasses it. Verify the replay failure
handler ran before checking redelivery, and in the wait-read test verify
the registered read was cancelled and a second read scheduled before
publishing, so the delivery cannot come from the original wait-read. Both
tests now fail on the unpatched dispatcher.
Assisted-by: Claude Code
Uh oh!
There was an error while loading. Please reload this page.
dlg99
added a commit
to datastax/pulsar
that referenced
this pull request
Nov 2, 2026
…ursor takes ownership (#863)
* [fix][broker] Clear pending read flags when a read fails before the cursor takes ownership OSS#27059
Re-working OSS PR apache#27059 from 4.0
to Luna 3.1
---------
Co-authored-by: Andrey Yegorov <[email protected]>
nodece
added a commit
to ascentstream/pulsar
that referenced
this pull request
Nov 5, 2026
nodece
added a commit
to ascentstream/pulsar
that referenced
this pull request
Nov 5, 2026
nodece
added a commit
to ascentstream/pulsar
that referenced
this pull request
Nov 5, 2026
This was referenced Nov 21, 2026
Uh oh!
There was an error while loading. Please reload this page.
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|