Skip to content

[Bug]: MergeOrder continues spill final merge after cancellation arrives at input EOF #26446

Closed

Description

@Ariznawlll

[Bug]: MergeOrder continues spill final merge after cancellation arrives at input EOF

Summary

MergeOrder.Call consumes its child until EOF and then immediately enters
prepareSpillFinalMerge when spill runs exist. If the pipeline context is
canceled immediately after the child returns EOF, the operator proceeds with
the final spill merge and returns a normal result instead of
context.Canceled.

For a large ORDER BY this leaves a canceled statement doing potentially
expensive spill file reads, run reduction, and output merging.

Reproduction

The focused white-box reproducer is:

pkg/sql/colexec/mergeorder/order_test.go
TestOrderSpillFinalMergeHonorsCancellationAfterInput

It creates three small input batches, sets MergeOrder.SpillThreshold = 1 to
force each batch into a spill run, and uses a child operator that cancels the
process exactly when it returns EOF. The expected result of the enclosing
MergeOrder call is context.Canceled.

Run:

go test ./pkg/sql/colexec/mergeorder \
  -run TestOrderSpillFinalMergeHonorsCancellationAfterInput -count=1 -v

Actual result

The test fails because MergeOrder.Call returns a nil error after the
cancellation:

Expected error with "context canceled" in chain but got nil.

The test necessarily reaches the spilled path: the threshold is one byte and
the EOF transition triggers prepareSpillFinalMerge before the normal sorted
result can be returned.

Expected result

Once the pipeline context is canceled, MergeOrder should stop before final
spill merge work, return the cancellation error, and let the standard
reset/free path clean up its spill runs and readers.

Reproducibility

Reproduced twice independently on origin/main at ebdc3de0f:

  1. First focused run returned nil after cancellation.
  2. Second focused run returned the same result; its command exits with code 1
    only because the test correctly expects context.Canceled.

Suspected area

pkg/sql/colexec/mergeorder/order.go switches from receiving to
prepareSpillFinalMerge after its EOF child call without another
vm.CancelCheck(proc). The spill finalization and reduction loops in
pkg/sql/colexec/mergeorder/spill.go likewise continue without consulting
the canceled process context.

Scope

This report is limited to MergeOrder's spilled final-merge cancellation path.
It does not change production code.

Activity

  1. added this to the MO-202607 milestone on Jul 30, 2026
  2. added
    kind/bug
    on Jul 30, 2026
  3. added theissue type on Jul 30, 2026
  4. self-assigned this
    on Jul 30, 2026
  5. XuPeng-SH commented on Aug 1, 2026

    @XuPeng-SH
    Contributor

    Roadmap grouping: #26563. Fix together with #26444/#26447 under one narrow invariant: after child EOF, observe cancellation before spill reload/final merge/materialization; during long work, check at bucket/run/chunk boundaries rather than per row. Reuse a shared test fixture, but do not change global ChildrenCall semantics or introduce a general cancellation/I/O framework.

  6. added a commit that references this issue on Aug 4, 2026
    7ca0dd2
  7. Ariznawlll commented on Aug 5, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:ace13bf4b43773dd962f03325b22f58ed31fee91
    • 部署方式:macOS/arm64 本地 Go 单测;受控 spill runs 与可取消 process.Context。
    • 测试日期:2026-08-05

    原问题验证

    执行 go test -count=3 ./pkg/sql/colexec/mergeorder 与 go test -race ./pkg/sql/colexec/mergeorder。TestOrderSpillFinalMergeHonorsCancellationAfterInput 覆盖输入 EOF 后、final merge 开始前取消的原问题边界。

    Happy Path 覆盖

    1. TestOrderSpill、TestOrderSpillMultiPass:单轮和多轮 spill merge 均正确返回有序结果。
    2. TestOrderSpillMultiKey、TestOrderSpillDesc、TestOrderSpillNullsLast:多 key、倒序和 NULL 排序语义保持正确。
    3. TestMergeOrderResetAndOpType:正常 merge 后可 reset/reuse。

    Unhappy Path 覆盖

    1. EOF 后 final merge 取消:TestOrderSpillFinalMergeHonorsCancellationAfterInput。
    2. 写 spill 后取消与 batch 边界停止:TestOrderSpillWriteHonorsCancellationAfterInputBatch、TestWriteSpillBatchStopsAtBatchBoundary。
    3. merge/read 阶段取消及 ownership 清理:TestMergeRunsToSpillCancellationCheckpoints、TestOpenSpillReadersCancellationPreservesOwnership、TestSpillReaderRolloverCancellationBoundaries。
    4. 未提交 active run:TestCleanupSpillDiscardsUncommittedActiveRun,不留下可见结果。

    白盒验证

    针对 order_test.go 运行 3 轮 UT 和 1 轮 -race;覆盖 final merge、reader rollover、run cleanup 和 reset。

    回归测试

    取消注入点位于 MergeOrder 内部 final merge,采用 Go UT 回归;不适合用普通 SQL/motr 的非确定性客户端超时替代。

    测试证据

    执行命令:go test -count=3 ./pkg/sql/colexec/mergeorder、go test -race ./pkg/sql/colexec/mergeorder,均以 exit code 0 完成。

    测试结论

    测试通过。

  8. added 3 commits that reference this issue on Sep 17, 2026
    35939a4
    abe980a
    d0bf810
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions