Repository navigation
Description
Title: [Bug]: Refactor remote dispatch receiver registry to eliminate unstable registration/attach races
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
main
Commit ID
c600bf1
Other Environment Information
- Hardware parameters: GitHub Actions nightly environment
- OS type: Linux
- Others:
- Multi-CN execution path
- External Hive partition queries
- Reproduced in nightly run
29054645722, job86253126494 - Existing related issue: Hive partition JOIN queries intermittently fail with dispatch timeout (dispatch process not registered within 30s) #25211
- Existing direct fixes already merged before this commit: fix: register remote dispatch receivers early #25238, fix: retry remote notify before dispatch registration #25475
Actual Behavior
#25211 is not fully closed at the root-cause level. On main at c600bf18ba003564955ee7962ec2bacead97d65d, the same workload still fails in the remote dispatch path with inconsistent lifecycle errors:
internal error: remote receiver already donedispatch process not registered within 30s
The current remote receiver registry still relies on implicit map states and destructive transitions in pkg/sql/colexec/server.go. It conflates at least these different states:
- not registered yet
- registered and waiting to be attached
- already attached / already consumed
- canceled before registration
- cleanup tombstone
Because these states are encoded indirectly by nil fields and delete-on-read behavior, duplicate or reordered register/attach/cancel flows can report misleading terminal errors, remove valid entries, or turn a real upstream failure into a registry artifact.
This means the current fix set mitigates one race, but the lifecycle model is still fragile and not first-principles safe under retry, duplicate notification, cancellation, and concurrent cleanup.
Expected Behavior
The remote dispatch receiver lifecycle should be explicit, idempotent, and race-safe:
- registration, attach, cancel, and completion should use explicit state transitions instead of implicit
nil/delete semantics - duplicate register / duplicate notify / cleanup-after-cancel should not destroy a valid live entry
- sender retry should not be the primary correctness mechanism
- the first real root cause should be preserved instead of being overwritten by secondary registry errors
- multi-CN external queries should either succeed or fail with the true upstream reason, without
remote receiver already done/not registered within 30sartifacts caused by registry state corruption
Steps to Reproduce
- Run a multi-CN environment on
mainat commitc600bf18ba003564955ee7962ec2bacead97d65d. - Execute the external Hive partition workload associated with
#25211. - Observe the latest nightly reproduction:
- nightly run:
29054645722 - job:
86253126494
- nightly run:
- In that run:
- the
EXISTSquery succeeds after about 91 seconds - a fact/dim join then fails with
internal error: remote receiver already done - a self-join then fails with
dispatch process not registered within 30s
- the
- Inspect
pkg/sql/colexec/server.goand related remote dispatch scheduling paths to see that the registry API is still destructive and state-ambiguous even after fix: register remote dispatch receivers early #25238 and fix: retry remote notify before dispatch registration #25475.
Additional information
Relevant code paths:
pkg/sql/colexec/server.gopkg/sql/compile/scopeRemoteRun.gopkg/sql/colexec/dispatch/
Relevant observations from code review:
GetProcByUuidconsumes live entries and mutates registry state as part of lookupPutProcIntoUuidMapcan delete an existing entry and returnremote receiver already done, which is unsafe for duplicate registration or reordered cleanupDeleteUuidsuses tombstones, but tombstones, live entries, attached entries, and canceled entries are not modeled explicitly- current retry logic in fix: retry remote notify before dispatch registration #25475 improves timing sensitivity, but does not fix lifecycle ambiguity
Suggested fix direction:
- Replace the implicit receiver map with an explicit state machine:
pendingregisteredattachedcompletedcanceled
- Split APIs into non-destructive operations such as:
RegisterReceiverAttachReceiverCancelReceiverCompleteReceiver
- Make duplicate operations idempotent and generation-safe.
- Add a registration-ready acknowledgment between remote scope setup and sender notify, instead of relying primarily on polling/retry.
- Preserve the first root error and improve observability with receiver UUID, query ID, source CN, target CN, and lifecycle transition logs/metrics.
- Add focused tests for:
- duplicate registration
- duplicate notify
- cancel-before-register
- register-vs-cleanup races
- attach-vs-timeout races
- end-to-end multi-CN external join execution
This issue is intentionally separate from #25211: #25211 tracks the user-visible failure, while this one tracks the deeper registry/lifecycle redesign still needed to make the fix set complete and robust.
Activity
aptend commentedon Jul 10, 2026
We did an independent RCA of this failure instead of treating the registry redesign proposed above as already proven. The evidence points to two narrower correctness bugs.
Evidence
c600bf18ba003564955ee7962ec2bacead97d65d.4ca2f9fd-50c1-89fb-50d9-5d443ad9be4c,remote receiver already donewas emitted at00:36:31.685, about 220 ms after the fact/dim JOIN started.not registered yetapproximately every 200 ms and reached the exact 30-second registration timeout at00:37:01.685.cannot get salt: i/o timeout, so load/network degradation widens the race and delays error delivery, but does not by itself explain the registry transition.remote receiver already donesurface appeared after retry was introduced.vm.Preparequickly.ErrRemoteDispatchNotRegistered;PutProcIntoUuidMapfails withremote receiver already done;The control path, where the retry attempt does not cancel the connection context, allows late registration and attach normally.
Root causes
A retry attempt can poison a query-wide receiver UUID.
sendNotifyMessageWithFactorycloses the stream after the typedNotRegisteredresponse.TryGetProcByUuidcurrently interprets that attempt/session close as permanent receiver cancellation and calls forcedGetProcByUuid, creating a tombstone. Late registration then consumes the tombstone and fails. An individual retry RPC does not own the lifetime of the query receiver UUID.Coordinator/local dispatch receivers are registered after remote consumers are started.
MergeRunstarts pre/remote scopes and their notify requests before the coordinator/local source reachesdispatch.Prepare, where its receiver UUID is currently published. fix: register remote dispatch receivers early #25238 performs early registration for a scope decoded on a remote CN, but does not establish the same happens-before relation for coordinator/local scopes. Slow external-scan/join preparation can therefore expose a full 30-second unregistered window.Relationship to existing work
NotRegisteredretry. The retry mitigates ordinary ordering, but closing each retry stream exposed the tombstone-poisoning bug above.Fix scope
PrepareDoneNotifyretry attempt tombstone an absent query receiver. Explicit query cancellation must still cancel an attached process and leave no live registration behind.PreScopes, make repeated traversal idempotent, roll back partial registration failure, and clean registrations on compile/run failure.Acceptance tests
procand channel;-race;vm.Prepare-> UUID is already live and remote notify can attach;PreScopes, repeated early-registration traversal, partial-registration rollback, and run/compile failure cleanup;fix: make remote dispatch registration race-safe (#25574)
aptend commentedon Jul 13, 2026
tracked by #25211
fix: make remote dispatch registration race-safe (matrixorigin#25574)
fix: make remote dispatch registration race-safe (matrixorigin#25574)
fix: make remote dispatch registration race-safe (matrixorigin#25574)