Skip to content

fix(pipeline): abort batch flow on stop sending - #26787

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
aptend:fix/pipeline-batch-stop-drain-26746
Aug 7, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
aptend:fix/pipeline-batch-stop-drain-26746

Conversation

@aptend

@aptend aptend commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

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.

@aptend
aptend requested review from XuPeng-SH and aunjgr as code owners August 7, 2026 09:42
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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:

  1. PrepareDoneNotifyMessage is admitted and its handler goroutine is delayed before lifecycle registration.
  2. The later StopSending goroutine runs first. CancelPipelineSending creates its existing alreadyDone tombstone, while abortPipelineBatchFlow sees no lifecycle and drops the abort.
  3. The original handler then registers a fresh, non-aborted batch flow. RecordDispatchPipeline intentionally 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.
  4. 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/compile normal and race suites passed;
  • dependent colexec dispatch/StopSending contract tests passed;
  • go build, go vet, targeted golangci-lint (0 issues), and git diff --check passed.

The current tests begin only after a lifecycle is already registered, so they cannot expose the missing generation transition above.

@aptend

aptend commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

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.

@aptend
aptend requested a review from XuPeng-SH August 7, 2026 10:58

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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:

  1. StopSending publishes the session-owned colexec tombstone before looking up/aborting the lifecycle.
  2. Lifecycle registration publishes the lifecycle before checking that tombstone.
  3. 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.
  4. 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, and pkg/sql/colexec/dispatch normal and race suites passed;
  • go list, go build, go vet, and git diff --check passed.

No blocking findings remain.

@XuPeng-SH
XuPeng-SH merged commit 7f0c723 into matrixorigin:main Aug 7, 2026
26 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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]>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## 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]>

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants