Skip to content

Retry clickhouse-server boot in functional-test setup - #109115

Merged
alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:ci-bugfix-validation-server-boot-retry
Sep 26, 2026
Merged

alexey-milovidov merged 6 commits into
ClickHouse:masterfrom
groeneai:ci-bugfix-validation-server-boot-retry

Conversation

@groeneai

@groeneai groeneai commented Jul 2, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Retry the initial clickhouse-server boot in the functional-test setup so a single transient boot death does not fail the whole job.

Description

Problem. In functional-test setup, start spawns the server and wait_ready polls select 1. A transient host condition at boot, such as a brief memory spike from a co-tenant job on the same runner, can make the server exit a few seconds in, before it creates clickhouse-server.err.log, so no diagnostics survive. A plain re-start then succeeds. It surfaces as an opaque Start ClickHouse Server setup FAIL, reddening the non-blocking Bugfix validation (functional tests) job and, through the rollup, Finish Workflow and Mergeable Check.

Evidence it is transient. In one 40-minute window the identical failure hit four unrelated PRs. On the commit that failed this step every other server-booting check passed, including Fast test, all Integration batches, AST fuzzer, BuzzHouse and Stress, and a sibling PR re-ran the same SHA and passed. Failing run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109052&sha=f03fe3af507daaf0a255f0cdc55867bc05662e59&name_0=PR&name_1=Bugfix%20validation%20%28functional%20tests%2C%20amd64%29

Fix. Retry the setup boot up to 3 times, but only a boot the server did not survive. wait_ready also fails by readiness timeout while the process is still up, and retrying that would let the next attempt's Utils.clean_dir wipe the run directory a live server is using, so the loop stops while any tracked replica is alive and fails with wait_ready's own diagnostics. A persistent boot failure still fails after the last attempt with the err-log tail preserved. The first-try-success path is unchanged, and the per-build-type binary-swap boot stays fail-closed.

clickhouse_proc.py carries the two stop_server fixes the retry depends on: it now kills a server whose pid file never appeared within start's poll, previously a silent no-op that left it running, and start clears the cached pid it had only ever assigned, so that path is reachable on a retry instead of shadowed by the previous attempt's pid.


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

Version info

  • Merged into: 26.10.1.781-master (included in 26.10 and later)

A transient host condition at boot (e.g. a brief memory spike from a
co-tenant job on the same runner) can make clickhouse-server exit a few
seconds after start, before it even creates its err.log, with no
diagnostics. A plain re-start then succeeds. This showed up as an opaque
"Start ClickHouse Server" setup FAIL across several unrelated PRs in the
same short window, reddening the non-blocking Bugfix validation job (and
thus Finish Workflow / Mergeable Check) even though the server binary was
healthy - proven by every other server-booting check passing on the same
commit, and by a same-SHA rerun of a sibling PR passing.

Retry the initial setup boot up to 3 times: on a failed start/wait_ready,
stop the server, clean its logs, wait briefly, and try again. A single
transient boot death no longer fails the whole job; a persistent boot
failure still fails after the last attempt with wait_ready()'s err-log
tail preserved for triage, so a real boot regression is not masked. The
first-try-success path is unchanged, and the per-build-type binary-swap
boot stays fail-closed.

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

groeneai commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Partial. The trigger is a transient host condition at boot (server exits ~115 a few seconds after start, before err.log is created); it cannot be forced on demand — that transience is exactly what this change handles. What IS deterministic: the failing setup step is fully reproduced from the CI report (server started pid 669 → exited: 115 → no err.log → wait_ready FAIL), and the retry control flow is validated deterministically (see e).
b Root cause explained? Yes. functional_tests.py setup calls CH.start() (returns once the pid file appears) then CH.wait_ready(). A transient boot death makes wait_ready see proc.poll()==115 before err.log exists, so the step FAILs with no diagnostics. There is no boot retry, so one transient death fails the whole (non-blocking) job and reddens Finish Workflow / Mergeable. A plain re-boot recovers — proven by a sibling PR's same-SHA rerun passing, and by every other server-booting check passing on the same commit.
c Fix matches root cause? Yes. It retries the initial boot (stop_server → clean_logs → sleep → re-start) up to 3 times, directly turning a proven-transient single boot death into a recovery. It is not a band-aid: it does not widen/relax any assertion or disable anything on the success path.
d Test intent preserved / new tests added? Preserved and strengthened. A persistent boot failure still fails after 3 attempts with wait_ready()'s err-log tail preserved, so a real boot regression is NOT masked. The distinct sub-cause markers (process did not start vs not ready (wait_ready)) are kept. No functional test is weakened; this is the CI harness itself.
e Both directions demonstrated? Yes, at the control-flow level (a real transient exit-115 cannot be summoned on demand). A standalone simulation of the exact loop confirms: (1) first-try success → no stop/clean/sleep, no extra output (normal path byte-for-byte unchanged); (2) start-OK-then-wait_ready-False once, then success → recovers with one restart, warning only, no FAILURE; (3) 3 persistent failures → fails closed with the SETUP FAILURE marker preserved; (4) process-never-starts → correct sub-cause marker, wait_ready skipped.
f Fix is general across code paths? Yes. The retry lives in the single shared functional-test setup path, so it benefits all functional-test and bugfix-validation jobs. The sibling boot site — the per-build-type binary-swap boot in bugfix validation — is intentionally left fail-closed (a dead server there must not be flipped into a "reproduction"); documented in the code comment.
g Fix generalizes across inputs? N/A — CI-harness resilience change, not a data/type code path.
h Backward compatible? N/A — CI-only Python; no settings, formats, or user-visible behavior change. Success path unchanged.
i Invariants and contracts preserved? Yes. Uses existing stop_server() (graceful clickhouse stop --pid-path, safe on an already-dead process; leaves MinIO/azurite/kafka up) and clean_logs() exactly as the bugfix-validation binary-swap path already does between iterations. No lifetime/ownership/concurrency contract is touched.

Session id: cron:clickhouse-worker-slot-0:20260702-041500

@groeneai

groeneai commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

cc @maxknv @leshikus — could you review this? It retries the initial clickhouse-server boot (up to 3x: stop_server → clean_logs → sleep → restart) in the functional-test setup, so a transient boot death (server exits ~115 before writing err.log, recovered by a plain restart) no longer FAILs the whole non-blocking Bugfix validation job and reddens Finish Workflow / Mergeable. A persistent boot failure still fails closed after the last attempt with the err-log tail preserved; the first-try-success path is unchanged.

@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [94b622c]

Summary: ✅


AI Review

Summary

This PR retries the initial functional-test clickhouse-server boot and hardens ClickHouseProc around pid-less starts. The retry loop itself is directionally correct, but stop_server still does not reliably stop the real server once a pid was recorded, so the PR has not fully restored the "safe restart on a clean process state" invariant it relies on.

Findings

❌ Blockers

  • [ci/jobs/scripts/clickhouse_proc.py:790-820] stop_server still signals the Popen wrapper instead of the real clickhouse-server process on the recorded-pid path. start launches the server with subprocess.Popen(..., shell=True), so proc is /bin/sh -c ...; a minimal shell=True trace leaves the child alive after both proc.terminate() and the SIGTRAP -> proc.kill() fallback. That means stop_server can return while the server is still running after a failed graceful stop, which reopens the dirty-state restart this PR is trying to prevent in bugfix validation, and it also leaks the server for terminate(force=True) callers such as ci/jobs/fast_test.py:413. Suggested fix: when the graceful stop fails, and in the force=True branch, target the real server (pid) or reuse the same pkill -f -- '--pid-file {pid_file}' cleanup already added for the pid-less branch before returning.
Final Verdict

Changes required. Do not merge until stop_server reliably kills the actual server process on the recorded-pid path as well.

LLVM Coverage Report

⚠️ No coverage measurement for commit 94b622c: incomplete coverage measurement: 16 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_ft_2.profdata, LLVM_COVERAGE_FILE_ft_3.profdata, LLVM_COVERAGE_FILE_ft_old_s3_db_repl_wasm_parallel_2.profdata, LLVM_COVERAGE_FILE_ft_old_s3_db_repl_wasm_parallel_3.profdata, LLVM_COVERAGE_FILE_ft_old_s3_db_repl_wasm_sequential_1.profdata, LLVM_COVERAGE_FILE_ft_old_s3_db_repl_wasm_sequential_2.profdata, LLVM_COVERAGE_FILE_ft_s3_async_sequential.profdata, LLVM_COVERAGE_FILE_ft_s3_sequential.profdata, LLVM_COVERAGE_FILE_it_1.profdata, LLVM_COVERAGE_FILE_it_2.profdata, LLVM_COVERAGE_FILE_it_3.profdata, LLVM_COVERAGE_FILE_it_4.profdata, LLVM_COVERAGE_FILE_it_5.profdata, LLVM_COVERAGE_FILE_it_6.profdata, LLVM_COVERAGE_FILE_it_7.profdata, LLVM_COVERAGE_FILE_it_8.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 2, 2026
Comment thread ci/jobs/functional_tests.py Resolved
ClickHouseProc.start() returns False after a 15s timeout waiting for the
pid file, but on that path it still spawned the server (self.proc is set)
and never recorded self.pid_0/1/2. stop_server() only acts when both proc
and the recorded pid are truthy, so for the slow / late-pid-file boot it
was a no-op and left the spawned server running.

The setup boot-retry loop calls stop_server() before restarting, so on a
slow start the previous server was left alive while the next attempt
unlinked the pid file, wiped run_path, and spawned a second server on the
same directories.

Handle the proc-but-no-pid case: kill any live server for that replica by
its unique --pid-file command-line token, then reap the process handle.
pkill -f is used because proc may be the `sh -c` wrapper rather than the
server itself (subprocess.Popen(..., shell=True) does not always exec), so
terminating proc alone can orphan the server; matching the token reaches
the wrapper, the server, and any leaked duplicate. The normal running-server
path (proc and pid both set) is unchanged.

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

groeneai commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator Author

Updated pre-PR validation gate for HEAD 8e01c41 (adds the stop_server hardening from the bot review).

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? The original failure (exit-115 boot death) is a proven-transient host condition, not on-demand reproducible; the review-found bug is deterministic: start() returning False after the 15s pid-file timeout sets self.proc but never self.pid_0/1/2, so stop_server's if proc and pid: guard is a no-op and the retry wipes run_path and spawns a second server. Reproduced the control flow and the dash shell-wrapper behavior in standalone simulations (.scratch/validate_stop_server.py, validate_boot_retry.py).
b Root cause explained? ClickHouseProc.start() records the pid only after the pid file appears; on the slow/late-pid-file path it returns False with self.proc set and pid still 0. stop_server guarded on both proc AND pid, so it skipped that case, leaving a live server. The retry loop then unlinked the pid file and cleaned run_path while the first server was still coming up, then spawned a second server on the same dirs.
c Fix matches root cause? Yes. Added an elif proc: branch in stop_server that kills a live server recorded without a pid, closing exactly the no-op gap. Chose option (B) over (A) because it fixes the primitive for all callers, not just the retry loop.
d Test intent preserved / new tests added? This is a CI-harness (Python) change with no functional-test target. Behavior validated by two standalone control-flow simulations (4/4 + 5/5 PASS), including a regression check that the OLD code leaked the slow-start server (PASS 2b). Normal running-server path proven unchanged.
e Both directions demonstrated? Simulations show: OLD stop_server classification leaks the proc-but-no-pid server (PASS 2b); NEW classification kills it (PASS 2). Live pkill -f -- '--pid-file <path>' verified against realistic sh -c-wrapped processes: kills wrapper+child, does not cross-kill other replicas, non-fatal (rc=1) on no match.
f Fix is general across code paths? Yes. Hardening stop_server (not just the retry loop) covers every caller that stops a server after a failed start. All three replica slots use the same loop body, so replicas 1/2 get the same protection.
g Fix generalizes across inputs? N/A (CI-harness process-management fix, not a data/type code path). The per-replica --pid-file token is distinct per replica (/etc/clickhouse-server, .../clickhouse-server1, .../clickhouse-server2), verified no substring collision.
h Backward compatible? Yes. No settings, formats, or user-visible behavior. Normal running-server stop path (proc and pid both set) is byte-for-byte unchanged; the new branch only fires for the previously-unhandled proc-but-no-pid case.
i Invariants and contracts preserved? Yes. stop_server's contract (stop only server processes, leave Kafka/MinIO/etc. up) is preserved: the pkill pattern matches only the clickhouse-server command line for that replica. proc is still reaped (wait/kill) so no zombie remains. On dash the shell wrapper is not exec'd, so killing by the cmdline token (not proc.terminate()) is required to avoid orphaning the server.

Session id: cron:clickhouse-worker-slot-5:20260702-074400

@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Jul 2, 2026
@groeneai groeneai added the groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect label Aug 19, 2026
Comment thread ci/jobs/scripts/clickhouse_proc.py Resolved
groeneai and others added 4 commits August 27, 2026 21:23
stop_server() decides between its graceful path and the pid-less kill path
from self.pid_0/1/2, but start() only ever assigned those fields, never
reset them. It unlinks the pid file at the top of every spawn, so after one
successful boot the cached pid outlived the file it mirrors: on a later slow
start, where the pid file does not appear inside the 15s poll, self.proc
pointed at the new `sh -c` wrapper while self.pid_0 still held the previous
server's pid. stop_server() then took the graceful branch on a pid that is
not this server's and the pid-less kill path added in 8e01c41 never ran, so
the slow-starting server was left alive - exactly the leak that path exists
to prevent, and the state the boot-retry loop then restarts on top of.

Measured against the real start()/stop_server() with `clickhouse stop` from
a release binary, driving the claim's premise (pid file late, server a child
of the wrapper): before, pid_0 carried over, the graceful branch ran,
`clickhouse stop --pid-path` exited 107 (FILE_DOESNT_EXIST, so a missing pid
file is a hard failure rather than a silent success), the escalation sent
SIGTRAP to the `sh` wrapper, and the server survived stop_server(); after,
pid_0 was 0, the pid-less kill path ran, and the server was gone. Both arms
had the same live wrapper/server tree before the stop.

Reset the cached pid next to the pid-file unlink, so the two are cleared
together, and route both writes through one _set_pid() helper instead of
repeating the replica dispatch.

Also trim the comments this PR had accumulated over its review rounds down
to the local invariants: why the pid-less path matches on the --pid-file
token rather than signalling proc, and that only the setup boot is retried.
The transient-host-condition background and the cross-PR evidence live in
the commit messages, not in the source.

Co-Authored-By: Claude Opus 5 <[email protected]>
`.claude/CLAUDE.md` asks for literal class, function and option names inside
inline code blocks, and for a function to be named `f` rather than `f()`.

Co-Authored-By: Claude Opus 5 <[email protected]>
`wait_ready` fails in two different states and only one of them was ever shown
to be transient. It returns False through its `proc.poll()` branch when the
server exited, which is the boot death this loop exists for, and through its
readiness timeout while the process is still up. Retrying the second state is
unsafe: the next attempt's `start` calls `Utils.clean_dir` on `run_path`, so it
would wipe the directory a live server is using. `stop_server` does not save it
either, because when `clickhouse stop --do-not-kill` reports the server is still
running (`Install.cpp`, `stop`, returns 1 in that case) the escalation signals
`proc`, which is the `sh -c` wrapper rather than the server.

Break out of the loop while the process is still alive, so that state fails with
`wait_ready`'s own diagnostics exactly as it did before this loop existed.

Measured with the real `start` and `wait_ready`: a server that exits 115 takes
the `proc.poll()` branch and leaves `poll` at 115, so it is still retried; a
server that stays up and never answers `select 1` takes the timeout branch after
63s and leaves `poll` at None, so it is not.

Co-Authored-By: Claude Opus 5 <[email protected]>
With `DatabaseReplicated`, `start` also spawns replicas 1 and 2, and
`wait_ready` returns as soon as replica 0 fails, before it looks at the other
two. Gating on replica 0 alone therefore still allowed a retry while a sibling
replica was up, and `start` clears each replica's run directory in turn.

Gate on every tracked replica instead. Nothing changes when the job is not
replicated, where `proc_1` and `proc_2` are None.

Measured over real `Popen` handles: (dead, None, None) and (dead, dead, dead)
retry, while (dead, alive, None), (dead, None, alive) and (alive, None, None)
do not.

Co-Authored-By: Claude Opus 5 <[email protected]>
elif proc:
# `proc` is the `sh -c` wrapper, not the server, so kill by the
# unique --pid-file token, then reap the wrapper.
Shell.check(f"pkill -9 -f -- '--pid-file {pid_file}'", verbose=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new elif proc branch fixes the pid-less case, but the same wrapper problem still exists a few lines above once a pid was recorded. This function now documents that proc is only the sh -c wrapper; a minimal sh -c "sleep 1000" trace leaves the child alive after kill -TRAP $wrapper, so if clickhouse stop --pid-path ... falls through, proc.send_signal(signal.SIGTRAP) / proc.kill() can return with the real clickhouse-server still running. The retry and bugfix-validation restart then continue on dirty process state, and terminate(force=True) callers still leak the server for the same reason.

Please escalate against the real server process (pid) or reuse the same pkill -f -- '--pid-file ...' cleanup after the graceful stop fails, instead of signalling the wrapper.

@clickhouse-gh clickhouse-gh Bot added the comp-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). label Sep 4, 2026
@alexey-milovidov alexey-milovidov self-assigned this Sep 26, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 26, 2026
Merged via the queue into ClickHouse:master with commit f77b081 Sep 26, 2026
179 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 26, 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-ci-infrastructure CI/CD pipelines (GitHub Actions, CI scripts, runners). groeneai-origin-ci-own-pr PR origin: CI on a groeneai PR exposed a separate defect 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.

3 participants