Skip to content

Fix flaky test 04365_cancel_insert_when_client_dies_distributed - #108343

Merged
CheSema merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-04365-distributed-cancel-insert-native-match
Jun 24, 2026
Merged

CheSema merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-04365-distributed-cancel-insert-native-match

Conversation

@groeneai

@groeneai groeneai commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

No related open issue found (searched issues + PRs for the test name; none exist).

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

Fixes flaky 04365_cancel_insert_when_client_dies_distributed (failed flakily in two unrelated PRs, 0 master hits; 2 FAIL / 59 OK in 30 days).

The test cancels an INSERT into a Distributed table over the native protocol only (the HTTP variants live in the siblings 02434 / 04366). Its final "ensure that thread_cancel actually did something" assertion was inherited from the HTTP-heavy 02434 and matches only HTTP-chunked / socket-drop messages (plus the predicate 'Query was cancelled or a client has unexpectedly dropped the connection', which exists only in the test files, never in the server source).

On the native protocol a SIGINT makes the client send a graceful Cancel packet and keep the connection open, so the server logs QUERY_WAS_CANCELLED_BY_CLIENT (Received 'Cancel' packet from the client, canceling the query.) rather than a broken pipe / connection reset. The socket-drop messages only fire when a SIGKILL lands while the server happens to be writing back to the client. thread_cancel picks INT or KILL with equal probability, so roughly half the cancellations produce no message the assertion recognizes, and the SIGKILL half matches only when the kill races a server-side write. Over the 40s run the margin is thin and the assertion occasionally observes zero matching messages, printing 0 instead of 1.

Fix: add the native-protocol cancellation messages to the matcher. Verified locally against a debug build with the test's cluster pointed at a single capped server:

  • A SIGINT interrupting an in-flight native distributed insert logs the cancel-packet message 11/12 times while matching the old filter 0/12 (directional proof: old assertion -> 0, the flaky failure; new assertion -> 1).
  • A normal uncancelled insert never logs it, so the assertion still requires a real cancellation.
  • The full .sh test passes 3/3 end-to-end (output 500000 / 500000 / 1).

Only the assertion's message list changes; the test's behavior and .reference are unchanged.

Version info

  • Merged into: 26.6.1.1200

The test drives the cancelled INSERT only over the native protocol, but its
final "ensure that thread_cancel actually did something" assertion was inherited
from the HTTP-heavy sibling 02434 and matches only HTTP-chunked / socket-drop
messages (plus a predicate that exists only in the test files).

On the native protocol a SIGINT makes the client send a graceful 'Cancel' packet
and keep the connection open, so the server logs QUERY_WAS_CANCELLED_BY_CLIENT
("Received 'Cancel' packet from the client, canceling the query.") instead of a
broken pipe or connection reset. The socket-drop messages only fire when a SIGKILL
lands while the server happens to be writing back to the client. thread_cancel
picks INT or KILL with equal probability, so roughly half of the cancellations
produce no message the assertion recognizes, and the SIGKILL half matches only
when the kill races a server-side write. Over the 40s run the margin is thin and
the assertion occasionally observes zero matching messages, printing 0 instead of 1.

Add the native-protocol cancellation messages to the matcher. Verified locally:
a SIGINT interrupting an in-flight native distributed insert logs the cancel
packet message 11 out of 12 times while matching the old filter 0 times; a normal
uncancelled insert never logs it, so the assertion still requires a real
cancellation.

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. Launch a native-protocol INSERT into the Distributed table, sleep 0.06, then kill -s INT <client_pid> while it is in flight. 11/12 such kills log Received 'Cancel' packet from the client, canceling the query. and the OLD assertion filter matched 0/12, deterministically printing 0 instead of 1.
b Root cause explained? The test cancels INSERTs over the native protocol only. A SIGINT makes the native client send a graceful Cancel packet and keep the connection open, so the server logs QUERY_WAS_CANCELLED_BY_CLIENT (Received 'Cancel' packet...), not a broken pipe / connection reset. The assertion was copied from the HTTP-heavy sibling 02434 and only matches HTTP-chunked / socket-drop messages (plus the predicate 'Query was cancelled or a client has unexpectedly dropped the connection', which exists only in the test files, never in src/). thread_cancel picks INT/KILL 50/50; the SIGINT half is invisible to the matcher and the SIGKILL half only matches when the kill races a server-side write to the client (broken pipe). Over 40s the margin is thin, so the assertion occasionally observes zero matches.
c Fix matches root cause? Yes. The fix adds the exact native-protocol cancellation message_format_strings the server produces (Received 'Cancel' packet from the client, canceling the query. and the defensive sibling Packet 'Cancel' has been received from the client, canceling the query.) to the assertion's matcher. It targets the identified mechanism, not a symptom; no bounds widened, no no-random-* tag added, no data reduced.
d Test intent preserved / new tests added? Preserved. The assertion still verifies that thread_cancel actually cancelled an in-flight insert. Verified a normal uncancelled insert logs the cancel-packet message 0 times, so the assertion cannot pass without a genuine cancellation. No reference change (output stays 500000 / 500000 / 1).
e Both directions demonstrated? Yes. Same in-flight-SIGINT repro: OLD filter -> 0 (the flaky failure), NEW filter -> 1 (pass). Full .sh test run end-to-end 3/3 PASS against a local debug build with the cluster pointed at one capped server.
f Fix is general across code paths? N/A (flaky test assertion fix, not a code bug). Scope is deliberately limited to this native-only test; siblings 02434 (2/3 HTTP) and 04366 (100% HTTP) reliably produce the socket-drop messages and are not flaky, so they are intentionally left unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (flaky test assertion fix, not a code bug).
h Backward compatible? (maintainer-approved exception only) N/A (flaky test assertion fix, not a code bug).
i Invariants and contracts preserved? N/A (flaky test assertion fix, not a code bug).

Session id: cron:clickhouse-worker-slot-0:20260623-224400

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CheSema — could you review this? This test (which you split out in #107886) drives the cancelled insert only over the native protocol, where SIGINT sends a graceful Cancel packet instead of dropping the socket, so the final "ensure thread_cancel did something" assertion (inherited from the HTTP-heavy 02434) misses the QUERY_WAS_CANCELLED_BY_CLIENT messages and occasionally prints 0 instead of 1. The fix just adds those native cancel-packet messages to the matcher.

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

clickhouse-gh Bot commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [401bc6c]

Summary: ❌

job_name test_name status info comment
Stress test (arm_asan_ubsan, s3) FAIL
Logical error: Distributed task iterator is not initialized (STID: 4662-3e65) FAIL cidb, issue

AI Review

Summary

This PR fixes the flaky 04365_cancel_insert_when_client_dies_distributed stateless test by expanding its final system.text_log assertion to recognize native-protocol Cancel packet cancellation messages in addition to the existing HTTP/socket-drop messages. The new strings match the QUERY_WAS_CANCELLED_BY_CLIENT exceptions thrown by TCPHandler, the PR is correctly categorized as CI Fix or Improvement, and I found no review findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 24, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.30% -0.10%
Functions 92.60% 92.60% +0.00%
Branches 77.60% 77.60% +0.00%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 435 line(s), 103 function(s) across 138 file(s) · Details

Top files
  • src/Functions/FunctionsBitmap.h: 27 line(s)
  • src/Functions/catboostEvaluate.cpp: 17 line(s), 4 function(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesChanges.h: 14 line(s), 32 function(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeseriesLinearRegression.h: 14 line(s), 32 function(s)
  • src/IO/S3/copyS3File.cpp: 13 line(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 401bc6c

Every failure below links a fixing PR (ours or external). Only CH Inc sync is exempt.

Check / test Reason Fixing PR
Stress test (arm_asan_ubsan, s3) / Logical error: Distributed task iterator is not initialized (STID 4662-3e65) chronic trunk bug, NOT PR-caused (this PR is a 1-file +6/-2 test-assertion-filter change to 04365; the stress fuzzer generated a nested cluster-table-function query reaching the trunk LOGICAL_ERROR; 6 master hits + ~24 unrelated PRs in 30d) #107107 (ours, open)
PR aggregator rollup of the above red n/a (aggregator)

The 04365 test this PR fixes is green on this commit (0 FAIL; SKIPPED in sanitizer builds per its tags, OK on arm_binary). Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108343&sha=401bc6c8948aafec9d012dc420984876161f259e&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29

Session id: cron:our-pr-ci-monitor:20260624-043000

@Algunenano
Algunenano requested a review from CheSema June 24, 2026 08:30
@CheSema
CheSema added this pull request to the merge queue Jun 24, 2026
@CheSema CheSema self-assigned this Jun 24, 2026
Merged via the queue into ClickHouse:master with commit 9d4d0e6 Jun 24, 2026
165 of 167 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 24, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ted-cancel-insert-native-match

Fix flaky test 04365_cancel_insert_when_client_dies_distributed
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