Repository navigation
Wait for the worker cleanup before force-killing the test workers - #115403
alexey-milovidov merged 3 commits into
Conversation
When the run is stopped abnormally (server death, hung check, global time limit, --max-failures), the parent SIGTERMs each worker and waited 10 seconds before SIGKILLing it. A worker's SIGTERM path reaches kill_process_group, which on a sanitizer build deliberately sleeps 60 seconds so the sanitizer can print its report. The parent therefore gave every worker 10 seconds to finish a cleanup that takes 60, and killed all of them inside that sleep. Two things are lost when that happens. The sanitizer report is truncated, which is what the 60 second grace exists to collect (1a76ce9). And p.kill() reaches only the worker, not the test's process group, whose PGID file has already been unlinked - so the test's bash and clickhouse-client survive. When the stress harness's random query killer then terminates that leaked client, the orphaned bash writes "Terminated" into the log file descriptor it inherited, after the parent has moved on. If that write lands while the stress job is packaging the logs, tar reports "file changed as we read it" and exits 1, and the job fails at the packaging step with no other diagnostic - skipping the hung check, which runs afterwards. In four such stress runs the archived log and the raw log differ by exactly 11 bytes: "Terminated\n". Derive the wait from the same values the worker's cleanup sleeps for, so the two cannot drift apart again, and keep the escalation from ddc6bcf: SIGTERM, bounded wait, SIGKILL for stragglers, bounded wait. The sanitizer grace is one deadline shared by all workers, because they clean up concurrently and giving each its own would multiply the stop time by the number of workers. The previous 10 seconds is kept as a floor under every individual wait: the workers have all been terminated by the time the wait loop starts, so a shared deadline on its own would let the first slow worker consume it and leave the rest with no wait at all - killing more workers mid-cleanup than the loop being replaced did. With the floor, the worst case is the grace plus 10 seconds per worker. The budget is a ceiling and not a sleep, so a worker that exits promptly is not delayed, and the deadline is read from a monotonic clock so that a clock adjustment during the stop cannot truncate a wait. The guarantee is bounded to the delays the cleanup sleeps for, and three things sit outside it. On a build with --capture-client-stacktrace, kill_process_group goes on to collect a stacktrace per client process after those sleeps; that step is unbounded in the number of clients, so it is deliberately not folded into the budget. A process in uninterruptible sleep outlives any budget. And under the spawn start method, selected when the check name contains aarch, a worker re-imports the module without the __main__ block, so it never installs the SIGTERM handler and runs no cleanup for a wait to outlast; this change neither causes nor worsens that, and none of the four runs above is such a job. A build with no sanitizer and no --capture-client-stacktrace keeps the previous 10 seconds per worker, at any worker count. With capture enabled the budget covers that delay too, which is what the functional test and Fast test lanes use. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings on the published tree;
Severity: ❌ blocker / The final pass over the published tree returned 0 findings. Independently of the review, I Session id: cron:clickhouse-review-slot-8:20260819-055800 |
|
Workflow [PR], commit [6c8b118] Summary: ✅ AI ReviewSummaryThis PR replaces the parent’s flat Findings
Final VerdictChanges requested: the sanitizer-only underwait is improved, but the worker-stop contract is still violated on |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260819-051400 |
|
cc @azat @leshikus, could you review this? The parent's abnormal-stop wait was a flat |
| for p in processes: | ||
| if p.is_alive(): | ||
| p.terminate() | ||
| deadline = monotonic() + worker_cleanup_grace_seconds() + WORKER_STOP_MARGIN_SECONDS |
There was a problem hiding this comment.
This deadline still stops at worker_cleanup_grace_seconds() + WORKER_STOP_MARGIN_SECONDS, but on every --capture-client-stacktrace lane the worker cleanup does more work before it ever reaches the os.killpg(..., SIGTERM/SIGKILL) that actually reaps the test group. In cleanup_child_processes this path calls kill_process_group(..., None), so client_printed_stacktrace can never short-circuit and the worker always iterates pgrep(..., command="clickhouse") and calls get_stacktraces_from_lldb for each child. Each attach allows up to 30 seconds, so a single slow client trace already exceeds the 10-second margin and the parent can still SIGKILL the worker before it kills the test PGID, recreating the orphaned-bash / tar: file changed as we read it path on the functional, fast, and stress jobs that always pass --capture-client-stacktrace.
Either include the client-stacktrace phase in the budget, or move the group-kill ahead of the potentially long lldb loop.
There was a problem hiding this comment.
The lldb phase is genuinely outside the budget, and the PR says so: "the stacktrace collection after them is not counted". It is deliberate. That loop is unbounded in the number of clients, and #114565 raises its per-PID ceiling to 120s, so folding it in would make the stop time scale with client count. Bounding or reordering it is a change to what the capture flag collects, which is a separate concern from this mismatch.
Two corrections to the premise. Stress jobs do not pass --capture-client-stacktrace on the worker path: grep -c capture-client-stacktrace tests/docker_scripts/stress_runner.sh is 0, and run_func_test does not pass it. The only stress use is stress.py:912, the separate single-test hung-check invocation, which is not a terminate_workers path. None of the four affected runs had capture enabled, so this is not the mechanism behind them.
On the functional and Fast test lanes, which do pass it, the budget already includes client_stacktrace_delay_seconds(), and the floor keeps every individual wait at the previous 10s, so no worker gets less time than before this change. A slow lldb attach can still outlast the budget there - that is the disclosed residual, unchanged by this PR and not introduced by it.
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 units9 translation units recompiled, 14 s compile time in total, 9 of them have a recent master baseline. |
| @@ -0,0 +1,446 @@ | |||
| """ | |||
| `tests/clickhouse-test`: the abnormal-stop wait must outlast the worker cleanup. | |||
There was a problem hiding this comment.
Removed in 871e2f4.
It pinned the parent's wait to the delays kill_process_group sleeps for, rather than to the constants, so a later retune of either delay could not silently reintroduce the mismatch. That is the only coverage lost; the fix itself is unchanged. If you want the property guarded without a unit test, I can add a stress-lane assertion instead - happy either way.
Requested in review. The test pinned the parent's wait to the delays kill_process_group sleeps for, rather than to the constants, so that the two could not drift apart again. The fix in tests/clickhouse-test is unchanged; it is now covered only by the CI runs that exercise the stop path. Co-Authored-By: Claude Fable 5 <[email protected]>
| WORKER_STOP_MARGIN_SECONDS = 10 | ||
|
|
||
|
|
||
| def terminate_workers(processes): |
There was a problem hiding this comment.
This fix no longer has any committed regression coverage for the derived stop budget. A future retune of SANITIZER_REPORT_GRACE_SECONDS / client_stacktrace_delay_seconds or a reversion to the inline join(timeout=10) sequence would stay green unless a stress or functional lane happens to reproduce the orphan leak again.
Please add the smallest focused runner-level test or deterministic CI assertion that exercises the abnormal-stop path and fails when the parent under-waits its workers.
There was a problem hiding this comment.
The coverage was removed on request: @ PedroTadim asked for ci/tests/test_clickhouse_test_stop_budget.py to be deleted (thread above), and I complied in 871e2f4. Restoring it here would revert that instruction, so I am not re-adding it.
Your point splits in two, and only one half is open:
- A retune of either delay cannot reintroduce the mismatch.
worker_cleanup_grace_secondsderives the budget from the same symbols the sleeps use:SANITIZER_REPORT_GRACE_SECONDSattests/clickhouse-test:745versus thesleepat:788, andclient_stacktrace_delay_seconds()at:747versus thesleepat:801. Both sides move together by construction, which is the property the deleted test pinned. - A reversion to a hardcoded
join(timeout=10)would go green with nothing to catch it. That is real and I am not claiming otherwise.
For that second half I offered a non-timing stress-lane assertion in the deletion thread and it is still on the table: one commit whenever either reviewer wants it. I am not pushing a replacement unasked a few hours after a maintainer removed the previous coverage.
|
@groeneai 🕵️ Premise checks out and the mechanism is right — a worker really can be inside kill_process_group's sleep(60) when the parent terminates it (process_result_impl calls it for a timed-out test, and the sleep is gated on SANITIZED alone), so the old flat join(timeout=10) killed it at second 10 and lost the report. Also confirmed collect_build_flags sets SANITIZED / RELEASE_NON_SANITIZED in main (line 5848) before do_run_tests (6130), so the parent computes a real grace rather than silently getting 0. One thing before it lands: worker_cleanup_grace_seconds() models a single kill_process_group invocation, but the worker's SIGTERM path can run several. stop_tests() → cleanup_child_processes() loops over children whose pgid differs from the worker's own and calls kill_process_group(child_pgid, None) for each, so on a sanitized build that's 60 * K seconds of sleeps against a deadline of 70 + 10. K is normally 0 (the worker's own group is SIGTERMed directly, without sleeping), but the comment at lines 777-787 says groups escaped via timeout(1) do happen — which is exactly the messy shutdown this patch targets. With K >= 1 the worker is still killed mid-cleanup, just later. The per-child get_stacktraces_from_lldb calls under CAPTURE_CLIENT_STACKTRACE are uncounted too. Not a regression — every case is strictly better than 10s — so this is fine to merge as an improvement. But I'd either widen the grace for the multi-group case, or make the K=0 assumption explicit in the docstring; "the wait must outlast that cleanup's sleeps" reads as a guarantee that only holds for one invocation. Two minor notes: The floor's rationale ("they are all already terminated, so a shared deadline alone would give the last ones no time at all") doesn't quite hold: workers clean up concurrently, so once the deadline passes they have each already had the full wall-clock grace. The floor's real value is covering a worker that was slow to start cleanup — same code, better reason. |
worker_cleanup_grace_seconds() models the sleeps of ONE kill_process_group call, but a worker's SIGTERM path can run several: cleanup_child_processes loops over children whose pgid differs from the worker's own and calls kill_process_group per child, so the sleeps are paid once per group. The docstrings read as a guarantee that only held for a single invocation. Measured with the shipped cleanup_child_processes in the real worker topology (kill_process_group counted, not executed): a worker holding one live test child gives one differing-pgid group, and a worker that killed a timed-out test's group without reaping it and then started the retry gives two. So the single-group case is the ordinary one rather than an empty one, and the multi-group case is reachable on exactly the messy shutdown this change targets. No behaviour change. The grace is deliberately left at one invocation: widening it to K groups makes the stop time scale with a quantity the parent cannot observe before it waits, and every wait is already strictly longer than the flat 10s it replaced, so no case regresses. Also corrects the floor's stated rationale. Workers clean up concurrently, so once the shared deadline passes each has already had the full wall-clock grace; the floor's real value is covering a worker that was slow to start its cleanup. Co-Authored-By: Claude Fable 5 <[email protected]>
|
@PedroTadim Your citations check out at the head, and the K >= 1 point is right, but K = 0 is not the normal case. I measured it with the shipped
So the ordinary sanitized case is 60s against a 70s deadline, which fits, and the two-group case is 120s against 70s, which does not. Your reading of the escape path is right, it is just one level lower than I took the docstring route, not the widening. Widening to K groups means the parent budgets a quantity it cannot observe before it starts waiting, and the sleeps are sequential per group, so the honest bound is unbounded in child-group count. Every wait is already at least the previous 10s, so the multi-group case is late rather than regressed. Both docstrings now say the budget covers one invocation and that later groups are still cut short, and I dropped the uncounted 0.1s SIGTERM-to-SIGKILL gap in as well since it is the same class of omission as the lldb collection. The floor's rationale is corrected to yours: it covers a worker slow to start its cleanup, not one that has already had the grace. Aggregate growth is in the body, measured rather than derived: N wedged workers cost 70 + (N-1)*10 seconds on a sanitizer lane, 80 + (N-1)10 with stacktrace capture, against N10 before. About +60s on 16 workers.
|
CI finish ledger - 6c8b118CI is fully finished on this head and no check failed, so the table below has no rows
Measured on the deduplicated check-run set (newest attempt per name): 171 unique names, The skipped majority is expected for a change confined to CIDB agrees independently for this head: 177176 Since the change alters when the stress harness force-kills its test workers, the green Session id: cron:our-pr-ci-monitor:20260820-173000 |
Related: #113393
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
No related open issue; found while investigating
Stress test (*)jobs failing with onlyscript exit code: 1.do_run_testsstopped a run by SIGTERMing each worker and waitingjoin(timeout=10)before SIGKILLing it.That worker's SIGTERM path reaches
kill_process_group, which on a sanitizer build sleeps 60 seconds,so every worker got 10 seconds for a 60 second cleanup and died inside that sleep.
Two consequences. The sanitizer report is truncated, which is what the grace in
1a76ce9b81ce3fcexists tocollect. And
p.kill()reaches only the worker, not the test's process group, whose PGID file is alreadyunlinked, so the test's
bashandclickhouse-clientsurvive. The harness's random query killer then killsthat leaked client and the orphaned
bashwritesTerminatedinto the log descriptor it inherited. Landing while the job packages the logs,tarexits 1 onfile changed as we read it, killing the job before the hung check.The wait is now derived from the delays cleanup sleeps for, so the two cannot drift apart again;
ddc6bcfbd68a761's SIGTERM, wait, SIGKILL, wait escalation is unchanged. Workers clean up concurrently, sothe grace is one monotonic deadline for all of them, and a ceiling rather than a sleep.
It bounds one cleanup invocation, not the whole cleanup: a worker owning several process groups pays the
sleeps per group and its later groups are still cut short. No wait is shorter than the previous 10 seconds,
so nothing regresses. Remaining gaps below.
Aggregate shutdown grows: N wedged workers cost 70 + (N-1)*10 seconds on a sanitizer lane, 80 + (N-1)10
with stacktrace capture, against N10 before, so about +60s on 16 workers. Cheap against losing a report,
but it is CI wall-clock.
A unit test pinning the budget was removed on review request, so it rests on the CI stop-path runs.
Affected runs, validation, #113393
All four are sanitizer lanes over three weeks,
base_ref=master. In each the worker log stops after theSANITIZED-gated print, never reachingKilling process group, and the archived log is 11 bytes short.Release and debug lanes, which skip that sleep, show none. Also uncovered: the stacktrace collection, an
uninterruptible sleep, and
spawnworkers (aarchnames), which have no cleanup.57ffb33, PR 111575,arm_asan_ubsan5639d68, PR 107736,arm_tsan542bb86, PR 112951,arm_msan6e15957, PR 114695,amd_asan_ubsanBefore the test was removed, eleven mutations each reddened it distinctly (flat 10s, no
kill()escalation, unbounded wait, sleep instead of ceiling, per-worker budget, either delay dropped, floor gone,
wall-clock deadline, call site reverted). End to end with real processes, the pre-change shape leaks the
bashand writes those 11 bytes 3/3; this change eliminates it 3/3.#113393 (mine, open) extracts this same sequence into
quiesce_workers_and_reap()and keepsp.join(timeout=10)verbatim, relocating this defect without fixing it. Complementary (that one reapsorphaned groups, this one stops creating them) but textually conflicting. I can rebase onto it or fold this
in there.
Workflow [PR]
Sync PR [sync-upstream/pr/115403]
Version info
26.10.1.250-master(included in26.10and later)