Skip to content

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

Closed

Description

@XuPeng-SH

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

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 done
  • dispatch 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 30s artifacts caused by registry state corruption

Steps to Reproduce

  1. Run a multi-CN environment on main at commit c600bf18ba003564955ee7962ec2bacead97d65d.
  2. Execute the external Hive partition workload associated with #25211.
  3. Observe the latest nightly reproduction:
    • nightly run: 29054645722
    • job: 86253126494
  4. In that run:
    • the EXISTS query 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
  5. Inspect pkg/sql/colexec/server.go and 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.go
  • pkg/sql/compile/scopeRemoteRun.go
  • pkg/sql/colexec/dispatch/

Relevant observations from code review:

  • GetProcByUuid consumes live entries and mutates registry state as part of lookup
  • PutProcIntoUuidMap can delete an existing entry and return remote receiver already done, which is unsafe for duplicate registration or reordered cleanup
  • DeleteUuids uses 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:

  1. Replace the implicit receiver map with an explicit state machine:
    • pending
    • registered
    • attached
    • completed
    • canceled
  2. Split APIs into non-destructive operations such as:
    • RegisterReceiver
    • AttachReceiver
    • CancelReceiver
    • CompleteReceiver
  3. Make duplicate operations idempotent and generation-safe.
  4. Add a registration-ready acknowledgment between remote scope setup and sender notify, instead of relying primarily on polling/retry.
  5. Preserve the first root error and improve observability with receiver UUID, query ID, source CN, target CN, and lifecycle transition logs/metrics.
  6. 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

  1. added this to the MO-202607 milestone on Jul 10, 2026
  2. self-assigned this
    on Jul 10, 2026
  3. added
    severity/s0
    on Jul 10, 2026
  4. aptend commented on Jul 10, 2026

    @aptend
    Contributor

    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

    • Latest reproduction: run 29054645722 / job 86253126494, commit c600bf18ba003564955ee7962ec2bacead97d65d.
      • For trace 4ca2f9fd-50c1-89fb-50d9-5d443ad9be4c, remote receiver already done was emitted at 00:36:31.685, about 220 ms after the fact/dim JOIN started.
      • A receiver UUID then reported not registered yet approximately every 200 ms and reached the exact 30-second registration timeout at 00:37:01.685.
      • The client did not observe the error until about 91 seconds after query start. The same window also contains CN:6002 closed connections and 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.
    • Historical reproduction: run 28338073448 / job 83953472996 shows the same coordinator-side receiver failing to register within 30 seconds. The newer remote receiver already done surface appeared after retry was introduced.
    • Successful control: run 28902823580 / job 85780232473 ran the same fact/dim JOIN in about 334 ms and the self-JOIN in about 628 ms. There was no registration retry. This is consistent with the race being hidden when the coordinator source reaches vm.Prepare quickly.
    • A deterministic registry test reproduces the first bug without S3 or network load:
      1. notify arrives before registration and returns ErrRemoteDispatchNotRegistered;
      2. closing that retry stream cancels its server-side connection context;
      3. the server writes an absent-UUID tombstone;
      4. late PutProcIntoUuidMap fails with remote receiver already done;
      5. a fresh retry still cannot attach.
        The control path, where the retry attempt does not cancel the connection context, allows late registration and attach normally.

    Root causes

    1. A retry attempt can poison a query-wide receiver UUID.

      sendNotifyMessageWithFactory closes the stream after the typed NotRegistered response. TryGetProcByUuid currently interprets that attempt/session close as permanent receiver cancellation and calls forced GetProcByUuid, 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.

    2. Coordinator/local dispatch receivers are registered after remote consumers are started.

      MergeRun starts pre/remote scopes and their notify requests before the coordinator/local source reaches dispatch.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

    Fix scope

    • Do not let cancellation/close of a single PrepareDoneNotify retry attempt tombstone an absent query receiver. Explicit query cancellation must still cancel an attached process and leave no live registration behind.
    • Early-register coordinator/local dispatch receiver UUIDs before any pre/remote scope or notify goroutine is started. Cover root and nested PreScopes, make repeated traversal idempotent, roll back partial registration failure, and clean registrations on compile/run failure.
    • Preserve the first meaningful error and add UUID/state information where registry transition errors are logged.

    Acceptance tests

    • closed/canceled retry attempt -> late registration succeeds -> the next retry attaches the same proc and channel;
    • explicit query cancellation -> attached process is canceled and no live UUID remains after cleanup;
    • close-vs-register in both barrier-controlled interleavings, runnable under -race;
    • coordinator source blocked before vm.Prepare -> UUID is already live and remote notify can attach;
    • nested PreScopes, repeated early-registration traversal, partial-registration rollback, and run/compile failure cleanup;
    • the two Hive partition JOIN queries intermittently fail with dispatch timeout (dispatch process not registered within 30s) #25211 Hive JOIN SQLs pass repeatedly under injected pre-Prepare delay and in nightly runs.
  5. added a commit that references this issue on Jul 10, 2026
    c5b128a
  6. aptend commented on Jul 13, 2026

    @aptend
    Contributor

    tracked by #25211

  7. added 3 commits that reference this issue on Sep 17, 2026
    05e071e
    fc9e0ca
    88f42a5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/stabilityattention/refactor-relatedkind/bugneeds-more-testsnightly-regressionseverity/s0source/dev

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions