Repository navigation
Description
[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 -vActual 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:
- First focused run returned a normal result after cancellation.
- Second focused run returned the same result; the command exited with code
1 only because the test correctly expectedcontext.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
XuPeng-SH commentedon Aug 1, 2026
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
ChildrenCallsemantics or introduce a general cancellation/I/O framework.fix(colexec): honor cancellation at spill phase boundaries (#26675)
Ariznawlll commentedon Aug 5, 2026
测试版本
mainace13bf4b43773dd962f03325b22f58ed31fee91process.Context。原问题验证
执行
go test -count=3 ./pkg/sql/colexec/top与go test -race ./pkg/sql/colexec/top。TestTopSpillEvalHonorsCancellationAfterInput覆盖输入 EOF 后取消、Top 不得继续物化 spilled result 的路径。Happy Path 覆盖
TestTop、TestTopSpill:内存与 spill Top 结果一致。TestTopCopiesNullVarlenaRow:NULL/varlen 行拷贝正确。TestTopSpillInsufficientRows与TestTopSpillMaxUint64LimitReturnsAllRows:不足 limit 及极大 limit 的边界行为正确。Unhappy Path 覆盖
TestTopSpillEvalHonorsCancellationAfterInput。TestTopSpillBatchCancellationBeforeWrite、TestTopSpillBatchCancellationAfterWrite。TestTopSpillEvalCancellationCheckpoints、TestTopSpillHeapCancellationCheckpoint、TestTopSpillWriteHonorsCancellationAfterInputBatch。白盒验证
针对 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 完成。测试结论
测试通过。
fix(colexec): honor cancellation at spill phase boundaries (matrixori…
fix(colexec): honor cancellation at spill phase boundaries (matrixori…
fix(colexec): honor cancellation at spill phase boundaries (matrixori…