Skip to content

Fix the race between KILL QUERY and the memory limit in 05315_freeform_structure_search_query_limits - #131525

Merged
fm4v merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-05315-freeform-kill-race
Nov 19, 2026
Merged

fm4v merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-05315-freeform-kill-race

Conversation

@groeneai

@groeneai groeneai commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #110102

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

Fast test (arm_darwin) on #110102 failed the KILL case of this test with MEMORY_LIMIT_EXCEEDED instead of QUERY_WAS_CANCELLED (report). It is not caused by #110102: 1 failure in about 170K runs since the test was added.

The test polled system.processes until the search reached 10 MB and only then started a client for KILL QUERY. On that runner no client of any test could connect to the server for 8.4 s right after the poll, so the search reached its own max_memory_usage = 2000000000 and failed, and the KILL arrived while the query was freeing its candidates.

Changes, test only:

  • The KILL is the poll and matches only while peak_memory_usage is between 10 MB and 1 GB, so a sent KILL is always seen by the search and a late one matches nothing. The peak, because memory_usage falls back through that range while a failed query frees its memory.
  • If the query ends before a KILL reaches it (no match, or a kill_status other than waiting), the attempt is repeated, at most 3 times. A KILL that reaches the query ends the loop and its result is checked.
  • kill_throw_if_noop = 0, the client output goes to a file under timeout 60, and the rows are built without yes. The last two come from Alexey Milovidov's review commit ba2c2d703b40 on Stop the Freeform structure search at the query's memory and time limits #123238, which never reached master.

Validation on a debug build: delaying the KILL client by 8.45 s gives the CI result with the old test (stdout MEMORY_LIMIT_EXCEEDED, empty stderr, 10 s), and the new test passes under the same delay. 100 randomized runs and 64 runs with 8 in parallel pass with no retry (p50 1.28 s). A local engine change that makes the search ignore the cancellation fails the new test 5/5.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.2808-master (included in 26.11 and later)

groeneai and others added 3 commits November 14, 2026 01:42
… memory limit

The KILL case of `05315_freeform_structure_search_query_limits` lost a race against the query's own
`max_memory_usage = 2000000000` on `Fast test (arm_darwin)` (PR ClickHouse#110102): no client could connect to
the server for 8.4 s right after the poll saw the search under way, the search reached its limit and
threw `MEMORY_LIMIT_EXCEEDED`, and the KILL then landed on the query while it was freeing its
candidates.

The KILL is now the poll itself and matches only while `peak_memory_usage` is between 10 MB and 1 GB,
so a sent KILL is always seen by the search, and a late one matches nothing instead of racing the
limit. The peak, not `memory_usage`: the current usage of a query that failed on its limit falls back
through the window while it frees its candidates. An attempt in which the query ended before any KILL
could be sent says nothing about KILL and is repeated, at most 3 times. `kill_throw_if_noop = 0`
keeps the empty matches off stderr. The client output goes to a file and the client is bounded by
`timeout 60`, as in the review change `ba2c2d703b40` of ClickHouse#123238, which never reached master.

Related: ClickHouse#110102
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`yes | head` prints `yes: standard output: Broken pipe` to stderr where SIGPIPE is ignored, which
fails a `.sh` test. The rows are byte-identical. This is part of the review change `ba2c2d703b40` by
Alexey Milovidov on ClickHouse#123238, which never reached master.

Co-Authored-By: Alexey Milovidov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`KILL QUERY ... ASYNC` also returns a row with `kill_status = finished` when the
query left `system.processes` between the KILL's snapshot and the cancel. Such an
attempt says nothing about KILL, so only `waiting` ends the attempts.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
⚠️ KILL QUERY ... ASYNC also returns a row (kill_status = finished) when the query left system.processes between the snapshot and the cancel; the loop treated any row as a sent KILL AGREE, fixed: only waiting ends the attempts
💡 Run counts in the description AGREE, fixed
💡 No explicit message when a client outlives the KILL Not changed: timeout 60 bounds the client and the result line fails the reference

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The old test with the KILL QUERY client delayed by 11 s fails 4/4 (MEMORY_LIMIT_EXCEEDED, NOTHING_TO_KILL on stderr); delayed by 8.45 s it gives the CI result 4/4 (MEMORY_LIMIT_EXCEEDED, empty stderr, about 10 s).
b Root cause explained? The KILL was sent by a separate client after a poll saw 10 MB. The search grows about 200 MB/s and reaches its own 2 GB limit after about 9 s; when no client can connect for longer than that (8.4 s on the darwin runner), the query fails on the limit and the KILL arrives during its cleanup.
c Fix matches root cause? Yes. The KILL is evaluated by the server when it arrives and matches only at peak 10 MB to 1 GB, far below the limit, so client scheduling no longer decides the result. An attempt where no KILL reached the query (no match, or a kill_status other than waiting) is repeated, at most 3 times. No bound widened, no tag added.
d Test intent preserved / new tests added? Yes. The case still needs QUERY_WAS_CANCELLED from a search that is under way. A local engine change that makes the search ignore the cancellation fails the new test 5/5 (one KILL sent, no retry). Test-only change, .reference unchanged.
e Both directions demonstrated? Yes. Same delays: old test fails 4/4 (11 s) and 4/4 (8.45 s); new test passes 4/4 (11 s) and 3/3 (8.45 s).
f Fix is general across code paths? The other four cases have no external actor and stop at 0.1 s or 20 MB, so a client stall cannot change them. No engine change.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only timing fix).
h Backward compatible? (maintainer-approved exception only) N/A (test-only).
i Invariants and contracts preserved? Yes. Same cases and reference; the attempts end on a KILL that reached the query (waiting), and each attempt's polling on any KILL row, on client exit, or at the test deadline; the client is bounded by timeout 60; both temporary files are removed on exit. 100 randomized runs and 64 runs with 8 in parallel pass with no retry.

Session id: cron:clickhouse-impl-slot-4:20261114-024300

@clickhouse-gh

clickhouse-gh Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e8bf361]

Summary: ✅


AI Review

Summary

This test-only change combines polling with KILL QUERY, uses peak memory to exclude cleanup after a memory-limit exception, and retries attempts where cancellation was not sent. The current diff preserves the expected cancellation result. No actionable findings; the focused test passed in the Fast test report.

Final Verdict

✅ Approve.

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors comp-testing Test content only (no production code). labels Nov 14, 2026
@fm4v
fm4v added this pull request to the merge queue Nov 18, 2026
Merged via the queue into ClickHouse:master with commit b737158 Nov 19, 2026
606 of 608 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 19, 2026
@clickhouse-gh clickhouse-gh Bot added comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). and removed comp-testing Test content only (no production code). labels Nov 22, 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 comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). 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.

3 participants