Skip to content

Do not use the client output format after a failed initialization destroyed its buffer - #127912

Merged
Avogar merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-client-output-format-after-failed-init
Oct 28, 2026
Merged

Avogar merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-client-output-format-after-failed-init

Conversation

@groeneai

@groeneai groeneai commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #93298
Related: #125517

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a possible server crash (use-after-free) when a web terminal session is closed while the client asks whether to output a binary format such as RowBinary.

Description

On 26.10.2.103-stable, closing a web terminal session while the client asks Do you want to output it anyway? [y/N] for a binary format (SELECT 1 FORMAT RowBinary) makes the server read a freed buffer: the ASan build of that tag reports heap-use-after-free. Since the Stress test runs queries over the web terminal (#125517), this crashed the server on master, release build included: Stress test (arm_release) and Stress test (amd_debug) (SIGSEGV), Stress test (amd_msan) (use-of-uninitialized-value).

The format created in ClientBase::initOutputFormat refers to std_out_wrapper (or out_file_buf). When the session closes, the read of the answer in ask fails with EIO, and the catch of initOutputFormat destroys both buffers but keeps output_format. The client keeps receiving the result, and resetOutput finalizes the format into the freed buffer.

The catch now destroys the format first, so a failure after the format was created leaves the same state as a failure before it.

The new test waits for the question and closes the WebSocket. Without the fix the ASan server aborts with the CI stack 10/10 times; with it the test passes 50/50 with random settings, and RowBinary, BSONEachRow, Parquet and Native no longer abort it. #126142 is a different defect in resetOutput.

🤖 Generated with Claude Code


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

Version info

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

…troyed its buffer

`ClientBase::initOutputFormat` creates `output_format` over `std_out_wrapper`
(or `out_file_buf`), and the format keeps a reference to that buffer. For a
binary format on a terminal it then asks whether to output it anyway. In the
web terminal, closing the session closes the PTY, the read of the answer fails
with EIO, and the `catch` destroyed `out_file_buf` and `std_out_wrapper` while
keeping `output_format`. `receiveResult` continues after the `LocalFormatError`,
and `resetOutput` then finalized the format into the freed buffer: a heap
use-after-free (ASan), use-of-uninitialized-value (MSan) and SIGSEGV (debug)
in Stress tests since they run queries over the web terminal (ClickHouse#125517).

Destroy the format first in that `catch`, before the buffers it writes into
(the same order as `resetOutput`). A failure after the format was created now
leaves the same state as a failure before it (ClickHouse#93298), and the next result
packet creates the format again.

The new test opens a web terminal session, runs a `RowBinary` query, waits for
the question and closes the connection; the server must stay alive.

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; 2 runs on the same code, the second after a PR description edit; 3 distinct findings, all nits, none on the code or the test).

# Sev Finding Verdict Evidence / action
1 💡 A server crash belongs under Critical Bug Fix (raised in both runs) DISAGREE I leave Critical Bug Fix to a maintainer's request. Bug Fix fits: the defect reproduces on 26.10.2.103-stable (ASan build of the tag) and a release build crashed on master (Stress test (arm_release)).
2 💡 Add a full-URL Related: line for #125517 AGREE, fixed in the description #125517 made the Stress test run queries over the web terminal, which exposed the defect.
3 💡 Add a full-URL Related: line for #126142 (raised in both runs) DISAGREE #126142 is a different defect in resetOutput; the description mentions it only to say that the two do not overlap.

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

Session id: cron:clickhouse-review-slot-8:20261027-041500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @azat, could you review this and add the can be tested label so CI runs? When the web terminal closes while the client asks [y/N] for a binary format, the catch in initOutputFormat destroyed the buffers but kept output_format, so resetOutput finalized it into freed memory (hit on master by the Stress test over the web terminal); the catch now resets the format first.

@clickhouse-gh

clickhouse-gh Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0ecac9e]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

PR #127912 fixes a use-after-free when output-format initialization fails by destroying output_format before releasing the buffers it references. It adds a stateless regression test that closes the web terminal at the binary-format confirmation prompt. No actionable findings in the current diff.

Missing context / blind spots
  • ⚠️ The regression was not rerun locally because this checkout has no runnable ClickHouse binary. The PR reports ASan failures in 10/10 runs before the fix and passes in 50/50 runs after it; targeted sanitizer and bugfix-validation CI results were not yet available at review time. A completed sanitizer run of 05453_webterminal_close_at_binary_format_question would independently confirm the reported results.
Final Verdict

Status: ✅ Approve. No code changes requested; let sanitizer CI and bugfix validation complete before merging.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-cli clickhouse-client (interactive CLI) and client library. labels Oct 27, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 27, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 0ecac9e

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 before comparing anything. 282 of 286 runs since 2026-10-26 22:00 UTC are red, on 236 PRs #126200 (mine, open)
PR praktika rollup of the row above #126200 (the owner of the row above)

The first run of this head stopped at Config Workflow / Pre Hooks (and its Finish Workflow / Post Hooks) because the can be tested label was not set yet; the run after the label was added is the one above. CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261027-203012

@Avogar
Avogar added this pull request to the merge queue Oct 28, 2026
@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Oct 28, 2026
Merged via the queue into ClickHouse:master with commit 956f38d Oct 28, 2026
522 of 528 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 28, 2026
alexey-milovidov added a commit to clickgapai/ClickHouse that referenced this pull request Oct 28, 2026
…cts the readiness probe for load

Since 2026-10-26 18:19 UTC the read-only sub-service of the CI logs cluster
answers every query, including the `SELECT 1` readiness probe and before
authentication, with `Code: 241` (`(total) memory limit exceeded: would use
213.20 GiB ... maximum: 212.40 GiB`). `select` classifies 241 as
`LogClusterUnavailable` (green check), but `is_ready` threw the answer away,
so every read ended with `LogClusterNotReady` and the check was ERROR in
practically every PR.

`is_ready` now logs the status and body of a failed probe and, when the
server itself answered (the `X-ClickHouse-Exception-Code` header is present),
keeps its classification. If no POST was attempted and the last probe was
rejected for load (202, 241), `select` raises `LogClusterUnavailable`. A
missing secret, no connection, a gateway answer, or any other exception code
(516 for a rotated password) still raise `LogClusterNotReady`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=127912&sha=0ecac9e802642ebf566075f5145796c9db1fde22&name_0=PR&name_1=Build%20profile%20diff
Related: ClickHouse#127912

Co-Authored-By: Claude Opus 5.5 <[email protected]>
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Nov 1, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release can be tested Allows running workflows for external contributors comp-cli clickhouse-client (interactive CLI) and client library. pr-bugfix Pull request with bugfix, not backported by default 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.

4 participants