Skip to content

fix: make remote dispatch registration race-safe - #25574

Merged
XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
aptend:fix/remote-dispatch-receiver-registration
Jul 10, 2026
Merged

XuPeng-SH merged 4 commits into
matrixorigin:mainfrom
aptend:fix/remote-dispatch-receiver-registration

Conversation

@aptend

@aptend aptend commented Jul 10, 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:

Fixes #25565
Related to #25211

What this PR does / why we need it:

This PR fixes two remote dispatch registration races found while investigating the recurring Hive JOIN failures in #25211:

  1. A retry attempt no longer tombstones an absent receiver UUID when its RPC connection closes. The attempt connection does not own the query-wide receiver lifecycle, so late registration remains attachable by the next retry.
  2. Coordinator/local dispatch receivers are published before Compile.runOnce starts any scope goroutine. This establishes the required happens-before relation between receiver registration and remote notification.

The implementation also:

  • registers each dispatch with its owning scope.Proc;
  • distinguishes local scope trees, coordinator-retained remote root dispatches, and remote-owned subtrees;
  • transfers early-registration state to the retained local dispatch runner to avoid duplicate registration;
  • cancels owner processes and performs owner-checked rollback on partial registration failure;
  • releases pending attaches when RemoteRun fails before Dispatch.Prepare;
  • includes receiver UUID and the previous live/tombstone state in registration conflict errors.

A full registry state-machine redesign remains possible follow-up hardening, but is not required for these two proven failure paths.

Validation

  • go test -count=1 ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
  • focused go test -race across pkg/sql/colexec, pkg/sql/colexec/dispatch, and pkg/sql/compile
  • go build ./pkg/sql/colexec ./pkg/sql/colexec/dispatch ./pkg/sql/compile
  • go vet ./pkg/sql/colexec ./pkg/sql/colexec/dispatch ./pkg/sql/compile
  • git diff --check

The full pkg/sql/compile suite still has three parquet external-scan fanout failures; the same command and assertions fail on clean base commit 0fcd615a7f, so they are unrelated to this change.

@aptend
aptend requested review from aunjgr and ouyuanning as code owners July 10, 2026 09:27
@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.

Requesting changes on head 6a5784ebc.

The two primary fixes are directionally correct: a typed retry attempt no longer tombstones an absent query-wide receiver, and local/coordinator receiver registration now happens before scope goroutines start. The scope ownership traversal, retained-root state transfer, cancellation on RemoteRun failure, and owner-token cleanup all look coherent under the covered paths.

High: a failed duplicate registration still deletes the existing live/attached owner

Server.PutProcIntoUuidMap still mutates the registry destructively on conflict (pkg/sql/colexec/server.go:105-110): whenever the UUID already exists, it deletes that entry and then returns remote receiver ... already done.

That violates the atomicity promised by the new owner-checked rollback. prepareRemote can successfully register UUID A and then encounter an existing owner for UUID B. It rolls back A using the new channel owner token, but B has already been deleted by PutProcIntoUuidMap even though B was never owned by this registration attempt. The previous valid receiver can no longer attach, and subsequent notification falls back to NotRegistered/timeout behavior, recreating the exact registry artifact this PR is intended to remove.

I reproduced this deterministically with a temporary regression:

  1. pre-register UUID B with oldProc / oldCh;
  2. register a new Dispatch containing [A, B];
  3. registration of A succeeds and B conflicts;
  4. the call returns an error, then GetProcByUuid(B, false) returns ok=false instead of the original owner.

The assertion fails on the current head with:

Should be true: a failed duplicate registration deleted the existing owner

The current tests miss this because TestPutProcIntoUuidMapConflictIncludesUuidAndState checks only the error text, and TestRegisterRemoteReceiversRollbackOnPartialFailure checks only that this attempt removes A; neither verifies that the pre-existing B owner survives.

Please make conflict handling non-destructive for an existing live or attached registration. Note that proc == nil alone is not enough to classify an entry as a cancellation tombstone: an attached registration also has proc/ch == nil but retains a non-nil ownerCh. At minimum the state handling should distinguish:

  • live owner: proc != nil, preserve it;
  • attached owner: proc == nil && ownerCh != nil, preserve it;
  • pre-registration cancellation tombstone: no owner token, apply the explicitly intended tombstone transition.

Then add regressions for both live-owner and attached-owner conflicts, including the [A, conflicting B] partial-registration rollback. Only UUIDs written by the failing attempt should be removed.

Local validation on the PR head:

go test ./pkg/sql/colexec ./pkg/sql/colexec/dispatch -count=1
PASS

go test -race ./pkg/sql/compile -run focused-registration-tests -count=5
PASS

temporary existing-owner preservation regression
FAIL deterministically

CI currently has no failed checks, but this uncovered ownership case is a correctness blocker independent of CI.

@aptend

aptend commented Jul 10, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the change request in b17ff9b33d.

  • Registration conflicts now preserve existing live and attached owners.
  • Only an ownerless cancellation tombstone is consumed/deleted on a late registration attempt.
  • Conflict diagnostics now distinguish live, attached, and tombstone.
  • Added direct owner-preservation tests and [A, conflicting B] partial-registration tests for both live and attached B. The failing attempt rolls back only A.

Validation:

  • go test ./pkg/sql/colexec ./pkg/sql/colexec/dispatch -count=1
  • focused related tests under -race
  • focused owner/rollback tests repeated with -count=10
  • git diff --check

@aptend
aptend requested a review from XuPeng-SH July 10, 2026 10:41
@matrix-meow matrix-meow added size/XL Denotes a PR that changes [1000, 1999] lines and removed size/L Denotes a PR that changes [500,999] lines labels Jul 10, 2026

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

Approved on head b17ff9b33d897568134e111e919a375e0783ba01.

The previous correctness blocker is resolved. Registration conflicts are now non-destructive for both live and attached owners, while only an ownerless cancellation tombstone is consumed. The new direct registry tests and [A, conflicting B] partial-registration regressions verify that rollback removes only entries owned by the failing attempt.

I also rechecked the broader lifecycle:

  • local/coordinator receivers are published before any scope goroutine starts;
  • remote-owned subtrees are skipped while retained remote roots are registered with the owning scope process;
  • repeated traversal is idempotent and retained-root cleanup state is transferred back safely;
  • partial registration failure cancels affected processes and performs owner-checked rollback;
  • RemoteRun failure cancels the owner process, releasing a notify blocked before Dispatch.Prepare;
  • normal/error cleanup removes live or attached entries without allowing delayed cleanup to delete a newer owner.

No production hot-path regression was found. The extra work is one pre-execution scope/operator walk plus the same O(receiver) registry inserts that previously occurred during Prepare. The explicit registry state-machine redesign remains useful follow-up hardening, but I did not find it necessary to close either proven race fixed here.

Local validation:

  • go test ./pkg/sql/colexec ./pkg/sql/colexec/dispatch -count=1
  • focused compile registration/interleaving tests under -race -count=10
  • owner/conflict/rollback tests under -race -count=20
  • git diff --check

All CI checks, including UT, multi-CN BVT, coverage, benchmark, and both upgrade jobs, are green.

@XuPeng-SH
XuPeng-SH merged commit c5b128a into matrixorigin:main Jul 10, 2026
23 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:

Fixes matrixorigin#25565
Related to matrixorigin#25211

## What this PR does / why we need it:

This PR fixes two remote dispatch registration races found while
investigating the recurring Hive JOIN failures in matrixorigin#25211:

1. A retry attempt no longer tombstones an absent receiver UUID when its
RPC connection closes. The attempt connection does not own the
query-wide receiver lifecycle, so late registration remains attachable
by the next retry.
2. Coordinator/local dispatch receivers are published before
`Compile.runOnce` starts any scope goroutine. This establishes the
required happens-before relation between receiver registration and
remote notification.

The implementation also:

- registers each dispatch with its owning `scope.Proc`;
- distinguishes local scope trees, coordinator-retained remote root
dispatches, and remote-owned subtrees;
- transfers early-registration state to the retained local dispatch
runner to avoid duplicate registration;
- cancels owner processes and performs owner-checked rollback on partial
registration failure;
- releases pending attaches when `RemoteRun` fails before
`Dispatch.Prepare`;
- includes receiver UUID and the previous `live`/`tombstone` state in
registration conflict errors.

A full registry state-machine redesign remains possible follow-up
hardening, but is not required for these two proven failure paths.

## Validation

- `go test -count=1 ./pkg/sql/colexec ./pkg/sql/colexec/dispatch`
- focused `go test -race` across `pkg/sql/colexec`,
`pkg/sql/colexec/dispatch`, and `pkg/sql/compile`
- `go build ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
./pkg/sql/compile`
- `go vet ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
./pkg/sql/compile`
- `git diff --check`

The full `pkg/sql/compile` suite still has three parquet external-scan
fanout failures; the same command and assertions fail on clean base
commit `0fcd615a7f`, so they are unrelated to this change.

---------

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:

Fixes matrixorigin#25565
Related to matrixorigin#25211

## What this PR does / why we need it:

This PR fixes two remote dispatch registration races found while
investigating the recurring Hive JOIN failures in matrixorigin#25211:

1. A retry attempt no longer tombstones an absent receiver UUID when its
RPC connection closes. The attempt connection does not own the
query-wide receiver lifecycle, so late registration remains attachable
by the next retry.
2. Coordinator/local dispatch receivers are published before
`Compile.runOnce` starts any scope goroutine. This establishes the
required happens-before relation between receiver registration and
remote notification.

The implementation also:

- registers each dispatch with its owning `scope.Proc`;
- distinguishes local scope trees, coordinator-retained remote root
dispatches, and remote-owned subtrees;
- transfers early-registration state to the retained local dispatch
runner to avoid duplicate registration;
- cancels owner processes and performs owner-checked rollback on partial
registration failure;
- releases pending attaches when `RemoteRun` fails before
`Dispatch.Prepare`;
- includes receiver UUID and the previous `live`/`tombstone` state in
registration conflict errors.

A full registry state-machine redesign remains possible follow-up
hardening, but is not required for these two proven failure paths.

## Validation

- `go test -count=1 ./pkg/sql/colexec ./pkg/sql/colexec/dispatch`
- focused `go test -race` across `pkg/sql/colexec`,
`pkg/sql/colexec/dispatch`, and `pkg/sql/compile`
- `go build ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
./pkg/sql/compile`
- `go vet ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
./pkg/sql/compile`
- `git diff --check`

The full `pkg/sql/compile` suite still has three parquet external-scan
fanout failures; the same command and assertions fail on clean base
commit `0fcd615a7f`, so they are unrelated to this change.

---------

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:

Fixes matrixorigin#25565
Related to matrixorigin#25211

## What this PR does / why we need it:

This PR fixes two remote dispatch registration races found while
investigating the recurring Hive JOIN failures in matrixorigin#25211:

1. A retry attempt no longer tombstones an absent receiver UUID when its
RPC connection closes. The attempt connection does not own the
query-wide receiver lifecycle, so late registration remains attachable
by the next retry.
2. Coordinator/local dispatch receivers are published before
`Compile.runOnce` starts any scope goroutine. This establishes the
required happens-before relation between receiver registration and
remote notification.

The implementation also:

- registers each dispatch with its owning `scope.Proc`;
- distinguishes local scope trees, coordinator-retained remote root
dispatches, and remote-owned subtrees;
- transfers early-registration state to the retained local dispatch
runner to avoid duplicate registration;
- cancels owner processes and performs owner-checked rollback on partial
registration failure;
- releases pending attaches when `RemoteRun` fails before
`Dispatch.Prepare`;
- includes receiver UUID and the previous `live`/`tombstone` state in
registration conflict errors.

A full registry state-machine redesign remains possible follow-up
hardening, but is not required for these two proven failure paths.

## Validation

- `go test -count=1 ./pkg/sql/colexec ./pkg/sql/colexec/dispatch`
- focused `go test -race` across `pkg/sql/colexec`,
`pkg/sql/colexec/dispatch`, and `pkg/sql/compile`
- `go build ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
./pkg/sql/compile`
- `go vet ./pkg/sql/colexec ./pkg/sql/colexec/dispatch
./pkg/sql/compile`
- `git diff --check`

The full `pkg/sql/compile` suite still has three parquet external-scan
fanout failures; the same command and assertions fail on clean base
commit `0fcd615a7f`, so they are unrelated to this change.

---------

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Refactor remote dispatch receiver registry to eliminate unstable registration/attach races

3 participants