Skip to content

Report the original error instead of CANNOT_WRITE_AFTER_BUFFER_CANCELED after a failed adaptive aggregation spill write - #128016

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-adaptive-aggregation-shared-spill-stream-error
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-adaptive-aggregation-shared-spill-stream-error

Conversation

@groeneai

@groeneai groeneai commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Description

Related: #123531. Reported in #109052 (comment): 00284_external_aggregation_2 failed with CANNOT_WRITE_AFTER_BUFFER_CANCELED while executing AggregatingTransform in Stateless tests (amd_asan_ubsan, distributed cache, meta in keeper, s3 storage, parallel, 1/2) of the private sync run (https://github.com/ClickHouse/clickhouse-private/actions/runs/40430348969/job/121952004416).

Since #123531 the adaptive aggregator spills its staged records to one temporary stream per partition (SpilledPartition), which all producer threads append to under the partition's mutex. When one write to such a stream throws (the temporary data limit, a failed write of the temporary file), WriteBuffer::next cancels the stream, and the next producer that appends to it gets CANNOT_WRITE_AFTER_BUFFER_CANCELED. The executor reports the exception of the lowest-numbered thread, so this secondary error often replaces the real one.

SpilledPartition::append now keeps the exception of a failed write and rethrows it to every later append, so the stream takes no more writes after a partial block and the query reports the error that broke it. A failed stream creation and the free space check are unchanged: neither leaves a written stream behind. The change is master-only (SpilledPartition is not in 26.10).

This does not make that run green: if a failed write broke the stream there, as in the test, its next occurrence reports that error instead of 734.

The new test fails the writes with max_temporary_data_on_disk_size_for_query: before the change 10 of 10 runs print CANNOT_WRITE_AFTER_BUFFER_CANCELED, after it 100 of 100 print only TOO_MANY_ROWS_OR_BYTES.

The bucket files of GraceHashJoin are also written by several threads and have the same masking; that is separate code and not changed here.


Workflow [PR]
Sync PR [sync-upstream/pr/128016]

Since ClickHouse#123531 the adaptive aggregator spills its staged records to one
temporary stream per partition (`AdaptiveAggregationSession::SpilledPartition`),
and every producer thread appends to it under the partition's mutex. When a
write to such a stream throws (the temporary data limit, a failed write of the
temporary file, a distributed cache write), `WriteBuffer::next` cancels the
stream. The next producer that appends to the same partition then writes to a
canceled buffer and gets `CANNOT_WRITE_AFTER_BUFFER_CANCELED`, and since
`ExecutorTasks::rethrowFirstThreadException` reports the exception of the
lowest-numbered executor thread, that secondary error often replaces the real
one. The same error was reported for `00284_external_aggregation_2` with
temporary data in the distributed cache.

Remember the exception of a failed write in the partition and rethrow it to
every later append, so the stream takes no more writes after a partial block
and the query reports the error that broke it. Only the writes are covered: a
failed stream creation leaves no stream behind, and the free space check
throws before anything is written.

The test makes the temporary data limit fail the writes. Before the change
10 of 10 runs report `CANNOT_WRITE_AFTER_BUFFER_CANCELED`, after it 100 of
100 report only `TOO_MANY_ROWS_OR_BYTES`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The test comment says the runner randomizes the pinned thresholds, but $CLICKHOUSE_LOCAL gets no random settings (tests/queries/shell_config.sh) AGREE, not changed Same wording as 05448_adaptive_aggregator_staged_spill_append_free_space.sh; the pins are still needed for the adaptive spill path.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261028-012500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. max_temporary_data_on_disk_size_for_query = 65536 with the pinned adaptive spill settings of the new test: 10 of 10 runs print CANNOT_WRITE_AFTER_BUFFER_CANCELED before the change.
b Root cause explained? A write to a per-partition spill stream shared by all producers throws, WriteBuffer::next cancels the stream, the next producer's append writes to the canceled buffer (734), and the executor reports the lowest-numbered thread's exception, so 734 replaces the original error.
c Fix matches root cause? Yes. SpilledPartition::append keeps the exception of a failed write and rethrows it to every later append; nothing is swallowed or remapped.
d Test intent preserved / new tests added? New 05453_adaptive_aggregator_staged_spill_append_error.sh (clickhouse-local, no tags). Existing adaptive aggregator tests unchanged and passing.
e Both directions demonstrated? Before: 10 of 10 runs fail with the extra CANNOT_WRITE_AFTER_BUFFER_CANCELED line. After: 100 of 100 pass (50 with randomized settings, 50 without).
f Fix is general across code paths? Both writes of append are covered; stream creation and the free space check throw before anything is written. The bucket files of GraceHashJoin have the same masking in separate code and are not changed here.
g Fix generalizes across inputs (params/datatypes/wrappers)? The defect is in the byte stream, independent of key types. Limits of 64 KiB, 256 KiB and 1 MiB all reproduce before and pass after.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change; master-only code (SpilledPartition is not in 26.10).
i Invariants and contracts preserved? write_failure is guarded by the partition's existing mutex; the record count still grows only on a successful append; a failed stream is never read back because the failing producer fails the query.

Session id: cron:clickhouse-impl-slot-5:20261028-005400

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @nihalzp @KochetovNicolai, could you review this and add the can be tested label so CI runs? When a write to a shared SpilledPartition stream throws, WriteBuffer::next cancels the stream and the next producer gets CANNOT_WRITE_AFTER_BUFFER_CANCELED, which the executor can report instead of the real error; append now keeps the first write failure and rethrows it to later appends.

@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ca1defc]

Summary: ❌

job_name test_name status info comment
Fast test (arm_darwin) FAIL
05227_mutate_part_ahead_of_table_metadata_version_mergetree FAIL cidb
Build profile diff ERROR

AI Review

Summary

PR #128016, reviewed at ca1defc5e5965cef764a658334a4600bdd24a980, preserves the first write exception in each shared adaptive aggregation spill stream and rethrows it on subsequent appends, preventing CANNOT_WRITE_AFTER_BUFFER_CANCELED from masking the cause. No actionable findings. The new regression test, 05453_adaptive_aggregator_staged_spill_append_error, passed in Fast test.

Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). labels Oct 28, 2026
@nihalzp nihalzp self-assigned this Oct 28, 2026
@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Oct 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - ca1defc

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build profile diff infra, not from this PR: the CI logs cluster never passed the readiness probe, so the job ended with LogClusterNotReady: the endpoint never became ready #126200 (mine, open)
Fast test (arm_darwin) / 05227_mutate_part_ahead_of_table_metadata_version_mergetree flaky, not from this PR (the test is not in its diff): the rewrite dropped the column: 1 instead of 0, failing on 111 PRs in 14 days; the tested merge tree predates the fix #127925 (mine, merged)
Mergeable Check, PR praktika rollups of the rows above #126200, #127925 (the owners of the rows above)

All five stateless flaky check builds passed on this head, and CH Inc sync is green. No job was dropped. The two earlier Config Workflow failures were the attempts before the can be tested label.

Session id: cron:our-pr-ci-monitor:20261028-163006

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants