Skip to content

Fix a failed web terminal query staying in system.processes until the next query of the session - #127913

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-localconnection-exception-ends-query
Nov 10, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-localconnection-exception-ends-query

Conversation

@groeneai

@groeneai groeneai commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #127364
Related: #126995

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 failed query staying in system.processes while the web terminal or SSH session that ran it was idle. Such a query counted toward max_concurrent_queries_for_user, and KILL QUERY ... SYNC on it hung.

Description

On 26.8.28.14, after SELECT throwIf(number = 0) FROM numbers(1) fails in a web terminal session, the query stays in system.processes while the session is idle at the prompt; from another client SELECT 1 SETTINGS max_concurrent_queries_for_user = 1 is rejected with TOO_MANY_SIMULTANEOUS_QUERIES, and KILL QUERY ... SYNC on it hangs.

Provenance: Stress test (arm_asan_ubsan), Hung check failed: a web terminal query that failed on an injected memory fault was listed for 45 minutes. That session itself leaked because of the PTY inheritance addressed by #126995 (open); this PR fixes the stale entry, which also affects any ordinary idle session.

Root cause: LocalConnection released the query state (the BlockIO owning the process list entry) only in finishQuery (EndOfStream), in the next sendQuery, or on destruction. A query that failed after executeQuery returned delivered its Exception packet and kept its state.

Change: receivePacket resets the state when it hands out the Exception packet, as finishQuery does for EndOfStream and as TCPHandler does with its per-query state. sendCancel can then be reached with no query, so it gets the same if (!state) return; guard as #127364 (identical hunk, so the two PRs merge in either order).

Validation: the new test 05453_webterminal_query_error_leaves_processlist prints 1 on master and 0 with the fix (5/5 each); 50/50 randomized runs and 50 with -j 8 pass; the webterminal|ssh and _local selections show no new failures against the unpatched build.


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

Version info

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

`LocalConnection` released the query state (the `BlockIO` that owns the
process list entry, and the `QueryScope` of the client thread) only in
`finishQuery` (the `EndOfStream` path), in the next `sendQuery`, or when the
connection was destroyed. A query that failed after `executeQuery` returned
delivered its `Exception` packet and kept its state, so in the web terminal
and the SSH interface it stayed in `system.processes` while the session was
idle at the prompt: it counted toward `max_concurrent_queries_for_user`, and
`KILL QUERY ... SYNC` on it did not return. In the Stress test a leaked web
terminal session kept such a query listed for 45 minutes and failed the hung
check.

Reset the state when the `Exception` packet is handed to the client, as
`finishQuery` does for `EndOfStream` and as `TCPHandler` does with its
per-query state. `sendCancel` can now be reached with no query (the catch of
`ClientBase::processInsertQuery`), so it ignores the cancel then, with the
same guard as ClickHouse#127364.

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; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The new test inlines the same WebSocket client as 05386 and 05445 DISAGREE Follows the existing per-test precedent; sharing it would mean editing two existing tests in a Bug Fix PR. No action.

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

Session id: cron:clickhouse-review-slot-9:20261027-043400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05453_webterminal_query_error_leaves_processlist prints 1 on unpatched master builds (5/5, master snapshot and the same HEAD without the fix) and 0 with the fix (5/5).
b Root cause explained? LocalConnection released the query state (the BlockIO owning the process list entry) only on EndOfStream, at the next query, or on destruction. A query that failed after executeQuery returned delivered its Exception packet and stayed registered while the session was idle.
c Fix matches root cause? Yes. receivePacket ends the query when it hands out the Exception packet, as finishQuery does for EndOfStream and as TCPHandler does per query.
d Test intent preserved / new tests added? New stateless test through /webterminal: a pipeline exception, then system.processes checked over HTTP while the session is still open. No existing test changed.
e Both directions demonstrated? Yes, on Build-ID-verified servers: 1 without the fix, 0 with it. 50/50 randomized runs, 50 more with -j 8 and 200 with -j 32 pass.
f Fix is general across code paths? Every failure path (sendQuery, poll, pushing executors, sendData) stores into the state and is delivered only by this case, so one site covers them. sendCancel gets the guard from #127364 (identical hunk) for the new empty-state case.
g Fix generalizes across inputs (params/datatypes/wrappers)? Input-independent (connection lifetime). Checked with pipeline errors, INSERT parse errors, multi-query batches with --ignore-error and test hints in clickhouse-local: errors reported, later queries run.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, protocol or format change. receivePacket after an Exception now returns EndOfStream instead of repeating the exception; no caller reads past an Exception.
i Invariants and contracts preserved? Once the client has a terminal packet, the query is no longer registered. The pipeline is already cancelled and joined in captureCurrentException before the packet is delivered, so no pipeline thread touches the reset state; members are destroyed in the same order as on the finishQuery path.

Session id: cron:clickhouse-impl-slot-4:20261027-040000

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @azat @yakov-olkhovskiy, could you review this and add the can be tested label so CI runs? After a query fails over the web terminal or SSH, LocalConnection kept its state, and so its system.processes entry, until the next query; receivePacket now resets it when it hands out the Exception packet, as finishQuery does for EndOfStream (the sendCancel guard is the same hunk as #127364).

@clickhouse-gh

clickhouse-gh Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [99681c0]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

LocalConnection now releases failed query state when delivering an Exception packet, removing idle failed queries from system.processes and freeing their concurrency slots. The accompanying sendCancel guard handles cleanup after that release. No actionable findings in commit 99681c0dd978b2e00777972333c706b8f29934f3; the focused regression test passed in Fast test.

Missing context / blind spots
  • ⚠️ Full stateless/sanitizer and bugfix-validation jobs were dropped, and Finish Workflow reports missing bugfix-validation results. A CI rerun would close this coverage gap. Before/after reproduction evidence is provided in the author's validation report; no local build or runtime tests were run during this review.
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 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 - 99681c0

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

@azat azat self-assigned this Nov 9, 2026
@azat
azat added this pull request to the merge queue Nov 9, 2026
Merged via the queue into ClickHouse:master with commit d457836 Nov 10, 2026
523 of 529 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 Nov 10, 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-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