Repository navigation
Do not finalize the client output format into a canceled buffer - #126142
alexey-milovidov merged 5 commits into
Conversation
Internal second-model review (click to expand)Final review: head 6165f22, four rounds. Gate B on the last delta: 0 findings.
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261015-215600 |
|
Workflow [PR], commit [3d3d937] Summary: ✅
AI ReviewSummaryPR #126142 prevents Final Verdict✅ Approve. |
CI finish ledger - 6165f22Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261016-070011 |
When the stdout of a client dies mid-result (a web terminal session is closed, or a `--pager` exits), the first failed flush cancels `std_out_wrapper`. `ClientBase::resetOutput` then still called `output_format->finalize()`, and formats whose suffix starts with `WriteBuffer::write(char)` (JSON, JSONCompact, JSONStrings, JSONObjectEachRow, ...) hit the `LOGICAL_ERROR` "Cannot write to canceled buffer" check. Debug and sanitizer builds abort: the whole server for the web terminal (reachable since ClickHouse#125558 let `resetOutput` continue past the failed progress-table clear), the client itself for `--pager head`. The two buffers at the bottom of the client output chain, `std_out_wrapper` and the `INTO OUTFILE` file, now set one `ClientBase` flag from `cancelImpl`, and `resetOutput` skips the format finalize once it is set. The teardown also cancels `out_file_buf` instead of finalizing it once the flag is set: `IOutputFormat::flushImpl` flushes a compression buffer's nested buffer directly, so a failing `INTO OUTFILE` file is canceled while the codec above it is not, and finalizing that codec wrote its trailer into the canceled file (an abort for `INTO OUTFILE '/dev/full' COMPRESSION ...` with most formats, already on master). The plain `WriteBuffer::canceled` flag cannot be read there, because a parallel formatting collector or the Pretty squashing thread may still be writing until `output_format.reset()` joins them. No buffer is added between the format and its sink, so successful output and its cost are unchanged, and `FORMAT SQLite` still writes the `INTO OUTFILE` file directly. Tests: 05386 gains the reported `FORMAT JSON` web terminal scenario plus a `system.errors` oracle; 02871 gains a `--pager head` + `FORMAT JSON` arm; 02346 gains `INTO OUTFILE ... AND STDOUT` with a closed stdout, `INTO OUTFILE '/dev/full'` and compressed `INTO OUTFILE '/dev/full'` arms that check the client exit code. Closes ClickHouse#125973 Co-Authored-By: Claude Opus 5.5 <[email protected]>
6165f22 to
ed72572
Compare
There was a problem hiding this comment.
I actually don't like this fix, what we do is workaround our own internal checks, and there are lots of other places as well.
Here we already know that we are writing to partial valid buffer, and we can use this, i.e. have WriteBuffer::tryFinalize (that will not throw LOGICAL_ERROR)/IOutputFormat::tryFinalize.
So we need to have try* variants, otherwise we will have more and more places like this.
But we can merge this as-is and work on the refactoring on top. I will take care.
Since ClickHouse#126638 the client stops quietly with exit code 0 when the reader of its own stdout goes away, also for the stdout leg of `INTO OUTFILE ... AND STDOUT`, so the `closed_stdout` arm no longer sees exit code 75 and `CANNOT_WRITE_TO_FILE_DESCRIPTOR`. The arm still catches the regression: without the fix the debug client aborts in `ClientBase::resetOutput` -> `IOutputFormat::finalize` -> `JSONUtils::writeArrayEnd`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units21 translation units recompiled, 125 s compile time in total, 21 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
macOS has no /dev/full, so on Fast test (arm_darwin) `INTO OUTFILE '/dev/full'` fails to open the file (CANNOT_OPEN_FILE, exit 76) instead of failing to write to it. The full_file and full_file_gzip arms move unchanged to 05461_into_outfile_full_file, tagged no-darwin; the closed_stdout arm stays in 02346 and keeps running on macOS. Co-Authored-By: Claude Opus 5.5 <[email protected]>
CI finish ledger - 4c91c4cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261029-080004 |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok. Somewhat weird. At one hand, we are duplicating the is_cancelled flag, while we can put it into the base class. At the other hand, having atomic flag in the base class could be unjustified.
Closes: #125973
Related: #125558
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Regression from #125558, reported in #125973: closing a web terminal while a
FORMAT JSONresult is streaming aborts a debug or sanitizer server withLogical error: 'Cannot write to canceled buffer'(ClientEmbeddedRunner::clientRoutine->ClientBase::resetOutput->IOutputFormat::finalize->JSONUtils::writeArrayEnd). The same teardown aborts a debug client whose output sink fails:--pager headorINTO OUTFILE ... AND STDOUTpiped intoheadwithFORMAT JSON, andINTO OUTFILE '/dev/full'(JSON-family formats, or almost any format withCOMPRESSION).When the sink dies mid-result, the failed flush cancels the buffer at the bottom of the output chain, but
resetOutputstill finalized what is above it: the format, whose JSON suffix starts withWriteBuffer::write(char)(a logical error on a canceled buffer), and theINTO OUTFILEcompression buffer, which stays uncanceled when only its nested file fails, becauseIOutputFormat::flushImplflushes the nested buffer directly. #125558 letresetOutputcontinue past the failed progress-table clear, which exposed this on the web terminal path.std_out_wrapperand theINTO OUTFILEfile now set a flag fromcancelImpl. Once it is set,resetOutputskips the format finalize and cancelsout_file_bufinstead of finalizing it. The flag is atomic because a parallel formatting collector or the Pretty squashing thread may still write untiloutput_format.reset()joins them. The format writes into the same buffers as before, so successful output and its cost are unchanged.05386 gains the reported scenario, 02871 a
--pager headarm, 02346 a closed-stdout arm, and the newno-darwin05461 (macOS has no/dev/full)/dev/fulland compressed/dev/fullarms; the last three check the exit code. Without the fix the first aborts a master debug server and the others the debug client; with it all pass 50 randomized runs. Official 26.8 and 26.9 builds reportCANNOT_WRITE_TO_FILE_DESCRIPTORon these paths, so no stable release shows a user-visible difference.Workflow [PR]
Sync PR [sync-upstream/pr/126142]
Version info
26.11.1.1649-master(included in26.11and later)