Skip to content

fix: prevent nested local shuffle deadlocks - #26055

Merged
XuPeng-SH merged 6 commits into
mainfrom
fix/shuffle-fixed-bucket-liveness
Jul 23, 2026
Merged

XuPeng-SH merged 6 commits into
mainfrom
fix/shuffle-fixed-bucket-liveness

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes

Fixes #25922

What this PR does / why we need it

The unified Shuffle implementation added bounded backpressure to shared local Shuffle pools. Per-bucket credits fixed same-level hot-bucket starvation, but a finite fixed-bucket pool can still deadlock when more than one local Shuffle is duplicated into the same pull-based worker tree.

The observed 16-worker wait-for cycle was:

  1. an outer Shuffle writer filled its target bucket and waited for that bucket's worker;
  2. the target worker was blocked in an inner Shuffle waiting for every inner writer to finish;
  3. the missing inner writer was the child of the blocked outer writer, so it could only finish if the outer writer resumed pulling.

This change prevents the cyclic topology instead of increasing or removing the memory bound:

  • keep the first fixed-bucket local Shuffle fast path;
  • detect a fixed-bucket Shuffle already present in a packed operator tree;
  • materialize a second join/group Shuffle through the existing producer scopes, bounded dispatch channels, and consumer scopes;
  • preserve local Shuffle reuse when no second probe Shuffle is introduced;
  • cancel an active request before connection cleanup waits for migration/reset or transaction rollback, so cleanup is independent of a stuck data path.

The fallback remains bounded: its existing dispatch channel capacity is 32, and this PR does not change Shuffle ready credits or introduce an unbounded queue. It also does not introduce a new hard query timeout.

Regression coverage

  • 16-way nested local shuffle join, on both probe and build chains;
  • 16-way nested local shuffle group;
  • fixed Shuffle below a non-root operator to cover recursive detection;
  • single-layer local group and reusable local join fast paths remain local;
  • connection cleanup cancels before a blocked lifecycle operation and before rollback.

The nested-join regression failed on origin/main by producing one packed local scope and passes with this change by producing 16 independent bucket scopes.

Validation

  • go test -race -count=1 -timeout=600s ./pkg/sql/compile ./pkg/frontend
  • focused new concurrency/topology tests under -race -count=20
  • go test -v -count=1 -timeout=600s ./pkg/sql/compile ./pkg/frontend
  • go test -count=1 ./pkg/sql/colexec/shuffle ./pkg/sql/colexec/dispatch ./pkg/sql/colexec/merge ./pkg/sql/colexec/group
  • go build ./pkg/sql/compile ./pkg/frontend
  • go vet ./pkg/sql/compile ./pkg/frontend
  • git diff --check

@XuPeng-SH
XuPeng-SH requested a review from aunjgr as a code owner July 23, 2026 02:43
@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

Copy link
Copy Markdown
Contributor Author

Deep-review follow-up pushed in 02bc01a and rebased by merge on the latest main in 1992a71.

Correctness:

  • close now seals lifecycle admission, cancels the active request and lifecycle operation, then waits;
  • migration, migrate-from, and reset propagate RPC/lifecycle cancellation;
  • reset failure restores protocol state and releases the speculative session;
  • a server-wide liveness monitor detects a disconnected client during a long synchronous Handler call without consuming protocol bytes, including TLS and unread-buffer cases.

Performance:

  • one ticker per MOServer, every 5 seconds;
  • only requests active for at least 30 seconds are probed;
  • 10,000-connection scan benchmark: 57.6-58.4 us and 0 allocs when idle; about 60.6-61.5 us and 4.4 KB with 1 percent long-running requests.

Risk controls:

  • no per-query goroutine or unbounded queue;
  • socket probing is nonblocking MSG_PEEK plus poll, with Linux POLLRDHUP support;
  • cleanup ownership stays in Conn.Close and Routine.cleanup, while the monitor only signals cancellation;
  • closing routines are excluded from later scans to avoid repeated probes and log amplification;
  • unsupported platforms fail open and retain existing behavior.

Post-main-merge validation:

  • full pkg/frontend tests;
  • full pkg/sql/compile tests;
  • targeted pkg/cnservice migration/reset handler tests;
  • targeted frontend race tests;
  • Linux socket-probe cross-compilation;
  • git diff --check.

@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/M Denotes a PR that changes [100,499] lines labels Jul 23, 2026
@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Addressed the requested change in 0b1a36a.

The reset path now uses a dedicated context-aware rollback entry point, so migrateController.startClose cancellation reaches the actual TxnOperator.Rollback call. Normal SQL rollback intentionally continues to inherit the transaction context; request cancellation must not skip ordinary transaction cleanup.

Added TestRoutineCloseCancelsResetRollback, which deterministically blocks inside TxnOperator.Rollback, closes the routine, and verifies both reset and waitAndClose terminate promptly.

Fresh validation after the change:

  • focused regression: count=10
  • focused race regression: count=10
  • full pkg/frontend tests
  • full pkg/frontend race tests
  • full pkg/sql/compile tests
  • targeted pkg/cnservice ResetSession test
  • go build and go vet for all three changed production packages
  • full PR self-review and git diff --check

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed the complete PR at 0b1a36a15feaa7765d2304fcc3a938ff3398a478, including the latest reset-rollback cancellation fix. The dedicated context-aware rollback path now breaks the close/reset wait cycle without changing normal statement or cleanup rollback semantics, and the new blocking regression test covers the actual storage rollback context. I found no remaining actionable correctness or regression issue.

@aptend aptend 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.

Independent re-review completed on head 0b1a36a15feaa7765d2304fcc3a938ff3398a478 against base 56735f02fd1e045685e4072289eae99bd8621b5a.

No merge-blocking findings remain. The review covered the complete diff, shuffle topology closure, frontend lifecycle/reset/close state transitions, socket liveness probing, and Q1-Q3 unhappy paths. Targeted and full owning-package tests, frontend race tests, build, vet, and diff checks passed.

@aunjgr
aunjgr self-requested a review July 23, 2026 07:29
@mergify

mergify Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@XuPeng-SH
XuPeng-SH merged commit 0d7eeb3 into main Jul 23, 2026
5 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/shuffle-fixed-bucket-liveness branch July 23, 2026 07:39
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes

Fixes matrixorigin#25922

## What this PR does / why we need it

The unified Shuffle implementation added bounded backpressure to shared
local Shuffle pools. Per-bucket credits fixed same-level hot-bucket
starvation, but a finite fixed-bucket pool can still deadlock when more
than one local Shuffle is duplicated into the same pull-based worker
tree.

The observed 16-worker wait-for cycle was:

1. an outer Shuffle writer filled its target bucket and waited for that
bucket's worker;
2. the target worker was blocked in an inner Shuffle waiting for every
inner writer to finish;
3. the missing inner writer was the child of the blocked outer writer,
so it could only finish if the outer writer resumed pulling.

This change prevents the cyclic topology instead of increasing or
removing the memory bound:

- keep the first fixed-bucket local Shuffle fast path;
- detect a fixed-bucket Shuffle already present in a packed operator
tree;
- materialize a second join/group Shuffle through the existing producer
scopes, bounded dispatch channels, and consumer scopes;
- preserve local Shuffle reuse when no second probe Shuffle is
introduced;
- cancel an active request before connection cleanup waits for
migration/reset or transaction rollback, so cleanup is independent of a
stuck data path.

The fallback remains bounded: its existing dispatch channel capacity is
32, and this PR does not change Shuffle ready credits or introduce an
unbounded queue. It also does not introduce a new hard query timeout.

## Regression coverage

- 16-way nested local shuffle join, on both probe and build chains;
- 16-way nested local shuffle group;
- fixed Shuffle below a non-root operator to cover recursive detection;
- single-layer local group and reusable local join fast paths remain
local;
- connection cleanup cancels before a blocked lifecycle operation and
before rollback.

The nested-join regression failed on `origin/main` by producing one
packed local scope and passes with this change by producing 16
independent bucket scopes.

## Validation

- `go test -race -count=1 -timeout=600s ./pkg/sql/compile
./pkg/frontend`
- focused new concurrency/topology tests under `-race -count=20`
- `go test -v -count=1 -timeout=600s ./pkg/sql/compile ./pkg/frontend`
- `go test -count=1 ./pkg/sql/colexec/shuffle ./pkg/sql/colexec/dispatch
./pkg/sql/colexec/merge ./pkg/sql/colexec/group`
- `go build ./pkg/sql/compile ./pkg/frontend`
- `go vet ./pkg/sql/compile ./pkg/frontend`
- `git diff --check`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes

Fixes matrixorigin#25922

## What this PR does / why we need it

The unified Shuffle implementation added bounded backpressure to shared
local Shuffle pools. Per-bucket credits fixed same-level hot-bucket
starvation, but a finite fixed-bucket pool can still deadlock when more
than one local Shuffle is duplicated into the same pull-based worker
tree.

The observed 16-worker wait-for cycle was:

1. an outer Shuffle writer filled its target bucket and waited for that
bucket's worker;
2. the target worker was blocked in an inner Shuffle waiting for every
inner writer to finish;
3. the missing inner writer was the child of the blocked outer writer,
so it could only finish if the outer writer resumed pulling.

This change prevents the cyclic topology instead of increasing or
removing the memory bound:

- keep the first fixed-bucket local Shuffle fast path;
- detect a fixed-bucket Shuffle already present in a packed operator
tree;
- materialize a second join/group Shuffle through the existing producer
scopes, bounded dispatch channels, and consumer scopes;
- preserve local Shuffle reuse when no second probe Shuffle is
introduced;
- cancel an active request before connection cleanup waits for
migration/reset or transaction rollback, so cleanup is independent of a
stuck data path.

The fallback remains bounded: its existing dispatch channel capacity is
32, and this PR does not change Shuffle ready credits or introduce an
unbounded queue. It also does not introduce a new hard query timeout.

## Regression coverage

- 16-way nested local shuffle join, on both probe and build chains;
- 16-way nested local shuffle group;
- fixed Shuffle below a non-root operator to cover recursive detection;
- single-layer local group and reusable local join fast paths remain
local;
- connection cleanup cancels before a blocked lifecycle operation and
before rollback.

The nested-join regression failed on `origin/main` by producing one
packed local scope and passes with this change by producing 16
independent bucket scopes.

## Validation

- `go test -race -count=1 -timeout=600s ./pkg/sql/compile
./pkg/frontend`
- focused new concurrency/topology tests under `-race -count=20`
- `go test -v -count=1 -timeout=600s ./pkg/sql/compile ./pkg/frontend`
- `go test -count=1 ./pkg/sql/colexec/shuffle ./pkg/sql/colexec/dispatch
./pkg/sql/colexec/merge ./pkg/sql/colexec/group`
- `go build ./pkg/sql/compile ./pkg/frontend`
- `go vet ./pkg/sql/compile ./pkg/frontend`
- `git diff --check`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes

Fixes matrixorigin#25922

## What this PR does / why we need it

The unified Shuffle implementation added bounded backpressure to shared
local Shuffle pools. Per-bucket credits fixed same-level hot-bucket
starvation, but a finite fixed-bucket pool can still deadlock when more
than one local Shuffle is duplicated into the same pull-based worker
tree.

The observed 16-worker wait-for cycle was:

1. an outer Shuffle writer filled its target bucket and waited for that
bucket's worker;
2. the target worker was blocked in an inner Shuffle waiting for every
inner writer to finish;
3. the missing inner writer was the child of the blocked outer writer,
so it could only finish if the outer writer resumed pulling.

This change prevents the cyclic topology instead of increasing or
removing the memory bound:

- keep the first fixed-bucket local Shuffle fast path;
- detect a fixed-bucket Shuffle already present in a packed operator
tree;
- materialize a second join/group Shuffle through the existing producer
scopes, bounded dispatch channels, and consumer scopes;
- preserve local Shuffle reuse when no second probe Shuffle is
introduced;
- cancel an active request before connection cleanup waits for
migration/reset or transaction rollback, so cleanup is independent of a
stuck data path.

The fallback remains bounded: its existing dispatch channel capacity is
32, and this PR does not change Shuffle ready credits or introduce an
unbounded queue. It also does not introduce a new hard query timeout.

## Regression coverage

- 16-way nested local shuffle join, on both probe and build chains;
- 16-way nested local shuffle group;
- fixed Shuffle below a non-root operator to cover recursive detection;
- single-layer local group and reusable local join fast paths remain
local;
- connection cleanup cancels before a blocked lifecycle operation and
before rollback.

The nested-join regression failed on `origin/main` by producing one
packed local scope and passes with this change by producing 16
independent bucket scopes.

## Validation

- `go test -race -count=1 -timeout=600s ./pkg/sql/compile
./pkg/frontend`
- focused new concurrency/topology tests under `-race -count=20`
- `go test -v -count=1 -timeout=600s ./pkg/sql/compile ./pkg/frontend`
- `go test -count=1 ./pkg/sql/colexec/shuffle ./pkg/sql/colexec/dispatch
./pkg/sql/colexec/merge ./pkg/sql/colexec/group`
- `go build ./pkg/sql/compile ./pkg/frontend`
- `go vet ./pkg/sql/compile ./pkg/frontend`
- `git diff --check`

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

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/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] TPCH Q16 hangs indefinitely: Shuffle execution engine deadlock in waitBatchOrEnd (TPCH 100G stuck for 15+ hours)

6 participants