Skip to content

[Refactor] Make pipeline termination protocol explicit and cancellation-safe #24971

Closed

Description

@XuPeng-SH

Background

Related to #24962.

The immediate hang in #24962 is not caused by IVF SQL semantics directly. The stack traces point to pipeline cleanup waiting indefinitely for terminal pipeline signals, for example Merge.Reset -> PipelineSignalReceiver.WaitingEnd, while the corresponding sender side may fail to send or the receiver may fail to consume the final end signal.

The current protocol is distributed across several implicit mechanisms:

  • nil batch means end/error in some paths.
  • WaitRegister.Ch2 is used for both data notifications and terminal notifications.
  • PipelineSpool.Close() waits for receivers to consume terminal messages.
  • cleanup ordering decides whether sender or receiver can make progress.
  • some cleanup paths used unbounded sends or context.TODO() waits.

This makes unhappy paths fragile: cancellation, remote CN failure, network jitter, channel backpressure, mismatched NilBatchCnt, or cleanup order can leave one side waiting for another side that is no longer guaranteed to make progress.

First principles

A pipeline edge should have an explicit lifecycle and these invariants:

  1. Terminal state is a protocol event, not an implicit nil data batch.
  2. End, Error, and Abort are idempotent. Calling them multiple times must be safe.
  3. Cleanup must not depend on the peer consuming one more channel message.
  4. Every send, receive, and terminal wait must be cancelable or bounded.
  5. Spool memory ownership must be independent from channel notification delivery. If receivers are late or gone, cleanup must avoid use-after-free even if it cannot reclaim all memory immediately.
  6. Normal long-running queries must not be affected by cleanup-only deadlines.

Proposed direction

Introduce an explicit pipeline edge abstraction, or evolve WaitRegister toward one, with typed events:

  • Data
  • End
  • Error
  • Abort

The edge should provide APIs along these lines:

type PipelineEventType int

const (
    EventData PipelineEventType = iota
    EventEnd
    EventError
    EventAbort
)

type PipelineEvent struct {
    Type  PipelineEventType
    Batch *batch.Batch
    Err   error
    Spool *pSpool.PipelineSpool
    Index int
}

type PipelineEdge struct {
    // implementation detail: channel, done/abort signals, terminal state, once guards
}

func (e *PipelineEdge) Send(ctx context.Context, ev PipelineEvent) error
func (e *PipelineEdge) Recv(ctx context.Context) (PipelineEvent, error)
func (e *PipelineEdge) Close(err error)
func (e *PipelineEdge) Abort(err error)
func (e *PipelineEdge) Done() <-chan struct{}

Connector, Dispatch, remote notify forwarding, and Merge should use this lifecycle instead of open-coded terminal sends and waits.

Suggested migration plan

  1. Add cancellation-aware terminal APIs behind the existing WaitRegister surface.
  2. Replace cleanup-time naked sends to WaitRegister.Ch2 with the new APIs.
  3. Add explicit End/Error/Abort event types to PipelineSignal or a replacement event type.
  4. Remove or reduce reliance on NilBatchCnt for terminal correctness.
  5. Move PipelineSpool terminal/close behavior to an abortable lifecycle API, preserving safe memory ownership.
  6. Convert normal data send paths such as local Connector/Dispatch notification sends to context-aware APIs where appropriate.
  7. Add tests for sender exits first, receiver exits first, channel full, missing end, multi-CN remote receiver failure, network/cancel races, and slow-but-healthy long-running queries.

Acceptance criteria

  • No pipeline cleanup path can wait forever on a peer channel, remote error channel, spool close, or terminal signal.
  • Merge.Reset and sender cleanup do not rely on cleanup ordering to avoid deadlock.
  • Terminal events are idempotent and observable in diagnostics.
  • Normal query execution is not bounded by cleanup-only timeouts.
  • Spool cleanup cannot free memory that a late receiver can still access.
  • Existing CTE/recursive pipeline special cleanup semantics are preserved.
  • Tests cover local, multi-CN, cancellation, backpressure, and receiver/sender mismatch unhappy paths.

Notes

A short-term mitigation can bound waits and add diagnostics, but the long-term fix should make the termination protocol explicit so correctness is enforced by the API rather than by cleanup order and implicit nil batch conventions.

Activity

  1. self-assigned this
    on Jun 30, 2026
  2. added
    kind/refactor
    priority/p1
    on Jun 30, 2026
  3. added theissue type on Jun 30, 2026
  4. added this to the MO-202607 milestone on Jun 30, 2026
  5. added
    severity/s2
    on Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/refactorpriority/p1severity/s2

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions