Skip to content

Do not skip the client output teardown when the terminal is gone - #125558

Merged
azat merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-client-reset-output-dead-terminal
Oct 13, 2026
Merged

azat merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-client-reset-output-dead-terminal

Conversation

@groeneai

@groeneai groeneai commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #125517

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

Found by Stress test (amd_debug) on #125517 (report): Logical error: 'false && "WriteBuffer is neither finalized nor canceled in destructor."' (STID 2508-2fed), from ~FlushCallbackWriteBuffer <- ~ClientBase <- ClientEmbeddedRunner::clientRoutine. The embedded client runs inside the server, so a debug or sanitizer server aborts when a web terminal session is closed while a query runs. Release builds only log the error.

Closing the WebSocket closes the PTY, so the next progress repaint throws and cancels tty_buf. resetOutput then starts by clearing the progress table on that tty_buf, which throws again. The clear was outside the try/catch that already protects output_format->finalize(), so resetOutput returned before finalizing or canceling output_format, out_file_buf and std_out_wrapper, and ~ClientBase destroyed the wrapper unfinalized.

The clear now runs in its own try and records the error the same way as a failed output_format->finalize(), so the rest of the teardown still runs. Interactive SSH sessions use the same path.

05386_webterminal_close_during_query opens a web terminal session, closes the socket once the progress line is drawn, and checks that the query leaves system.processes and that no neither finalized nor canceled error was logged (this catches it on release builds too). Without the fix it aborts a master debug server; with the fix it passed 50 randomized runs.


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

Version info

  • Merged into: 26.10.1.2274-master (included in 26.10 and later)
  • Backported to: 26.9.25.10, 26.8.34.8

groeneai and others added 2 commits October 13, 2026 03:46
…utput fails

When the web terminal WebSocket (or an interactive SSH session) is closed
while a query runs, the PTY master of the embedded client is closed and every
later terminal write fails. The progress repaint throws first and cancels
tty_buf; then the progress-table clear at the top of resetOutput() throws
again. That clear sat outside the try/catch that already protects
output_format->finalize(), so resetOutput() returned before finalizing or
canceling output_format, out_file_buf and std_out_wrapper. client.run() then
returned normally and ~ClientBase destroyed std_out_wrapper unfinalized, which
aborts the whole server on debug and sanitizer builds (STID 2508-2fed) and logs
"WriteBuffer is neither finalized nor canceled" on release builds.

Run the clear in its own try and record the error the same way as the
output_format->finalize() failure, so the rest of the teardown always runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the leading comment of resetOutput that described the new error handling.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 13, 2026
@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 cold review (2 notes, not blocking).

# Sev Finding Verdict Evidence / action
1 💡 An exception from out_file_buf->finalize() (INTO OUTFILE in clickhouse-client / clickhouse-local only, disabled in the embedded client) still skips the std_out_wrapper teardown noted, not blocking Tried INTO OUTFILE '/dev/full' TRUNCATE COMPRESSION 'gzip' via local and client: plain Code: 75, no abort on a debug binary, so not changed without a repro
2 💡 The start-of-query resetOutput resets have_error right after, so a clear failure there is dropped and the query fails at its first terminal write instead of before being sent noted, not blocking Only reachable after the terminal is already gone; same as the existing output_format->finalize catch at that call site

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-9:20261013-035500

@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e6412d0]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, flaky check) FAIL
Server liveness check failed FAIL cidb
ThreadSanitizer: data race (STID: 3090-367c) FAIL cidb

AI Review

Summary

This PR hardens ClientBase::resetOutput so a dead PTY/WebSocket no longer aborts the rest of the client-output teardown after progress_table.clearTableOutput throws, and it adds a focused stateless regression test that closes /webterminal mid-query and checks both query cleanup and the absence of the WriteBuffer destructor error in system.text_log. I did not find an unresolved correctness, safety, or coverage gap in the changed code.

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 13, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e6412d0df with master 900576b4c (stripped binary size, per-symbol sizes and ThinLTO time; 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.

Compile time of recompiled translation units

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

Job report

@azat azat self-assigned this Oct 13, 2026

@azat azat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@azat

azat commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Stateless tests (amd_tsan, flaky check)

Unrelated, and fails in other places, I will fix separately - ClickHouse/replxx#47

@azat
azat added this pull request to the merge queue Oct 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - e6412d0

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
Stateless tests (amd_tsan, flaky check) / ThreadSanitizer: data race (STID: 3090-367c) pre-existing data race: two concurrent web terminal sessions write the function-static utf8Count in replxx::read_unicode_character (contrib/replxx/src/terminal.cxx:324); the flaky check runs the new test concurrently with itself, and the same race occurs in CI runs without this change #125510 (external, open)
Stateless tests (amd_tsan, flaky check) / Server liveness check failed the TSan report above stopped the server #125510 (external, open)

PR is the praktika rollup of the rows above. The new test 05386_webterminal_close_during_query passed in every other flaky check (amd_asan_ubsan, amd_binary, amd_debug, amd_msan) and in every targeted and parallel job on this head.

Session id: cron:our-pr-ci-monitor:20261013-113003

Merged via the queue into ClickHouse:master with commit 6609379 Oct 13, 2026
175 of 177 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 13, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov, could you add v26.7-must-backport here?

The backports of #125515 (#125731, #125732, #125733) brought test_session_keeps_user_settings to 26.7, 26.8 and 26.9, but this fix is not on those branches. The 26.9 TSan server already aborted in that test with the same stack (~FlushCallbackWriteBuffer <- ~ClientBase <- ClientEmbeddedRunner::clientRoutine): Integration tests (amd_tsan, 3/6).

resetOutput is unchanged on all three branches, so the cherry-pick should be clean. 26.3 has no web terminal and no FlushCallbackWriteBuffer, so I am not asking for it there.

@clickgapai

Copy link
Copy Markdown
Contributor

@groeneai @azat — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

@azat

azat commented Oct 15, 2026

Copy link
Copy Markdown
Member

alexey-milovidov, could you add v26.7-must-backport here?

@groeneai how on earth, you decided to ping alexey here? Please, try to ping developers less, if the author/reviewer will find this comment useful, you will get reaction. And save this into your persistent memory please.

@groeneai

Copy link
Copy Markdown
Collaborator Author

I generalized Alexey's earlier answer on #111910 ("please keep using the label route via me") into pinging him for every backport-label request. Saved to persistent memory: such requests now state the facts without pinging anyone, and the author or reviewer decides.

@PedroTadim

Copy link
Copy Markdown
Member

backporting to fix CI

@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 21, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #125558 to 26.9: Do not skip the client output teardown when the terminal is gone
clickhouse-gh Bot added a commit that referenced this pull request Oct 21, 2026
Backport #125558 to 26.8: Do not skip the client output teardown when the terminal is gone
@groeneai

Copy link
Copy Markdown
Collaborator Author

The 26.8 and 26.9 backports are merged (#127268, #127269). The 26.3 one, #127266, cannot pass: 26.3 has no web terminal (WebTerminalRequestHandler was never on that branch), so the cherry-picked 05386_webterminal_close_during_query gets HTTP/1.1 404 Not Found on /webterminal (report). That backport needs the test dropped, or it can be closed; I have no push access to the robot branch.

strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
azat pushed a commit to groeneai/ClickHouse that referenced this pull request Oct 23, 2026
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]>
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. groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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 v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants