Skip to content

Let the hung check report what it saw instead of retrying past its deadline - #120303

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:hung-check-query-budget
Sep 19, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:hung-check-query-budget

Conversation

@groeneai

@groeneai groeneai commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #115706

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Description

Requested by @ alexey-milovidov on #115706: the harness half of that incident, kept out of #120216 which fixes the server-side deadlock. The defect is in the harness here, so that is where the fix goes: a hung check that cannot say what it saw.

Stress test (arm_release) reported Test script failed with hung_check.log holding only progress dots. The dump query system.processes JOIN system.stack_trace never set max_http_retries, so it inherited the default 5; with its 120 s socket timeout and the loop's min(i + 1, 3) sleeps its worst case is 5 * 120 + 9 = 609 s, against the 600 s hung_check.wait in stress.py, which then SIGKILLs the process group. The parent killed the child during attempt five, so the except branch that prints what the check saw never ran, and neither did the code that writes the Hung check failed, possible deadlock found row with a tail of the log.

max_http_retries is a per-caller knob, added by c1c278a for this purpose. This caller runs under a deadline it can exceed, so it takes one attempt and fails at 120 s with the rest of the budget left to report with. All three branches of the collector get it, so the SharedCatalog and replicated-database legs are covered, and the stateless and fast-test callers are fixed too, since the change is in the collector rather than in a per-caller budget.

When the dump fails, the stacks are collected anyway: the plain system.stack_trace read over TCP, which still works when only the expensive aggregate could not finish, and the C-level stacks, which need no answer from the server at all. Every other hung-or-dead-server path in the runner already takes that pair. That also absorbs this change's one cost, a transient HTTP error now being reported rather than retried.

Measured, including the failure end to end

Against a socket that accepts and never answers, the collector takes 609.3 s over 5 attempts with gaps of 121.1 / 122.0 / 123.1 / 123.1 s, and 120.0 s over one attempt with this change. Those gaps match the server log of the reported run (dump attempted at 09:39:27 / 09:41:28 / 09:43:30 / 09:45:33 / 09:47:36), so the 609-vs-600 arithmetic is confirmed independently of it. Forcing the new constant back to 5 returns the collector to 609.4 s, and each of the three branches is measured separately: 1 attempt with the change, 5 without.

End to end, with system.stack_trace wedged by the defect #120216 fixes and the hung check run under a parent mirroring stress.py (own session, tee, wait(timeout=600), killpg on expiry):

parent hung_check.log
before TimeoutExpired at 600.0 s, group killed 40,246 progress dots, nothing else
after child exits 1 at 254.6 s the failure line, then both stack collections

rc = 1 is what fires stress.py's res != 0 and have_long_running_queries gate, so the row is written with a tail that now holds the failure line. That line is also flushed before the collectors run, since the child's stdout is a block-buffered pipe: had the deadline expired during them, an unflushed report would have died with the SIGKILL.

A batch of stateless tests with --hung-check still exits 0, prints No queries hung., reaches the log-message statistics report, and does not collect stacks: the reporting arm and the success arm are exclusive.

There is no in-repo test: the runner assertion that pinned one attempt and the 120 s timeout on the three collector branches was removed on review. Coverage is out of tree, in the runs above, since the hung-check block is inline in main().


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

Version info

  • Merged into: 26.10.1.168 (included in 26.10 and later)

groeneai and others added 3 commits September 15, 2026 18:50
…adline

The stacktrace dump never set max_http_retries, so it inherited the default 5.
With its 120s socket timeout and the loop's min(i + 1, 3) sleeps its worst case
is 5 * 120 + (1 + 2 + 3 + 3) = 609s, while the caller in stress.py waits 600s and
then SIGKILLs the whole process group. The parent therefore killed the child in
the middle of attempt five, so the except branch that prints "Failed to get
processlist with stacktraces" never ran, and neither did the code that writes the
"Hung check failed, possible deadlock found" row with a tail of the log. The job
reported a bare "Test script failed" and hung_check.log held only progress dots.

max_http_retries exists so each caller can pick its own policy: c1c278a
introduced it for that, replacing a global constant. This caller runs under a
deadline it can exceed, so it takes one attempt and fails at 120s with the rest
of the budget left to report with.

When the dump cannot be obtained, collect the C-level stacks as well. That is the
dump which does not need the server to answer SQL, which is precisely the state
this branch reports, and every other hung-or-dead-server path in the runner
already takes it. It also covers the one cost of the retry change, since a
transient HTTP error is now reported rather than retried.

Both branches of get_processlist_with_stacktraces for clustered configurations
get the same policy, so the SharedCatalog and replicated-database legs are not
left behind. The stateless and fast-test callers of the collector are fixed too,
since the change is in the collector rather than in a per-caller budget.

Measured against a blackhole server: 609.3s over five attempts with gaps of
121.1/122.0/123.1/123.1s, matching the gaps in the server log of the reported
run, and 120.0s over one attempt with this change. End to end under a parent
mirroring stress.py, with system.stack_trace wedged: before, the parent's wait
expired at 600s and the log held nothing but 40246 dots; after, the child exits 1
at 214s and the log carries the failure line and the stacktrace collection.

Co-Authored-By: Claude Opus 5 <[email protected]>
The failure arm took only the C-level stacks. What the arm establishes is that the
expensive aggregate over system.processes JOIN system.stack_trace did not return
over HTTP; it does not establish that the server cannot answer SQL. The only
firing of this arm in 60 days of CI (PR 112157, Stress test (arm_tsan),
2026-07-28) was a server-side Code: 159 TIMEOUT_EXCEEDED, elapsed 96139 ms
against a 60000 ms max_execution_time, so the server was answering and enforcing
its limits and only the JOIN plus symbolization could not finish. That is exactly
where the plain system.stack_trace read still works: it goes over native TCP,
bounded at 30s, and it is also the only dump that maps a stuck thread to its
query_id. The C stacks need no answer from the server at all. Take both, SQL
first, which matches the six other hung-or-dead-server dump sites in this file;
all three job scripts already collect the pair.

Both calls are bounded and neither propagates, so the arm still reaches
exit_code.value = 1. With system.stack_trace wedged, the reporting path now ends
at 247.8s of the parent's 600s budget, 33.6s more than with the C dump alone, and
carries the failure line, the SQL collector's "Collected no stacks from
system.stack_trace" and the C collection.

Add a stateless test for the retry budget. It loads tests/clickhouse-test as a
module, records what get_processlist_with_stacktraces passes to the executor, and
pins one attempt and the 120s socket timeout on all three of its branches. The
cluster name is printed alongside them so that three lines produced by one branch
cannot pass, and the timeout is pinned as well as the attempt count because the
bound this establishes is their product: raising the socket timeout later has to
redden the test too. It reports the budget that takes effect rather than the
argument that sets it, so a branch which stops passing the argument and inherits
clickhouse_execute's default is caught by value. Against the runner on master all
three lines report five attempts, so the test fails there and passes here.

Co-Authored-By: Claude Opus 5 <[email protected]>
The report goes into a block-buffered pipe. stress.py runs the hung check
with `stdout=tee.stdin`, the stress image does not set PYTHONUNBUFFERED
and the job's own `docker run` does not pass it, and the runner is
reached through its shebang, so CPython buffers 8 KiB of its output.
After the failure line is printed the arm spends up to ~150 s in bounded
collection (30 s reading system.stack_trace, 120 s in lldb) while the
caller kills the process group on its own 600 s deadline. A kill inside
that window discards the buffer, and the log again holds nothing but
progress dots, which is the symptom this change exists to remove.

Flushing before the dumps makes the report durable under that kill
instead of relying on CPython's flush at process exit, which covers only
a normal exit. Measured with two copies of the runner differing in
exactly this line, both killed at 230 s while collecting: without it the
log carries no `Hung check failed` line at all, with it the line reached
the log 19.5 s before the kill. The normal path is unchanged, exit 1
after 254.6 s with the failure line and both collections present.
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 15, 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), plus my own cold read of the
resulting code on each pass: 16 findings over 1 full pass and 2 fix rounds, each re-reviewed against
the previous head. 5 changed the code or the description, 10 were refused with recorded evidence,
1 was withdrawn.

# Sev Finding Verdict Evidence / action
1 ⚠️ No executed test covers the new retry limit, so CI stays green if it is reverted AGREE, fixed Added 05218_clickhouse_test_hung_check_stacktrace_retries.sh, in the shape of the existing 05099_clickhouse_test_keeper_retry_matching.sh: it loads the runner as a module and pins the effective timeout and attempt count on all three collector branches. Red against the runner on master (attempts=5), and reverting either the constant or one branch's argument reddens exactly the affected lines.
2 ⚠️ The failure arm collected only the C stacks, while every other hung-or-dead-server path in the runner collects both AGREE, fixed The one run in 60 days whose report tail carries this arm's message failed with Code: 408 / Code: 159 TIMEOUT_EXCEEDED after 96.1 s, i.e. the server was answering SQL and only the expensive aggregate could not finish, which is exactly where the plain read over TCP works. Both artifacts are already collected by the job scripts.
3 ⚠️ The report goes into a block-buffered pipe, so a SIGKILL landing during the collections discards it and the log holds only dots again AGREE, fixed stress.py gives the child stdout=tee.stdin, the stress image does not set PYTHONUNBUFFERED and the job's own docker run does not pass it. Two copies of the runner differing in exactly the added sys.stdout.flush(), both killed at 230 s while collecting: without it the log carries no failure line at all, with it the line reached the log 19.5 s before the kill.
4 ⚠️ The in-diff comment claimed the branch reports a server that cannot answer SQL AGREE, fixed The pre-existing comment in the same arm says the expected cause is an expensive query timing out, and the observed firing agrees with it. Comment, commit message and description restated.
5 ⚠️ The reporting arm itself should have in-repo coverage DISAGREE The hung-check block is inline in main() behind a full test run and is not callable in isolation; extracting it is larger than the fix. It is covered end to end out of tree, against a wedged system.stack_trace under a parent that mirrors stress.py.
6 ⚠️ The whole block can still exceed the 600 s wait, so the report may be unreachable anyway DISAGREE Re-derived the worst case independently: about 10 s of startup, at most 249 s of polling (90 s window plus one in-flight 159 s size query), 120 s for the dump after this change, at most 159 s for the re-check, so the print is reached by 538 s. What can overrun is the bounded collection after the print, which is what finding 3 makes survivable.
7 💡 Set the failure flags before collecting, as two sibling sites do DISAGREE Those sites say why: they race other workers through try_claim_stop_cause, which this single-threaded end-of-run arm has no analogue of. The collectors also cannot raise into the arm; every shell-out returns "" on failure, measured 3/3 with a negative control.
8 💡 Naming, the test asserting an effective rather than a literal budget, and three further comment nits DISAGREE (5), withdrawn (1) The constant mirrors max_http_retries, whose own default likewise counts attempts. The test reports the effective budget through the executor's signature defaults, which also catches a branch that silently drops the argument; the literal form raised KeyError and lost the other two lines.

Noted, not blocking: the collectors' own progress lines stay buffered after the flush, so a kill
inside lldb still loses them. Their substantial output does not depend on that, since both dumps are
appended to sql_stacktraces.log and c_stacktraces.log through a with open(...) that closes per
append, and the line that says what the check saw is the one now flushed.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal
per finding. Findings on hunks unchanged by a fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-11:20260915-231300

@clickhouse-gh clickhouse-gh Bot closed this Sep 15, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Sep 15, 2026
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Sep 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8eaf13a]

Summary: ✅


AI Review

Summary

This PR makes the end-of-run hung check fail fast on the expensive system.processes JOIN system.stack_trace dump, flushes the failure report before the bounded follow-up collectors, and gathers both the SQL-side and C-side stack dumps when that aggregate query fails. I did not find a remaining blocker or major issue in the current net change.

Final Verdict

✅ No findings.

LLVM Coverage Report

Measured on commit 8eaf13a.

Metric Baseline Current Δ
Lines 88.50% 88.50% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 80.70% 80.70% +0.00%

Changed lines: Uncovered code analysis did not run: No coverable C/C++ source files changed (contrib/ is excluded from coverage).

Newly covered: +131 lines in 46 files (-121 lines lost coverage) · Details

Full report

@PedroTadim

Copy link
Copy Markdown
Member

cc @alexey-milovidov

# and the attempt count on all three of its branches. The cluster name is
# printed as well so that three lines produced by one branch cannot pass.

CUR_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)

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.

@groeneai you can remove this test

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Removed in 8eaf13a (the test and its reference). It pinned the effective 120 s timeout and the single attempt on the three collector branches, so that invariant is now unpinned in CI; the change itself is unchanged and the PR is down to the 15 lines in tests/clickhouse-test.

The test module-loaded tests/clickhouse-test and pinned the effective
socket timeout and attempt count on the three
get_processlist_with_stacktraces branches. Removed at the reviewer's
request on ClickHouse#120303; the retry budget and the flush stay verified out of
tree by the runs recorded in the PR description.
@alexey-milovidov alexey-milovidov self-assigned this Sep 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 19, 2026
Merged via the queue into ClickHouse:master with commit a9b6c93 Sep 19, 2026
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 Sep 19, 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-testing Test content only (no production code). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai manual approve Manual approve required to run CI pr-ci 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