Repository navigation
Reland #111979 with a cross-platform server liveness check - #112906
alexey-milovidov wants to merge 16 commits into
Conversation
#111979 made `stop_server` signal the server itself rather than the `sh -c` wrapper above it, so that a wedged server gets `SIGTRAP`/`SIGKILL` and releases its lock on `<run_path>/status` before the `system.*_log` scraping runs. It identified the server by reading `argv[0]` from `/proc/<pid>/cmdline`. macOS has no `/proc`, so on the `Fast test (arm_darwin)` runners the read always raised and `_server_process_alive` always answered "already gone": the server was never signalled and was left orphaned on every run, holding the write end of praktika's `TeePopen` stdout pipe. `TeePopen.wait` never saw EOF, so `praktika run` hung after an otherwise-green run, and since those runners are `--ephemeral` each hung job consumed a Mac host for good - the pool drained from 10 usable to 2 in about four hours and #111979 was reverted by #112812. Process identity is now read through `/proc` where it exists and through `ps` where it does not, keeping the Linux path byte for byte what it was. `ps` runs with `-ww` so it does not truncate the command line to the terminal width, which was the reason `/proc` was preferred in the first place. When neither source can answer, an existing pid is reported alive rather than gone: a server wrongly believed gone is never stopped, which is what burns a runner, while signalling a recycled pid needs that pid to be both freed and handed out again inside the teardown window. `ClickHouseProc.HAS_PROC` is probed once at import instead of branching on `platform.system()`, so the Darwin path is reachable from tests on Linux CI. The restored regression test adds seven cases that drive it, including the incident end to end; all four that depend on the fix fail against the original code with the same `already gone - TRAP not sent` line the wedged hosts logged. Caused by: #111979 Related: #112812
|
Workflow [PR], commit [d93c027] Summary: ✅ AI ReviewSummaryThis PR relands the Missing context / blind spots
Findings
Final Verdict |
… lives `_stop_respawned_server` read the pid file once and returned as soon as it named no live server. That is not proof that nothing is coming back: in `CLICKHOUSE_WATCHDOG_RESTART=1` mode the replacement server publishes its pid only once it is up, and `start` itself budgets 15 seconds for that - well past the wait for the `sh -c` wrapper above. A respawn slower than the teardown therefore came up after `stop_server` returned and held `<run_path>/status` while `clickhouse local` scraped `system.*_log`, which is the failure this teardown exists to prevent. `_stop_one_server` now snapshots the watchdogs above the server before signalling anything - once the server is gone they can no longer be found by walking up from the pid file - and `_stop_respawned_server` treats an empty pid file as final only when every one of them has exited. While one is alive the decision is deferred for `RESPAWN_GRACE_TIMEOUT`, and a watchdog that outlives even that is killed, because from that point nothing else would stop it from publishing another server. `_stop_server_watchdogs` becomes `_kill_watchdogs`, taking the pids to kill rather than deriving them from a server pid, since the grace-period path has no server pid to walk up from. The regression test drives the exact race: a `delayed` fake watchdog that publishes its replacement server 5 seconds after the first one dies, with the `sh -c` wrapper already reaped. Before this commit `stop_server` returned in 1.3 seconds with the watchdog still alive and free to bring a server back.
…-platform-liveness
`BaseDaemon::setupWatchdog` renames the watchdog by writing `clickhouse-watchdog` over its `argv[0]` in place - truncated to the length of the original name, so a server started as `clickhouse-server` leaves it named `clickhouse-watchd` - and restores the original name just before each respawn fork. The watchdog handling in `stop_server` filtered every candidate through the `clickhouse-server`-only identity check, so in `CLICKHOUSE_WATCHDOG_RESTART=1` mode `_server_watchdog_pids` returned nothing, the grace-period wait in `_stop_respawned_server` had no watchdog to wait for, and an empty pid file could still be taken as final while the real, renamed watchdog was free to publish a replacement server holding `<run_path>/status`. `_watchdog_process_alive` now accepts any truncation of `clickhouse-watchdog` no shorter than `clickhouse-watch`, plus the server's own name for the moments around the rename, and is used for watchdog discovery, the settling wait, and the kill path (`_signal_server` and `_wait_server_gone` take the identity check as a parameter). The fake server in the tests now models the rename with `execv` through differently named symlinks - same pid, new `argv[0]`, like the in-place `memcpy` - so the respawn tests exercise the real `clickhouse-watchd` shape; three focused discovery tests cover the truncated, untruncated, and not-yet-renamed watchdog names. The renamed-watchdog discovery test and both respawn tests fail on the previous code. #112906 Co-Authored-By: Claude Fable 5 <[email protected]>
When `stop_server` begins after one server exited but before the watchdog's replacement rewrote the pid file, both the pid file and the startup pid name a dead process, so the walk in `_stop_one_server` snapshotted no watchdogs and `_stop_respawned_server` took the empty pid file as final - while the still-live watchdog was free to publish a replacement server holding `<run_path>/status` moments later. Snapshot the watchdog chain in `start`, while the pid just read from the pid file is known to name a live server, and fall back to that snapshot at teardown when the walk from the current pid finds nothing. The watchdog keeps its own pid across the respawns it performs, so the startup snapshot still names the process that can bring a server back. Add the reviewer's reproducer as a regression test: kill the first `restart_mode="delayed"` fake server before calling `stop_server` and assert the watchdog does not outlive the teardown and no server comes back. It fails before this fix and passes after; 18/18 pass. Co-Authored-By: Claude Fable 5 <[email protected]>
…-platform-liveness
Two gaps in how `ProcessIdentityUnknown` was handled in watchdog discovery on hosts without `/proc`: - `_parent_pid` let it escape from `_ps_field`, so a transient `ps` failure made `start` report success without recording `self.pid_*` (the later `stop_server` skipped the replica), or aborted `_stop_one_server` before any signal was sent. It now reads as "parent unknown" (0), degrading to the startup snapshot / no-watchdog path. - `_watchdog_process_alive` inherited the fail-close "unknown means alive" of the server liveness check, which is wrong for ancestor discovery: with `ps -o command=` failing while `ps -o ppid=` works, `_server_watchdog_pids` promoted the `sh -c` wrapper and every live ancestor into SIGKILL targets for `_kill_watchdogs`. Discovery now passes `unknown_alive=False`, ending the walk at the first ancestor it cannot identify, while the signal/wait paths keep the fail-close default for pids already established as watchdogs. Both reproductions from the review are added as tests; they fail before the fix and pass after.
…-platform-liveness
…respawned_server` when the fresh walk fails On the `HAS_PROC = False` path a transient `ps` failure makes `_server_watchdog_pids` return no ancestors, so the came-back-server branch of `_stop_respawned_server` killed only the current server while the watchdog stayed alive and respawned another one; after `RESPAWN_STOP_ATTEMPTS` the teardown returned with a live server still holding `<run_path>/status`. The branch now falls back to the `watchdogs` snapshot taken before the teardown, the same fallback `_stop_one_server` already uses; `_kill_watchdogs` re-checks each pid's identity before signalling. The reviewer's reproduction is added as `test_stop_respawned_server_survives_a_walk_failure_with_the_startup_snapshot`; it fails before the fix, and all 21 tests pass after it. Co-Authored-By: Claude Fable 5 <[email protected]>
…"no watchdog" On the `HAS_PROC = False` path a transient `ps` failure while `start` snapshotted the watchdog chain recorded `watchdogs_0 = ()`, conflating "the discovery failed" with "there is definitively no watchdog". A teardown that then began inside the watchdog restart gap - dead pid in the pid file, dead startup pid - had nothing to walk and nothing to fall back on, so `_stop_respawned_server` trusted the empty snapshot and returned in milliseconds, while the still-live watchdog republished a server that kept holding `<run_path>/status`. Now the walk reports whether its answer is definitive (`_server_watchdog_pids_checked`), `start` retries a failed walk while the pid it starts from is known to be alive and records a still-failing one as None (`_startup_watchdog_snapshot`), and `_stop_respawned_server` treats an empty-but-unknown watchdog set as "wait the whole respawn grace period on the pid file" instead of proof that nothing is coming back (`_wait_server_republished`). The reviewer reproduction is added as `test_stop_server_wins_the_restart_gap_even_when_startup_discovery_failed`; on the pre-fix code it fails exactly as reported (`stop_server` returns in ~34 ms and a respawned server survives holding the lock), and all 24 tests pass after the fix. Co-Authored-By: Claude Fable 5 <[email protected]>
…-platform-liveness
|
🕵 The sole red check, Stress test (amd_tsan), failed with |
|
Agreed it is unrelated to this PR, and the fix is already in progress: #113609. Same defect, not just the same area. The Root cause: the array-no-supertype branch of Two things worth adding:
#113640 does not reference #113609 in either direction at the moment; I will add the cross-reference to #113609's description. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units6 translation units recompiled, 3 s compile time in total, 6 of them have a recent master baseline. |
…roof of no watchdog The first server can die between writing its pid file and `start` taking the watchdog snapshot, with its watchdog already counting down to the respawn. `_startup_watchdog_snapshot` then walked a corpse: a dead pid has no readable parent, so `_server_watchdog_pids_checked` returned an empty chain that looked definitive, and a teardown beginning inside the restart gap trusted the `()` snapshot, believed the empty pid file immediately, and returned before the still-live watchdog republished a locked server. A completed walk now counts only while the walked pid is still a live server (`_server_process_alive` with `unknown_alive=None`, so an unreadable identity retries like a failed walk); a walk over a dead pid is recorded as None (unknown), which makes the teardown wait out the full respawn grace period before believing an empty pid file. The reviewer's repro is added as `test_startup_snapshot_over_a_dead_server_is_not_proof_of_no_watchdog`: it fails pre-fix with the snapshot recorded as `()`, and all 25 tests pass post-fix. Co-Authored-By: Claude Fable 5 <[email protected]>
…-platform-liveness
|
🕵 The sole red check on |
… for a lost startup snapshot `watchdogs_definitive` was derived from the startup snapshot alone, so one transient `ps` outage at `start` (snapshot recorded as unknown) kept every later teardown of that replica in `_wait_server_republished` for the whole `RESPAWN_GRACE_TIMEOUT`, even when the walk from the live teardown pid had just completed and found the real watchdog chain. The definitive bit now follows the teardown-time `_server_watchdog_pids_checked` walk - confirmed against the walked pid still being a live server, the same corpse guard `_startup_watchdog_snapshot` uses - and inherits the startup snapshot's state only as the fallback. Regression test: `test_teardown_walk_recovers_from_a_failed_startup_discovery` fails on the previous code with `stop_server took 31.0s` and passes now; all 26 tests in the suite pass.
…-platform-liveness
|
🕵 The sole red check on |
|
Agreed it is unrelated to this PR. The defect is on pristine It is my own regression, from merged #113225 ( Five lines, every table setting default, aborts on a pristine- CREATE TABLE t (id UInt8, `arr.n` Array(String)) ENGINE = MergeTree ORDER BY id;
INSERT INTO t VALUES (1, ['a','b']);
SYSTEM STOP MERGES t;
ALTER TABLE t MODIFY COLUMN `arr.n` Array(Nullable(String)) SETTINGS alter_sync = 0;
SELECT `arr.n`.null FROM t; -- expected [0,0]Root cause: Three things that may help when you next see this signature:
For reference, I have already answered this same signature on carriers 113595, 113596, 109433, 110886 and 110127; this is the sixth report of it in about a day, which is consistent with the breadth you measured. |
|
🕵 The sole CI failure on |
…-platform-liveness
|
🕵 Merged No code changes in this push. Awaiting fresh CI. |
| try: | ||
| with open(f"/proc/{pid}/cmdline", "rb") as cmdline: | ||
| return os.fsdecode(cmdline.read().split(b"\0", 1)[0]) | ||
| except OSError: |
There was a problem hiding this comment.
_process_argv0 still collapses every /proc/<pid>/cmdline OSError to None, so the new fail-closed "cannot identify the process" behavior only exists on the ps path. If this read fails transiently on a live Linux server (for example EMFILE or an I/O error), _server_process_alive returns False, _signal_server prints already gone, and the server is left holding <run_path>/status; _parent_pid has the same collapse-to-0 behavior for /proc/<pid>/status, so the restart-gap watchdog snapshot can also become a definitive (). Can we distinguish "the process went away" from other /proc errors and raise ProcessIdentityUnknown for the latter, so the Linux path honors the same alive/unknown contract described in the PR body?
|
It's complex. I don't like the result. |
Related: #111979
Related: #112812
Related: https://github.com/ClickHouse/clickhouse-private/issues/66906
Relands #111979, which was reverted by #112812.
#111979 made
stop_serversignal the server itself rather than thesh -cwrapper above it, so a wedged server getsSIGTRAP/SIGKILLand releases its lock on<run_path>/statusbefore thesystem.*_logscraping runs. It identified the server by readingargv[0]from/proc/<pid>/cmdline.macOS has no
/proc, so on theFast test (arm_darwin)runners that read always raised and_server_process_alivealways answered "already gone". The server was never signalled and was left orphaned on every run, holding the write end of praktika'sTeePopenstdout pipe;TeePopen.waitnever saw EOF, sopraktika runhung after an otherwise-green run. Those runners are--ephemeral, so each hung job consumed a Mac host permanently and the pool drained from 10 usable to 2 in about four hours.What is different this time
/procwhere it exists and throughpswhere it does not. The Linux path is byte for byte what it was.psruns with-ww, so it does not truncate the command line to the terminal width - that truncation was the reason/procwas preferred in the first place, and there is a test for it._parent_pid(the watchdog walk) got the same treatment.ClickHouseProc.HAS_PROCis probed once at import rather than branching onplatform.system(), so the Darwin path is reachable from tests running on Linux CI.Testing
The regression test from #111979 is restored and extended from 6 to 13 cases; the 7 new ones set
HAS_PROC = Falseto drive the macOS path on Linux CI, including the incident end to end (test_stop_server_stops_the_server_without_proc).Verified locally that they catch the original defect: with
_process_argv0reverted to the/proc-only behaviour, 4 of the 7 fail, and the captured output reproduces the exact pair of lines every wedged host logged:All 13 pass with the fix (
python3 -m pytest ci/tests/test_stop_server_signals_server_not_wrapper.py).Follow-up not included here
Contributing factor 4 of the postmortem -
TeePopen.wait()blocking forever on a pipe whose write end a grandchild inherited - is untouched. Correct teardown avoids it, but any future process leak reintroduces the same wedge, so hardeningTeePopenis worth a separate PR.Changelog category (leave one):