Skip to content

Only drain the rest of the result after a local format error in the client - #128238

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-client-drain-after-local-format-error
Nov 1, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-client-drain-after-local-format-error

Conversation

@groeneai

@groeneai groeneai commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #128090
Related: #127912

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

Follow-up requested in #128090 (comment).

After a LocalFormatError (the client failed to create or write its output), receiveResult sent Cancel but still passed every remaining Data / Totals / Extremes packet to onData / onTotals / onExtremes. When the error came from initOutputFormat, output_format was null afterwards, so every remaining block built the output again:

  • the INTO OUTFILE file was opened again for every block, and the pager was started again;
  • if the failure went away during the query, later blocks were written after earlier ones were lost, while the client still exited with the first error;
  • since Do not use the client output format after a failed initialization destroyed its buffer #127912 the binary-format question was asked again and read the stdin that the failed first answer canceled: Logical error: 'ReadBuffer is canceled. Can't read from it.' from ask <- initOutputFormat, which aborts debug builds: failing master job.

A per-query flag is now set where a LocalFormatError is created (initOutputFormat, the write in onData, the background error check in receiveResult), checked at the top of the three callbacks, and reset in processParsedSingleQuery. Setting it at these three sites rather than in receiveResult also covers the INSERT ... RETURNING drain, which calls the callbacks directly. cancelled is not reused: it would turn off the receive timeout and Ctrl+C handling while draining, skip waiting for the pager and print "Query was cancelled.". The ask guard of #128090 is kept.

The new test counts pager starts for a failing FORMAT Template over three one-row blocks: 1 with the fix, 4 without. Without the fix, #128090's scenario asks twice and aborts; with it, once.

Not for changelog: the abort needs #127912, which is only on master; in 26.10 the rebuild only repeats work after an error the client already reports.


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

Version info

  • Merged into: 26.11.1.1023-master (included in 26.11 and later)

…lient

After a `LocalFormatError` (the client failed to create or write its output
format), `receiveResult` sends `Cancel` but kept passing every remaining
`Data` / `Totals` / `Extremes` packet to `onData` / `onTotals` /
`onExtremes`. When the error came from `initOutputFormat`, `output_format`
is null afterwards, so every remaining block built the output again: the
pager was started again, the `INTO OUTFILE` file was opened again, and a
failure that went away mid-result left an output with a gap while the client
still exited with the first error. Since ClickHouse#127912 the binary-format question
is asked again too, reading the stdin the first failed answer canceled
(`LOGICAL_ERROR 'ReadBuffer is canceled'`, STID 2508-2bbd on master).

A per-query flag is set where a `LocalFormatError` is created (the three
sites: `initOutputFormat`, the write in `onData`, the background error check
in `receiveResult`), checked at the top of the three result callbacks, and
reset in `processParsedSingleQuery`. Setting it at the origins instead of in
`receiveResult` also covers the `INSERT ... RETURNING` drain
(`receiveEndOfQueryForInsert` calls the callbacks directly). `cancelled` is
not reused: it would disable the receive timeout and Ctrl+C handling in the
drain loop, skip waiting for the pager and print "Query was cancelled.".

Requested by @PedroTadim in
ClickHouse#128090 (comment)

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; 1 finding; plus my own review, 2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ 05473 exercises only the initOutputFormat failure; the flag set at the write in onData and at the background error check, the onTotals / onExtremes checks and the INSERT ... RETURNING drain are not run by it DISAGREE, noted The test fails without the fix (10/10) and when the onData check or the initOutputFormat set is removed. The other sites are the same one-line set / early return. INSERT ... RETURNING reaches the same onData check, measured locally: 2 pager starts without the fix, 1 with it. The write and background errors have no deterministic trigger in a stateless test.
2 💡 After a failed write, onEndOfStream still finalizes the format (suffix only, no result block) DISAGREE, out of scope Unchanged by this PR; an initialization failure leaves no format to finalize.

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

Session id: cron:clickhouse-review-slot-9:20261030-133300

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: 05473 with a failing FORMAT Template over 3 one-row blocks (interactive_delay = 10000000): the pager starts 4 times without the fix, 10/10.
b Root cause explained? After a LocalFormatError the client cancels the query but still passes the remaining result packets to onData / onTotals / onExtremes; with output_format null they rebuild the output (pager, INTO OUTFILE, format, binary-format question) for every block.
c Fix matches root cause? Yes: a per-query flag set where LocalFormatError is created stops the callbacks; the rest of the result is only drained.
d Test intent preserved / new tests added? New stateless test 05473; removing the check in onData or the set in initOutputFormat makes it fail.
e Both directions demonstrated? Yes: 4 vs 1 pager starts, 10/10 each; 100/100 randomized runs with the fix (incl. -j 8).
f Fix is general across code paths? All three LocalFormatError sites set the flag; all three callers of initOutputFormat check it; covers both drains (receiveResult and INSERT ... RETURNING).
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked: INTO OUTFILE ... APPEND (4 file opens -> 1), a target directory that appears mid-query (rows 1 2 3 written -> nothing), the binary-format question (abort -> one question), INSERT ... RETURNING (2 pager starts -> 1). Data types N/A.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change; output differs only after an error the client already reports.
i Invariants and contracts preserved? cancelled keeps its meaning (timeout, Ctrl+C, pager wait); Exception / EndOfStream / progress handling and the rethrow of the first error are unchanged; the flag is reset per query.

Session id: cron:clickhouse-impl-slot-4:20261030-122900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @azat @Avogar, could you review this and add the can be tested label so CI runs? After a local format error the client now only drains the rest of the result: a per-query flag set where LocalFormatError is created makes onData / onTotals / onExtremes return early, so the output (pager, INTO OUTFILE, the binary-format question) is no longer rebuilt for every remaining block.

@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [181c8a7]

Summary: ✅


AI Review

Summary

The client now skips further result formatting after a LocalFormatError, preventing repeated pager launches, output-file opens, and binary-format prompts while draining remaining packets. No actionable findings. The focused regression test 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-cli clickhouse-client (interactive CLI) and client library. labels Oct 30, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 181c8a730 with master 941eb115e (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 32cf84552; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

26 object files changed (+268.75 KiB total), 3 added.

Object file Master PR Δ
utils/compact-symbols/CMakeFiles/compact-symbols.dir/__/__/src/Common/CompactSymbols.cpp.o new 87.89 KiB +87.89 KiB
src/CMakeFiles/clickhouse_common_io.dir/Common/CompactSymbols.cpp.o new 87.89 KiB +87.89 KiB
src/CMakeFiles/clickhouse_common_io.dir/Common/SymbolIndex.cpp.o 95.20 KiB 146.02 KiB +50.81 KiB (+53.37%)
utils/compact-symbols/CMakeFiles/compact-symbols.dir/compact-symbols.cpp.o new 37.38 KiB +37.38 KiB

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

21 translation units recompiled, 134 s compile time in total, 21 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 1, 2026
Merged via the queue into ClickHouse:master with commit 958c7e7 Nov 1, 2026
530 of 532 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 1, 2026
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-cli clickhouse-client (interactive CLI) and client library. pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants