Skip to content

[Bug]: Top returns spilled results after cancellation arrives at input EOF #26447

Closed

Description

@Ariznawlll

[Bug]: Top returns spilled results after cancellation arrives at input EOF

Summary

The spill-enabled Top operator consumes input, then enters its evaluation
phase when its child returns EOF. If the process context is canceled
immediately after that EOF child call, Top still reads its temporary spill
file and returns an output batch instead of context.Canceled.

This makes a canceled large ORDER BY ... LIMIT statement continue doing
spill-file I/O and emit data after the pipeline has been canceled.

Black-box confirmation

On origin/main, the following query uses a real spilled Top/Sort execution
path (the plan reports SpillRows=600000 and SpillSize=4.58 MiB):

SELECT id, k
FROM spill_core.fact_big
ORDER BY k DESC, id ASC
LIMIT 20000;

Focused reproduction

The deterministic white-box reproducer is:

pkg/sql/colexec/top/top_test.go
TestTopSpillEvalHonorsCancellationAfterInput

It sets LIMIT to topSpillThreshold + 1, supplies three 8,192-row batches
to force Top's temporary-file mode, and uses a child that cancels the process
exactly when it returns EOF. The Top call has already created a spill file
when it starts its evaluation phase.

Run:

go test ./pkg/sql/colexec/top \
  -run TestTopSpillEvalHonorsCancellationAfterInput -count=1 -v

Actual result

The test fails because Top.Call returns a non-nil result batch and a nil
error after cancellation:

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

Expected result

Once the pipeline context is canceled, Top should stop before spilled-result
materialization, return the cancellation error, and let the standard
reset/free path remove its temporary spill file.

Reproducibility

Reproduced twice independently on origin/main at ebdc3de0f:

  1. First focused run returned a normal result after cancellation.
  2. Second focused run returned the same result; the command exited with code
    1 only because the test correctly expected context.Canceled.

Suspected area

pkg/sql/colexec/top/top.go invokes vm.ChildrenCall while building input,
but after the EOF transition calls evalSpill without another
vm.CancelCheck(proc). evalSpill reads spillFile and materializes an
output chunk without consulting the canceled process context.

Scope

This report is limited to Top's temporary-file spill 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/#26446 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 单测;受控 Top spill heap 与可取消 process.Context。
    • 测试日期:2026-08-05

    原问题验证

    执行 go test -count=3 ./pkg/sql/colexec/top 与 go test -race ./pkg/sql/colexec/top。TestTopSpillEvalHonorsCancellationAfterInput 覆盖输入 EOF 后取消、Top 不得继续物化 spilled result 的路径。

    Happy Path 覆盖

    1. TestTop、TestTopSpill:内存与 spill Top 结果一致。
    2. TestTopCopiesNullVarlenaRow:NULL/varlen 行拷贝正确。
    3. TestTopSpillInsufficientRows 与 TestTopSpillMaxUint64LimitReturnsAllRows:不足 limit 及极大 limit 的边界行为正确。

    Unhappy Path 覆盖

    1. EOF 后取消:TestTopSpillEvalHonorsCancellationAfterInput。
    2. spill write 的取消前/后边界:TestTopSpillBatchCancellationBeforeWrite、TestTopSpillBatchCancellationAfterWrite。
    3. 计算、heap、batch materialization 的取消检查点:TestTopSpillEvalCancellationCheckpoints、TestTopSpillHeapCancellationCheckpoint、TestTopSpillWriteHonorsCancellationAfterInputBatch。
    4. 断言取消后不发布部分 Top 结果。

    白盒验证

    针对 top_test.go 完成 3 轮 UT 与 -race;验证 spill heap、临时 batch 和取消后所有权。

    回归测试

    该问题的关键是输入 EOF 与 Top 内部 materialization 之间的取消时序,现由 Go UT 固定注入;不新增普通 motr SQL 用例。

    测试证据

    执行命令:go test -count=3 ./pkg/sql/colexec/top、go test -race ./pkg/sql/colexec/top,均以 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