Skip to content

Fix a web terminal query that failed on the client side staying in system.processes - #128209

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:fix-localconnection-resync-ends-abandoned-query
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:fix-localconnection-resync-ends-abandoned-query

Conversation

@groeneai

@groeneai groeneai commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118372
Related: #127913

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 query of a web terminal or SSH session staying in system.processes after it failed on the client side of the session (for example, with MEMORY_LIMIT_EXCEEDED while the result was being received), until the next query of the session. Such a query counted toward max_concurrent_queries_for_user.

Description

On 26.9.25.15, SELECT number, toString(number) FROM numbers(1000000) SETTINGS max_memory_usage = '5M' in a web terminal session can fail on the client side (2 of 18 fresh sessions); the failed query then stays in system.processes while the session is idle, and a query of the same user from another session with max_concurrent_queries_for_user = 1 is rejected with TOO_MANY_SIMULTANEOUS_QUERIES.

Provenance: Stress test (amd_debug) on #118372, Hung check failed: a web terminal query was listed for 1207 s.

Root cause: LocalConnection attaches the client thread to the query, so an exception on that thread mid-exchange escapes as a client exception. ClientBase then resynchronizes with checkConnected: for TCP a ping that drops the desynchronized exchange, for LocalConnection a plain return true, which kept the abandoned query's state (its BlockIO owns the process list entry) until the next query or the end of the session.

Change: LocalConnection::checkConnected ends the abandoned query, as sendQuery already does at the next query; like ~LocalConnection it ignores an exception from the cancellation (the abandoned pipeline's own failure), so it never throws. #127913 covers the other path (a query that fails on the server side and delivers an Exception packet).

Validation: the new test 05386_webterminal_client_error_leaves_processlist (test-only fail point local_connection_receive_packet_throw) prints 1 without the fix and 0 with it (10/10 each, 50/50 randomized). With CI's memory_tracker_fault_probability = 0.05, 96 of 108 injected faults left the query listed before the fix, 0 of 101 after. Web terminal, SSH and clickhouse-local tests: no new failures.


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

`LocalConnection::sendQuery` attaches the client thread to the query, so an
exception on that thread in the middle of the exchange (for example, a memory
limit or an injected memory fault while the client handles a packet) escapes as
a client exception instead of an `Exception` packet. `ClientBase::runInteractive` then
resynchronizes the connection with `checkConnected`. For a TCP `Connection`
that is a ping, which fails on a desynchronized exchange, and the reconnection
drops the query on the server. For `LocalConnection` it returned `true` and
kept the abandoned query state, whose `BlockIO` owns the process list entry,
until the next query of the session or its end. In the web terminal and the
SSH interface the failed query therefore stayed in `system.processes` while the
session was idle at the prompt and counted toward
`max_concurrent_queries_for_user`; in the Stress test such a query was listed
for 1207 s and failed the hung check (PR ClickHouse#118372, `Stress test (amd_debug)`).

`checkConnected` now ends the abandoned query, as `sendQuery` already does at
the next query: cancel it if it is still running and reset the state. Its only
caller is `ClientBase::resynchronizeConnectionAfterError`, which runs outside
the try block of `runInteractive`, so the cancellation is guarded as in
`~LocalConnection`: the executors' `cancel` rethrows the failure of a pipeline
that failed while the client was not waiting for it, and letting it escape
ended the session. The server-side failure path (an `Exception` packet) is
handled separately in ClickHouse#127913.

The test uses a new test-only fail point `local_connection_receive_packet_throw`
that throws on the client thread at the first packet of a query.

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; 1 full pass, no fix round needed).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test does not exercise the catch around sendCancel in LocalConnection::checkConnected (05459) DISAGREE The cancellation rethrows only if the pipeline thread fails after the last pull and before the join in PullingAsyncPipelineExecutor::cancel; holding it there needs a pause inside IProcessor::work(), so no deterministic test exists. Measured with a race probe instead: without the catch 33 of 300 sessions ended, with it 0 of 300.

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

Session id: cron:clickhouse-review-slot-8:20261030-083700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05459_webterminal_client_error_leaves_processlist (ONCE fail point in LocalConnection::receivePacket) prints 1 on every run without the fix (10/10). Real triggers too: CI's memory_tracker_fault_probability = 0.05 (96 of 108 faults lingered), and max_memory_usage = '5M' on official 26.9.25.15 (2 of 18 sessions).
b Root cause explained? The client thread runs attached to the query, so an exception on it mid-exchange escapes as a client exception; runInteractive resynchronizes via checkConnected, which for LocalConnection was return true, so the abandoned state (BlockIO owning the process list entry) stayed until the next query or session end.
c Fix matches root cause? Yes. checkConnected (the resynchronization primitive, only caller resynchronizeConnectionAfterError) ends the abandoned query, covering every client-side throw site instead of guarding one.
d Test intent preserved / new tests added? New test 05459 (web terminal session, fail point, system.processes count while the session idles, then checks that the session still works).
e Both directions demonstrated? Yes, on Build-ID-verified binaries: fail point 10/10 1 vs 10/10 + 50/50 0; fault loop 96 lingering vs 0; real max_memory_usage trigger 3/18 vs 0; SSH 3/3 vs 0/3; max_concurrent_queries_for_user = 1 rejected vs accepted. Negative control: without enabling the fail point the test times out waiting for the error.
f Fix is general across code paths? Web terminal, SSH and interactive clickhouse-local share LocalConnection + runInteractive. The server-side Exception packet path is the separate #127913. TCP Connection::checkConnected unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? Protocol-state bug, not type dependent; exercised with Pretty, TSV and Null output and with faults at the first and at later packets.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change.
i Invariants and contracts preserved? An exchange that is over (completed, failed or abandoned) leaves no query state; the release is the one ~LocalConnection already does (sendCancel if running, exceptions ignored, then reset), plus next_packet_type and last_sent_snapshots as in finishQuery. checkConnected never throws, like Connection::checkConnected: an unguarded cancellation rethrew the pipeline's own failure outside runInteractive's try and ended 33 of 300 sessions in a race probe, 0 with the guard. The pushing executors set finished before they can throw, so their destructors' chassert holds.

Session id: cron:clickhouse-impl-slot-5:20261030-064900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @azat @Algunenano, could you review this and add the can be tested label so CI runs? When the client side of a web terminal, SSH or clickhouse-local session throws mid-query, ClientBase resynchronizes through checkConnected, which was a no-op for LocalConnection, so the abandoned query stayed in system.processes until the next query; now checkConnected ends it.

@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4a3a5f8]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 1/6) FAIL Performance dashboard
Check Results FAIL
Performance Comparison (arm_release, master_head, 3/6) FAIL Performance dashboard
Check Results FAIL

AI Review

Summary

PR #128209 makes LocalConnection::checkConnected cancel and release an abandoned query during client error recovery, preventing it from occupying system.processes while the session is idle. Reviewed head 4a3a5f8c0e036797cd4923f5daad70729458c78c and all prior discussion. No actionable findings.

Missing context / blind spots
  • ⚠️ Relevant CI jobs for the current head are pending; server tests were not run locally. The unchanged regression test passed 25 repetitions in debug CI and 50 in targeted ASan/UBSan CI. The subsequent commit only renames the test files. Completion of the head CI jobs closes this remaining validation gap.
Final Verdict

Status: ✅ Approve. No code changes requested.

@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 30, 2026
@tiandiwonder tiandiwonder self-assigned this Oct 30, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Oct 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4a3a5f8c0 with master 4e3df38e8 (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

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

Median compile-time ratio to the baselines is ×1.05 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost +7.5 s (+5%) in total before that adjustment.

Job report

Keep it next to the related 05386_webterminal_close_during_query test,
as asked in review.

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

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 4a3a5f8

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
Performance Comparison (arm_release, master_head, 1/6) infra, not from this PR: the shard's own report has 0 slower queries (4 too long), but the performance dashboard answered HTTP 500 (Unknown table expression identifier 'perf_test_times_v1') at the end of its 600 s wait, and the harness treats that answer as a hard error, so the shard fails instead of being judged by its own report a fix task is created (investigating at full effort, fixing-PR link to follow here)
Performance Comparison (arm_release, master_head, 3/6) the same dashboard HTTP 500 as the row above; own report 5 too long, 0 slower a fix task is created (the same task as the row above)
Mergeable Check, PR praktika rollups of the rows above a fix task is created (the same task as the rows above)
Sync - CH Inc sync (private, not actionable by me)

Session id: cron:our-pr-ci-monitor:20261030-150005

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the Performance Comparison (arm_release, master_head, 1/6 and 3/6) dashboard HTTP 500 failures: #128255

huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Oct 30, 2026
On 2026-10-30 the performance dashboard backend was switched to a new
CIDB (explained by egor-click on ClickHouse#128236). For about 3 minutes
(14:09:54Z to 14:13:05Z, measured from the polls of 6 shards) it
answered HTTP 500 with ClickHouse code 60 for the tables it reads:

  failed to resolve performance comparison run: code: 60, message:
  Unknown table expression identifier 'perf_test_times_v1'
  failed to load performance comparison rows: code: 60, message:
  Unknown table expression identifier 'query_metrics_v2'

`dashboard_did_not_answer` knew no signature for this message, so the
500 was a `PerfDashboardError`; the poll loop keeps the first hard
error, and `main` failed the shard instead of judging it by its own
report. Four `Performance Comparison (arm_release, master_head, N/6)`
jobs whose 600 s wait ended inside the window failed this way
(ClickHouse#126879 6/6, ClickHouse#128209 1/6 and 3/6, ClickHouse#128097 2/6), all with 0 slower
queries. Shards whose wait outlived the window got the same requests
answered with 200 and passed.

The request cannot cause this answer: the table names are the
backend's own storage, and identical requests (same run id and test)
were served after the window. It is the case the class documents,
"the dashboard, or the CIDB tables it reads, did not answer", so add
the measured signature next to the two existing ones. Only a 5xx with
an application envelope is affected; a 4xx with this text, a non-JSON
body, and every other 5xx keep their classification, and a real hard
error seen before or after it still wins.

Validated locally against a fake dashboard serving the logged bodies:
the incident replay fails on master and reaches the own-report
fallback with the fix.

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

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-cli clickhouse-client (interactive CLI) and client library. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants