Skip to content

Fix flaky cancel_insert_when_client_dies tests on fast builds - #111157

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/04365-arm-binary-cancel-timing-race
Aug 18, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/04365-arm-binary-cancel-timing-race

Conversation

@groeneai

@groeneai groeneai commented Jul 21, 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):

...

Description

Requested by @ alexey-milovidov on #109261: fix the flaky
04365_cancel_insert_when_client_dies_distributed on Stateless tests (arm_binary, parallel)
(the line-3 1 -> 0 flip). This is a separate PR from the coverage-only stdout fix.

thread_cancel took a single /proc/*/cmdline snapshot per loop, then slept 3x sleep 0.$RANDOM
(0.$RANDOM is lexical, up to 0.9999s; mean of the three ~0.95s), so it sampled for an in-flight
insert only about once per second. On fast release/binary builds a 500000-row insert finishes in
well under a second, so the once-per-second snapshot repeatedly lands between inserts, no insert is
cancelled during the 40s window, no cancellation message is logged, and the guard
select count() > 0 from system.text_log returns 0 -> the test fails. The server behavior and data
integrity are correct (the table stays at 500000 rows); only the "did we cancel anything?"
observation is missed.

Fix: thread_cancel now polls at 10ms until an insert client is actually in flight, then kills it
immediately (one sleep 0.$RANDOM retained to keep the signal timing randomized). The assertion,
the SIGINT/SIGKILL mix, the row counts, and the tags are unchanged, so the test still requires a
real cancellation to be observed.

The thread_cancel helper is identical across the three native-protocol cancel-insert tests and
all three flake on arm_binary, so the same fix is applied to each:
02434_cancel_insert_when_client_dies, 04365_cancel_insert_when_client_dies_distributed,
04366_cancel_insert_when_client_dies_insert_select.

Note: #108864 separately adds a no-llvm-coverage tag to 02434 for an unrelated coverage
stdout-pollution issue; it touches only the tags line, so it does not overlap this change.

Reproduced both directions locally by extracting the actual thread_cancel from the file and
running it against fast inserts (sub-10ms in-flight window): the current version flips ~38% of
runs, the polling version is stable (30/30). The tests are no-random-settings and run only on
release/binary builds, so a --random-settings sweep does not apply.

CI report for the arm_binary failure on #109261:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109261&sha=e532419f9209dd0e350462cfcc698dcbe4c1f31d&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29


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

thread_cancel took a single /proc snapshot per loop then slept 3x
sleep 0.$RANDOM (~1s), so it sampled for an in-flight insert only about
once per second. On fast release/binary builds an insert finishes well
under a second, so the sampler repeatedly lands between inserts, nothing
gets cancelled in the 40s window, and the "did we cancel anything" guard
returns 0. Poll at 10ms until an insert client is in flight, then kill it
at once. The assertion, the SIGINT/SIGKILL mix, the row counts, and the
tags are unchanged. The helper is identical across the three native
cancel-insert tests and all three flake on arm_binary, so the same fix
is applied to 02434, 04365 and 04366.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@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; 1 finding; bounded: 1 full pass + 1 fix round + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ⚠️ The poll detects a client process, not an in-flight server query, so it could repeatedly kill clients before the insert registers server-side and starve the required cancellation evidence. DISAGREE Disproved empirically with the actual polling thread_cancel: poll logs MORE server-side cancellation evidence than the current test, not less. Recorded system.text_log cancellation-message rows (EVID) - poll: 14 (20k rows) and 24 (500k rows), 53/53 and 58/58 kills landing; current test: 10 (500k) and 0 (20k, which failed). Extracting the committed thread_cancel and running it against fast inserts gave 30/30 PASS with the guard = 1, vs ~38% flip for the current version. If the 10ms poll systematically killed clients before the query registered, the guard would be 0 across those 30 runs; the opposite is observed. thread_insert runs inserts back to back and connection setup is a small fraction of the client's ~0.1s lifetime, so a 10ms poll overwhelmingly finds a client already mid-insert; an occasional kill during setup just yields no evidence that iteration and the next poll catches one. No change made.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-author-slot-2:20260720-231200

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Extracting the actual thread_cancel from the file and running it against fast inserts (sub-10ms in-flight window, mimics arm_binary): current version flips ~38% (5 pass / 3 fail of 8), fixed version 30/30 pass. Trigger fully isolated: sub-second insert vs the coarse ~1s single-snapshot sampling.
b Root cause explained? Yes. thread_cancel took ONE /proc/*/cmdline snapshot per loop then slept 3x sleep 0.$RANDOM (0.$RANDOM is lexical, up to 0.9999s; mean of three ~0.95s), so it sampled ~once/second. On fast builds an insert finishes well under a second, so the snapshot lands between inserts, nothing is cancelled in the 40s window, no cancellation message is logged, and the guard count() > 0 returns 0 -> line 3 flips 1 -> 0.
c Fix matches root cause? Yes. Replaces the single blind snapshot + 3 long random sleeps with a 10ms poll that waits until an insert client is actually in flight, then kills it immediately (one sleep 0.$RANDOM kept to keep signal timing randomized). Directly closes the sampling gap.
d Test intent preserved / new tests added? Yes. The guard still requires a real cancellation to be observed (count() > 0 unchanged); SIGINT/SIGKILL random mix, 500000-row / 40s window, and all assertions unchanged; no tags added, no bounds widened.
e Both directions demonstrated? Yes. Using the committed thread_cancel: current version fails ~38% under fast inserts; fixed version 30/30 pass. 20k-row comparison: current GUARD=0 EVID=0 (fail) vs fixed GUARD=1 EVID=14 (pass). Data integrity (row count) held every run.
f Fix is general across code paths? Yes. thread_cancel is byte-identical across the three native-protocol cancel-insert tests (02434, 04365, 04366) and all three flake on arm_binary; the identical fix is applied to all three (post-fix all three helpers are again byte-identical).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-harness timing fix, not a code/datatype bug). The poll is input-agnostic - it waits for whichever insert client is in flight regardless of protocol, signal, or data.
h Backward compatible? (maintainer-approved exception only) N/A (test-only change; no server behavior, settings, or on-disk/wire format touched; the tags line is unchanged so the same CI jobs run these tests).
i Invariants and contracts preserved? Yes. The inner poll loop is bounded by the same TIMELIMIT as the outer loop (never runs past the 40s window; on shutdown it exits when SECONDS reaches TIMELIMIT and the empty-PID guard still protects the kill). No change to thread_insert/thread_select, the wait, or the guard query.

Session id: cron:clickhouse-author-slot-2:20260720-231200

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [37e7705]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_binary, parallel) FAIL
04676_adaptive_aggregation_replicated_argument FAIL cidb IGNORED
Stress test (arm_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb IGNORED

AI Review

Summary

This PR replaces the coarse /proc/*/cmdline sampling in the three cancel_insert_when_client_dies shell tests with a bounded 10 ms poll so thread_cancel can reliably catch short-lived insert clients on fast builds. I did not find a remaining contract violation in the current diff: the helper still respects the original TIMEOUT, the "must observe a real cancellation" guard is unchanged, the three sibling tests stay aligned, and the current PR CI is green.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 8a43197

Both red checks are pre-existing flaky tests unrelated to this PR's change (this PR only touches 02434/04365/04366 cancel-insert tests). Each is owned by an open fixing PR:

Check / test Reason Owner / fixing PR
Stateless tests (amd_asan_ubsan, distributed plan, parallel) / 01730_distributed_group_by_no_merge_order_by_long flaky (randomized-settings diagnosis: 15/15 reruns passed, not reproducible) #109847 (external, open)
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) / 04507_mvt_encode_state_serialize flaky (fails only under parallel replicas, rc 70) #111148 (external, open)

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111157&sha=8a43197aa9f9634f173035a0ddb8ed7269f6aa39&name_0=PR

@PedroTadim PedroTadim self-assigned this Aug 18, 2026
@PedroTadim
PedroTadim enabled auto-merge August 18, 2026 09:38
@clickhouse-gh

clickhouse-gh Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 37e7705eb with master 2bb9dae14 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 702.05 MiB 699.03 MiB -3.03 MiB (-0.43%)

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 units

6 translation units recompiled, 4 s compile time in total, 6 of them have a recent master baseline.

Job report

@PedroTadim
PedroTadim added this pull request to the merge queue Aug 18, 2026
Merged via the queue into ClickHouse:master with commit b03b21d Aug 18, 2026
178 of 181 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 18, 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 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