Repository navigation
Fix flaky 04365_cancel_insert_when_client_dies_distributed under host load - #119778
alexey-milovidov merged 1 commit into
Conversation
…s_distributed thread_cancel located the client to kill by grepping /proc/*/cmdline. That sweep globs every process on the host and forks five processes per poll, so its cost is set by the machine's process count rather than by the test: measured on a shared 96-core box, one sweep takes 0.08 s at 6,400 host processes and 0.31 s at 27,900, against the loop's intended 10 ms. An insert client here lives about 130 ms, of which the server-side insert is 42.5 ms. Once a sweep outlasts the client, two things fail at once. The poll starves, so the number of kills in a 40 s run falls from about 120 to between 0 and 8; and the pid a sweep returns is up to one sweep old, so 12 to 50% of the kills that do fire hit a process that has already exited. No cancellation then lands inside the in-flight window, and the anti-vacuity guard on line 3 prints 0 truthfully while both data lines stay at 500000. That is the failure seen in CI: 10 public occurrences across 9 pull requests in 90 days, all on ..., parallel lanes. 8a43197 shortened the poll interval but kept the sweep, which is why the failure survived it. insert_data now runs its client asynchronously and publishes the pid, and thread_cancel reads that one pid, accepting it only while /proc/<pid>/cmdline still carries the query id, so liveness and identity are checked at once and a reused pid is never killed. The guard, its whitelist and the reference file are untouched. Backgrounding is safe for this test in particular because clickhouse-client installs its own SIGINT handler (ClientApplicationBase::setupSignalHandler), overriding the SIG_IGN that bash gives an asynchronous command. A backgrounded curl does not, and was measured to survive SIGINT, so 02434 and 04366 keep the sweep even though they were measured to flip 3/3 under the same induced load; porting this there needs a different way to publish the pid. Measured: 9 failures in 11 unmodified runs against 0 in 114 runs with this change, 110 to 133 cancellations per run, every kill landing on a live pid, and the graceful-cancel evidence unchanged at 7 to 10 text_log rows per run. Six alternating pairs place the flip boundary between a 0.09 s and a 0.26 s sweep, with the fixed test flat across that range.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus 4 findings from
Severity: ❌ blocker / Independently re-verified while reviewing, rather than taken from the implementation notes: Session id: cron:clickhouse-review-slot-10:20260913-111400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260913-091400 |
|
Workflow [PR], commit [fc2b4b2] Summary: ✅ AI ReviewSummaryThe PR changes Final Verdict✅ No findings. |
44d65c7
Related: #96844
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
04365_cancel_insert_when_client_dies_distributednow finds the insert client it has to kill in constant time instead of scanning every process on the host, so its "something was cancelled" guard no longer reads0on a loaded machine.Description
Requested by @ alexey-milovidov in #96844 (comment). Line 3 of the reference flips to
0while both data lines stay500000: 10 public failures across 9 pull requests in 90 days, all on..., parallellanes. His own report is on a private lane I cannot read, so this PR fixes the public signature; his lane is..., paralleltoo.Root cause.
thread_cancellocates the in-flight client by grepping/proc/*/cmdline, which costs O(all processes on the host) and forks five processes per poll. One sweep measured 0.08 s at 6,400 processes and 0.31 s at 27,900, against a 10 ms poll, while the client lives about 130 ms. Once a sweep outlasts the client, the poll starves (kills per 40 s run fall from ~120 to 0-8) and the pids it returns are stale (12-50% hit an exited process). Nothing gets cancelled, and the anti-vacuity guard prints0truthfully. #111157 shortened the poll interval but kept the sweep.Change.
insert_dataruns its client asynchronously and publishes$!;thread_cancelreads that one pid and accepts it only while/proc/<pid>/cmdlinestill carries the query id: liveness and identity in one check. The guard, its whitelist and.referenceare untouched.Validation (debug build, one server): 9 failures in 11 unmodified runs, 0 in 114 fixed runs including two
clickhouse-test --test-runs 50sets, with 110-133 cancellations per run and every kill landing on a live pid.Received 'Cancel' packetrows stay at 7-10 per run, so SIGINT still reaches a backgrounded client:clickhouse-clientinstalls its own handler,curldoes not. That is why02434and04366keep the sweep here, even though they flip 3/3 under the same induced load.Its 24 master hits over 90 days are a different,
LLVM Profile Errorfacet, fixed by merged #113619.Measurements
Six alternating pairs, one unmodified run and one fixed run each, with the sweep cost sampled just before. The fleet's own load fell during the arm, so no processes were spawned for this table:
Over all 11 unmodified runs the guard reads
1in every run with at least 33 kills and0in every run with at most 8, and no run of any variant ever produced a row count other than500000.A cheaper scan does not help:
pgrep -fandpsalso walk all of/proc, so they stay O(host processes); only publishing the pid makes the lookup independent of host load.Two mutants of the fixed test, three runs each, both expected to fail and both failing with guard
0: disablingkill -s(the poll still found the client 115-126 times per run), and publishing the pid where the poll does not read it (2,600 polls per run, no candidate ever). A control of the fixed test in the same window passes 3/3 with 129-133 kills.Client wall time per insert path, 5 invocations each: 04365 native 116-137 ms; 02434 native 104-116 ms; 02434
curl31-36 ms; 04366curl80-90 ms. So the two curl-driven siblings are not protected by a longer-lived client, and with 6,000 extra processes on the host (sweep 0.33-0.36 s) they flip 3/3 each. Porting this fix to them needs a different way to publish the pid, because a backgroundedcurlignores SIGINT, which would silently turn half of their kill signals into no-ops.One thing this does not touch: the whitelist entry
Query was cancelled or a client has unexpectedly dropped the connectionhas matched nothing insrc/sincecea797e254e6(2024-11-11) and is still present in four test files.Workflow [PR]
Sync PR [sync-upstream/pr/119778]