Repository navigation
Make the primes table function respond to query cancellation - #108353
Conversation
`PrimesSource::generate` / `PrimesSimpleRangedSource::generate` (src/Processors/Sources/SourceFromPrimes.h) run a `while (written < target)` loop calling the prime generator's `next` with no cancellation point. A large `step` (or `limit`) makes the loop call `next` an astronomical number of times to skip to the next selected prime index, so a single `generate` call never returns. The pipeline executor only checks cancellation and time limits between `generate` calls, so a cancelled, KILLed or timed-out query keeps spinning (observed as a query stuck with `is_cancelled = 1` and no progress in `read_rows`). `next` itself is bounded (segment size 2^19, prime gaps are far smaller), so the long pole is the `generate` loop. Check `isCancelled` at the top of both loops. It is a cheap atomic load, set by the executor on KILL / max_execution_time / shutdown, and each iteration already does a bounded `next`, so the overhead is negligible. This was the hung query behind a `Stress test (arm_tsan)` "Hung check failed, possible deadlock found" on PR #108212 (server-side AST fuzzer feeding boundary arguments to `primes`). Related: #108212 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [51fd094] Summary: ❌
AI ReviewSummaryThis PR makes the Final VerdictStatus: ✅ Approve |
The test gated `KILL QUERY` on the query merely being visible in
`system.processes`. That only proves the query reached `ProcessList::insert`,
which happens before the `PipelineExecutor` is attached -- if `KILL` won that
race, the cancellation would be thrown from `QueryStatus::addPipelineExecutor`
before any source work runs, so the test could pass even without the in-loop
`isCancelled()` check it is meant to guard.
Wait until the query has been executing for a bounded interval (`elapsed > 1` in
`system.processes`) before issuing the `KILL`, so the executor is attached and
`PrimesSource::generate` is spinning in its loop. Also add `no-random-settings`:
the SELECT must keep running until the test kills it, so a randomized
`max_execution_time` (or `max_rows_to_read`) must not terminate it first.
Verified on a local build: with the fix `KILL QUERY ... SYNC` returns in ~0.2s
("killed promptly") after the query has been generating for ~1.2s; with the
in-loop `isCancelled()` checks removed and rebuilt, the same `KILL` blocks on the
spinning generate loop and the test prints "KILL QUERY SYNC did not return in
time", so the test now genuinely guards the fix.
nihalzp
left a comment
There was a problem hiding this comment.
It looks good. I think the performance should be almost the same. I have added a performance test to be sure.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 8/10 (80.00%) | Lost baseline coverage: none · Uncovered code |
nihalzp
left a comment
There was a problem hiding this comment.
LGTM! The performance tests show no degradation.
|
@alexey-milovidov Both are the same check, "Hung check failed, possible deadlock found", and neither is caused by the primes change. This PR only touches
A fuzzer-inflated INSERT keeps grinding in the part writer after the outer fuzzer query is cancelled, because the inner executor has no cancel callback and no read/block-size cap. This is the AST-fuzzer-inflight-hang tracked in #107941; over the last 30 days it hits hundreds of unrelated PRs plus master on every stress sanitizer, so it is pre-existing and not introduced here. Fix: our open PR #108212 modifies exactly |
…e-cancellation Make the primes table function respond to query cancellation
Problem
A
SELECTfrom theprimestable function can keep running for a long time afterKILL QUERY(or amax_execution_time/ shutdown) when given a largesteporlimit. The query stays insystem.processeswithis_cancelled = 1and makes no progress inread_rows. This was the hung query behind aStress test (arm_tsan)"Hung check failed, possible deadlock found" (the server-side AST fuzzer feeds boundary arguments toprimes).Root cause
PrimesSource::generate/PrimesSimpleRangedSource::generate(src/Processors/Sources/SourceFromPrimes.h) run awhile (written < target)loop that calls the prime generator'snextwith no cancellation point. A largestepmakes the loop callnextan astronomical number of times to skip to the next selected prime index, so a singlegeneratecall never returns. The pipeline executor only checks cancellation and time limits betweengeneratecalls, so the in-flight call cannot be interrupted.nextitself is bounded (segment size2^19, prime gaps are far smaller), so the long pole is thegenerateloop, not a singlenext.Fix
Check
isCancelledat the top of bothgenerateloops and return early.ISource::isCancelledis a cheap atomic load, set by the executor on KILL /max_execution_time/ shutdown, and each iteration already does a boundednext, so the overhead is negligible.Regression test
04410_primes_source_cancellation.sh: runsSELECT * FROM primes(0, 1000000, 100000000000)(hugestep, withmax_rows_to_read = 0to bypass the up-front row-estimate check) in the background, waits until it is insystem.processes, then requiresKILL QUERY ... SYNCto return within atimeoutbound. With the fix it returns in ~1 s; without it theKILLblocks until the bound trips.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
SELECTfrom theprimestable function not responding to cancellation: with a largestep(orlimit) the query could keep running for a long time afterKILL QUERYor a timeout. It now stops promptly.Version info
26.7.1.205(included in26.7and later)