Repository navigation
fix(pipeline): abort batch flow on stop sending - #26787
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep review completed on exact head 909b587f9d339f90a38f2944a94bd3f6875ce38f, base main, merge-base 47c8302f083b61c3b4a92a0ea539a28ef86d33c2.
P1 — StopSending is lost when it overtakes lifecycle registration
abortPipelineBatchFlow returns immediately when (session, stream ID) is not yet in pipelineStreamLifecycles (pkg/sql/compile/remoterun_batch_flow.go:210-216). That miss is a reachable ordering, not an unknown-stream case: pkg/cnservice/server.go starts every admitted pipeline message in an independent goroutine, so the later StopSending handler can run before the original PipelineMessage/PrepareDoneNotify handler reaches registerPipelineStreamLifecycle.
The concrete dispatch interleaving is:
PrepareDoneNotifyMessageis admitted and its handler goroutine is delayed before lifecycle registration.- The later StopSending goroutine runs first.
CancelPipelineSendingcreates its existingalreadyDonetombstone, whileabortPipelineBatchFlowsees no lifecycle and drops the abort. - The original handler then registers a fresh, non-aborted batch flow.
RecordDispatchPipelineintentionally treats the pre-registration colexec tombstone as stale, deletes it, and continues; this preserves the established #23153 rule that StopSending must not cancel a dispatch receiver through that map. - The client has stopped consuming, but this new flow can still retain outstanding batches or block on exhausted credits. Its reserve/drain wait never observes the lost StopSending and is released only by the connection/30-second teardown timeout—the failure this PR is intended to remove.
Please give StopSending and lifecycle publication one linearization owner: either retain a bounded/session-cleaned pending abort that registration atomically consumes, or publish the lifecycle before a later stream message can overtake it. Do not route this through dispatch ReceiverDone, which would reintroduce #23153. Add a deterministic regression with the real lifecycle registry and dispatch registration in the exact order StopSending -> lifecycle registration -> outstanding batch/drain, and assert prompt release plus cleanup.
The new flow-local state is otherwise coherent: abort is idempotent/first-cause, clears bounded accounting, wakes both waiter classes, rejects later reservations, and makes late ACK/rollback harmless. The added mutex checks do not add material work beyond locks already taken on the credit path.
Validation on the exact head:
- both added tests enumerated and passed with
-race -count=100(T=0,B=30s,N=100); - full
pkg/sql/compilenormal and race suites passed; - dependent colexec dispatch/StopSending contract tests passed;
go build,go vet, targeted golangci-lint (0 issues), andgit diff --checkpassed.
The current tests begin only after a lifecycle is already registered, so they cannot expose the missing generation transition above.
|
Addressed the P1 registration race in cda5570. The fix uses the existing session-cleaned colexec cancellation tombstone as the pending StopSending owner: StopSending publishes the tombstone before looking up the lifecycle, while lifecycle registration publishes first and then checks the tombstone. This closes both orderings without routing cancellation through dispatch ReceiverDone. Added a deterministic regression for StopSending -> lifecycle registration -> outstanding batch/drain, including prompt interruption, accounting cleanup, harmless late ACK, and unchanged dispatch tombstone cleanup semantics. Validation: affected package build/vet/lint passed; focused normal tests passed; each modified concurrency test passed with -race -count=100; full pkg/sql/compile and pkg/sql/colexec suites passed normally and under -race. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-review completed on exact head cda55704fc9826a50faca4741b474f2287c76ef7, base main, merge-base 47c8302f083b61c3b4a92a0ea539a28ef86d33c2.
The previous P1 is resolved. The handshake now has a valid two-sided linearization argument:
- StopSending publishes the session-owned colexec tombstone before looking up/aborting the lifecycle.
- Lifecycle registration publishes the lifecycle before checking that tombstone.
- Therefore, if Stop wins the colexec-map order, registration observes the tombstone; if registration wins, Stop observes the lifecycle. Interleavings between the check and RecordBuiltPipeline/RecordDispatchPipeline are also covered by the Stop-side lifecycle lookup.
- Session cleanup can erase a tombstone only after the connection context is done, which independently terminates reserve/drain and handler waits.
Q1/Q2/Q3 audit:
| Gate | Result |
|---|---|
| Q1 ownership | The original stream handler remains the single lifecycle removal/cleaned owner; abort is idempotent, first-cause, and does not compete for removal. The colexec tombstone remains session/original-handler cleaned. |
| Q2 waits | abort clears accounting and closes changed, releasing both credit reservation and terminal drain waiters. Handler completion then reaches terminal response and FIN/connection cleanup. No new lock inversion or wait cycle was found. |
| Q3 bounds | Pending accounting remains bounded by negotiated credits (max 8 batches / 64 MiB) and is cleared on abort. No new growing registry or per-message background worker was added. |
Compatibility and performance are preserved: dispatch ReceiverDone semantics remain unchanged, late ACK/rollback are harmless only after the flow is terminal, old/non-negotiated flows keep the nil-flow behavior, and the only healthy registration-path cost is one existing-registry lock/map lookup per negotiated stream—not per batch/row.
Validation on the exact head:
- added and dependent ACK/StopSending/dispatch tests passed normally;
- modified concurrency tests passed with
-race -count=100; - full
pkg/sql/compile,pkg/sql/colexec, andpkg/sql/colexec/dispatchnormal and race suites passed; go list,go build,go vet, andgit diff --checkpassed.
No blocking findings remain.
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#26746 ## What this PR does / why we need it: The client sends a batch ACK before StopSending, but the server can process those messages concurrently and observe StopSending first. StopSending canceled query execution without terminating the negotiated batch-flow state, so the original handler could remain blocked in waitUntilDrained until the 30-second connection timeout. This change adds an idempotent aborted state to pipelineBatchFlow. StopSending now aborts the matching flow after canceling execution, releases outstanding batch accounting, wakes credit and drain waiters, rejects new reservations, and treats an already-flushed late ACK as harmless. It also closes the pre-registration race called out in review. StopSending first publishes the existing session-cleaned colexec cancellation tombstone and then checks the lifecycle; lifecycle registration publishes the lifecycle and then checks that tombstone. This publish-then-check handshake guarantees that either side observes the other without changing dispatch ReceiverDone semantics from matrixorigin#23153. Regression coverage verifies blocked waiter release, first-cause/idempotent abort behavior, accounting cleanup, the lifecycle-before-StopSending case, and the exact StopSending -> lifecycle registration -> outstanding batch/drain ordering with dispatch tombstone cleanup. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#26746 ## What this PR does / why we need it: The client sends a batch ACK before StopSending, but the server can process those messages concurrently and observe StopSending first. StopSending canceled query execution without terminating the negotiated batch-flow state, so the original handler could remain blocked in waitUntilDrained until the 30-second connection timeout. This change adds an idempotent aborted state to pipelineBatchFlow. StopSending now aborts the matching flow after canceling execution, releases outstanding batch accounting, wakes credit and drain waiters, rejects new reservations, and treats an already-flushed late ACK as harmless. It also closes the pre-registration race called out in review. StopSending first publishes the existing session-cleaned colexec cancellation tombstone and then checks the lifecycle; lifecycle registration publishes the lifecycle and then checks that tombstone. This publish-then-check handshake guarantees that either side observes the other without changing dispatch ReceiverDone semantics from matrixorigin#23153. Regression coverage verifies blocked waiter release, first-cause/idempotent abort behavior, accounting cleanup, the lifecycle-before-StopSending case, and the exact StopSending -> lifecycle registration -> outstanding batch/drain ordering with dispatch tombstone cleanup. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: issue matrixorigin#26746 ## What this PR does / why we need it: The client sends a batch ACK before StopSending, but the server can process those messages concurrently and observe StopSending first. StopSending canceled query execution without terminating the negotiated batch-flow state, so the original handler could remain blocked in waitUntilDrained until the 30-second connection timeout. This change adds an idempotent aborted state to pipelineBatchFlow. StopSending now aborts the matching flow after canceling execution, releases outstanding batch accounting, wakes credit and drain waiters, rejects new reservations, and treats an already-flushed late ACK as harmless. It also closes the pre-registration race called out in review. StopSending first publishes the existing session-cleaned colexec cancellation tombstone and then checks the lifecycle; lifecycle registration publishes the lifecycle and then checks that tombstone. This publish-then-check handshake guarantees that either side observes the other without changing dispatch ReceiverDone semantics from matrixorigin#23153. Regression coverage verifies blocked waiter release, first-cause/idempotent abort behavior, accounting cleanup, the lifecycle-before-StopSending case, and the exact StopSending -> lifecycle registration -> outstanding batch/drain ordering with dispatch tombstone cleanup. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
issue #26746
What this PR does / why we need it:
The client sends a batch ACK before StopSending, but the server can process those messages concurrently and observe StopSending first. StopSending canceled query execution without terminating the negotiated batch-flow state, so the original handler could remain blocked in waitUntilDrained until the 30-second connection timeout.
This change adds an idempotent aborted state to pipelineBatchFlow. StopSending now aborts the matching flow after canceling execution, releases outstanding batch accounting, wakes credit and drain waiters, rejects new reservations, and treats an already-flushed late ACK as harmless.
It also closes the pre-registration race called out in review. StopSending first publishes the existing session-cleaned colexec cancellation tombstone and then checks the lifecycle; lifecycle registration publishes the lifecycle and then checks that tombstone. This publish-then-check handshake guarantees that either side observes the other without changing dispatch ReceiverDone semantics from #23153.
Regression coverage verifies blocked waiter release, first-cause/idempotent abort behavior, accounting cleanup, the lifecycle-before-StopSending case, and the exact StopSending -> lifecycle registration -> outstanding batch/drain ordering with dispatch tombstone cleanup.