Repository navigation
Conversation
A QueryRunner table used to start its `threads` workers at CREATE and ATTACH time and keep them blocked on an empty queue for the table's whole lifetime. A ThreadPool worker thread is itself a job on the global pool, so those blocked workers held `threads` global slots permanently, and `threads` is validated only per table (range [1, 1024]) with no server-wide budget. Because the workers never returned, the pool's idle-reclaim path was never reached. Measured on a table that received no inserts: GlobalThread goes from 2 to 1027 right after CREATE ... threads = 1024. Twelve such idle tables exceed the 10000-slot pool, and the next ordinary query fails with Code: 439 ... no free thread (threads=10000, jobs=10000) (CANNOT_SCHEDULE_TASK) after which the server does not recover until the tables are dropped. In CI this surfaced as an AST fuzzer job failing with a bare "Received signal 15" on any branch whose corpus reached 04337_query_runner.sql, because the fuzzer mutates the `threads = 2` literal in that test. The dispatcher was reimplementing, on top of ThreadPool, a concurrency cap, a bounded work queue and on-demand worker startup, and that reimplementation is what broke thread reclamation. Submit each query to the pool directly and delete the private queue and the worker loop, so the pool supplies all three: max_free_threads = 0 keeps no thread for an idle table, queue_size is the saturating sum of `threads` and `max_queue_size` (which counts running plus scheduled jobs, matching the previous total capacity), and shutdown_on_exception = false keeps one failing user query from disabling the table. Completion accounting has to fire exactly once however the pool disposes of a task: it may run it, refuse it, dequeue it unrun at shutdown, or destroy the copy it makes of the task. A shared, non-copyable completion state held by the task covers all four. The window before that state exists keeps its own exception guard, because a throw there would otherwise leave the sequence number pending and block SYSTEM WAIT QUERY RUNNER, which polls without a timeout. An explicit admission boundary replaces the queue's finish flag: shutdown closes admission before waiting on the pool, so an insert racing a drop cannot schedule a task that outlives the dispatcher. Closes: ClickHouse#114367
Review round 1: three of the five arms in 04892_query_runner_threads_not_held_when_idle could not fail for the reason they were named for. The idle-thread arm asserted a count below 512 where both the contract and the measurement are exactly zero, so a regression retiring only some workers would have passed. It now measures a delta across the CREATE, which is tighter than any absolute bound and immune to QueryRunner tables held by tests running in parallel. The queue-refusal arm printed an unconditional literal, so its output was identical whether the surplus queries were refused, all of them ran, or the pool's queue silently became unlimited - the last being the regression the saturating sum exists to prevent. The submitted queries now have a counted side effect and the arm asserts a two-sided bound on it. The dropped-table arm used synchronous mode, where the INSERT waits for its whole batch, so nothing was ever still outstanding when the DROP ran. It is now asynchronous, proves by metric that jobs are outstanding at DROP time, and asserts that the abandoned ones did not execute. Each new oracle was verified by mutation: forcing the pool's queue to be unlimited reddens the refusal arm, and removing the early return that abandons jobs of a shut-down table reddens the drop arm. Also drops QueryRunnerJob::seq, which no longer has a reader or a writer.
QueryRunnerThreads and QueryRunnerThreadsScheduled are single counters shared by every QueryRunner table in the process, so a stateless test cannot own them. The functional test carries no no-parallel tag, its number puts it in range of --repeat-newly-modified-tests, and 04337_query_runner creates QueryRunner tables too, so copies of it can be inside a measured window at the same time. That contaminates the assertions in both directions: a neighbour's worker can make the idle-thread reading positive when this table holds nothing, and can satisfy a "work is outstanding" reading when this table has submitted nothing. Move both metric assertions to tests/integration/test_query_runner, where each xdist worker gets a dedicated server, and pin them exactly there: an idle table holds no threads, a table that has finished its queries also holds none, a table accepts exactly threads + max_queue_size queries at a time, and the queries still outstanding at DROP are abandoned rather than run. What stays in the functional test reads only its own tables. The reclamation-after-work assertion is new. Every previous arm inspected a table that had never run a query, so raising max_free_threads left all of them green while workers persisted after real work completed.
…uling refusal Every query submitted to a QueryRunner table is now scheduled on a ThreadPool, and ThreadPool::scheduleImpl consults the thread-allocation fault injector that stress runs enable, so the pool can refuse a query that the engine would otherwise have run. Such a query is accounted for without being executed, which makes an exact effect count in a functional test dependent on the injector rather than on the engine. The two functional arms that asserted exact effect counts now assert an exact upper bound and a loose lower one, matching the shape the capacity arm already used, and the arm covering a failing query submits two follow-up queries so that its lower bound survives a single refusal. The exact counts move to tests/integration/test_query_runner, which runs on a private server with the injector disabled: test_failing_query_leaves_table_usable pins the post-failure count there.
Arms 1 and 3 assert count() > 0, which is falsified only when *every* effectful submission in the arm is refused. A submission can be refused by thread-allocation fault injection (stress CI installs it at 1e-4) or by global thread pool saturation, and a refused query is accounted for without failing the INSERT, so a fully-refused arm legitimately yields 0. Both arms submitted two queries, so a single pair of refusals falsified the lower bound: measured 1/40 at an injection probability of 0.02. Four submissions per arm survive three refusals; the same sweep now measures 0/40, while the pre-reshape exact counts still fail 4/40, so the sweep still discriminates. The upper bounds move from 2 to 4 accordingly. The reference file is unchanged: every assertion still yields 1. No source change.
…lsify them The two liveness arms of 04892 asserted count() <= 4 AND count() > 0. The upper bound is the engine's exactly-once promise and is refusal-proof: a refused query never runs, so refusal can only lower the count. The lower bound is not. The thread pool refuses a submission both when thread allocation is being fault injected and when the whole server is saturated, and the saturation source is not probability-bounded and refuses consecutive submissions, so under sustained saturation every submission in an arm is refused whatever the sample size and count() > 0 is falsified. Measured directly against every reachable post-refusal state: at count() = 0 the old form fails and the new one passes, and count() = 0 was the most common single outcome (8 of 22 readable iterations) when the arm's shape was run against a server injecting refusals at p = 0.1. count() <= 4 still falsifies double execution, the other half of the exactly-once property, so nothing is lost: it returns 0 at counts 5 and 8. Each arm keeps two blocking oracles that a lost query falsifies with a hang rather than a count, both of which a bound cannot express: a synchronous INSERT waits on a CountDownLatch that a job disposed of without its completion never counts down, and SYSTEM WAIT QUERY RUNNER polls until every issued seq retires. The exact counts are pinned in tests/integration/test_query_runner, which never installs the fault injector, by test_dispatching, test_threads_released_after_work and test_failing_query_leaves_table_usable. Arm 2 keeps its lower bound: 32 submissions against a capacity of 2 would need all 32 refused. The comments are updated so the file no longer claims an oracle it does not have. The reference file is unchanged.
…w comment The capacity arm of 04892 also required at least one query to have run. That lower bound is not sound: a submission is refused whenever starting its worker thread fails, which includes the whole server's thread pool being saturated, and every submission in that arm needs a new thread because max_free_threads leaves no parked worker to reuse. Saturation is a sustained state rather than an independent per-submission draw, so all 32 submissions can legitimately be refused together and submitting more buys nothing. Measured against a server with thread allocation fault injected at p=0.1, an empty table was the single most common outcome, 15 of 40 runs, and the lower bound failed on every one of them. The arm keeps the upper bound, which is the engine's promise, and keeps asserting that every refused query is accounted for: a lost submission hangs the synchronous INSERT and SYSTEM WAIT QUERY RUNNER forever. test_capacity_bound in tests/integration/test_query_runner pins the accepted count exactly, on a server no other test shares. Also correct the comment above saturatingAdd. It named both operands as unbounded, but threads is validated to [1, 1024]. The overflow is still reachable through max_queue_size, which is validated only as at least 1, so the guard stays. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (click to expand)Reviewed cold against the code on disk, then adjudicated with the recorded validation evidence.
|
|
Workflow [PR], commit [4deab47] AI ReviewSummaryThis PR replaces Final Verdict✅ No material findings. |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260812-041900 |
|
cc @mstetsyuk @azat, could you review this? A QueryRunner table started its |
|
Much simpler: #114522. |
Closes: #114367
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a
QueryRunnertable occupyingthreadsglobal thread pool slots for its whole lifetime, even while completely idle. A few tables created with a largethreadssetting could exhaust the server's thread pool, after which every query failed withCANNOT_SCHEDULE_TASKuntil the tables were dropped. Queries are now scheduled on demand, so an idle table holds no threads. Closes #114367.Description
CREATE TABLE ... ENGINE = QueryRunner SETTINGS threads = Nstarted N worker threads immediately and kept them blocked on an empty queue until the table was dropped. AThreadPoolthread is itself a job on the global pool, so those workers held N global slots permanently, andthreadsis settable per table up to 1024 with no server-wide budget.Measured on a table that received no inserts:
GlobalThreadgoes from 2 to 1027 right afterCREATE ... threads = 1024. Twelve such idle tables exceed the 10000-slot pool, the next ordinary query fails withCode: 439 ... no free thread (threads=10000, jobs=10000) (CANNOT_SCHEDULE_TASK), and the server does not recover until they are dropped. After this change the same script leavesQueryRunnerThreadsat 0.The dispatcher reimplemented a concurrency cap, a bounded queue and on-demand worker startup on top of
ThreadPool, and that reimplementation is what broke thread reclamation. This deletes the private queue and the blocked-worker layer and submits each query to the pool directly, so the pool supplies all three.threadskeeps its documented meaning and the capacity bound staysthreads + max_queue_size. No new or changed settings.In CI this surfaced as
AST fuzzer (amd_debug, targeted, old_compatibility)failing with a bareReceived signal 15on any branch whose corpus reached04337_query_runner.sql: report.Thread pool occupancy is server-global, so the idle-table property and the reclamation of threads after real work are asserted in
tests/integration/test_query_runner, which gets a server to itself. A new stateless test covers the reworked disposal paths on its own tables: a query refused at capacity, a query still queued atDROP, and a failing query leaving the table usable.