Skip to content

Give back fully consumed CPU slots when the last running thread is preempted - #123640

Merged
alexey-milovidov merged 2 commits into
masterfrom
cpu-lease-give-back-on-full-preemption
Oct 3, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
cpu-lease-give-back-on-full-preemption

Conversation

@serxa

@serxa serxa commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

With cpu_slot_preemption enabled, CPULeaseAllocation::consume finishes at most one resource request per report. A report that covers several quanta leaves the other requests fully consumed while they still hold their slots. If the last running thread of the query is then preempted, no thread reports consumption until a grant resumes one, so the query keeps these slots while it waits. If the grant needs one of them, the query never resumes.

One way to hit it: only worker threads compete for slots (CREATE RESOURCE cpu (WORKER THREAD)), so the master request is free and finishing it returns no slot. With a workload's max_concurrent_threads slots held this way, the preempted master thread waits forever. It has no preemption timeout.

The fix: when a preemption leaves no running thread, renew finishes all requests in consumption and enqueues the next one if needed. Preemption with no running thread implies consumed_ns >= requested_ns, so this returns only slots that were already used (checked by a chassert). The one-request-per-report limit in consume still applies while threads are running, so the pipeline does not stall.

The new unit test SchedulerCPULeaseAllocation.FullPreemptionReturnsConsumedSlots reproduces the deadlock with one query, max_concurrent_threads = 1 and a free master slot. Without the fix the master thread never resumes.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a possible hang of a query under workload CPU scheduling with cpu_slot_preemption enabled: when all its threads were preempted, the query could keep CPU slots it had already used and wait forever for one of them.


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

Version info

  • Merged into: 26.10.1.1431-master (included in 26.10 and later)
  • Backported to: 26.9.12.3, 26.8.19.5, 26.7.23.10

…eempted

`CPULeaseAllocation::consume` finishes at most one request per report. When a
report covers several quanta, the remaining requests are fully consumed but
still hold their slots. If the last running thread is then preempted, nothing
reports consumption until a grant resumes a thread, so these slots stay held
while the query waits. When the grant needs one of them, the query waits for
itself forever. This happens, for example, when only worker threads compete
for slots (`CREATE RESOURCE cpu (WORKER THREAD)`): finishing the free master
request returns no slot.

Now, when no thread is left running after a preemption, all requests in
consumption are finished. Preemption with no running thread implies
`consumed_ns >= requested_ns`, so this returns only slots that were already
used.

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

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3c69c08]

Summary: ✅


AI Review

Summary

This PR fixes a real deadlock in CPULeaseAllocation: when the last running thread is preempted after one renew report covers multiple quanta, fully consumed requests could keep their slots forever because no running thread remained to finish them. The new finishConsumedRequests path is placed on the exact transition where reporting stops, preserves the one-request-per-report behavior while threads are still running, and the added unit test exercises the worker-only-slot scenario described in the PR. I did not find a remaining correctness, concurrency, or metadata issue in the current diff.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 3c69c08.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.80% 92.10% +0.30%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 17/19 (89.47%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 3, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... label Oct 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3c69c0863 with master bd202925d (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

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

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

Job report

@serxa
serxa marked this pull request as ready for review October 3, 2026 10:34
@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into master with commit cd175a6 Oct 3, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the cpu-lease-give-back-on-full-preemption branch October 3, 2026 19:31
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 5, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 5, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 5, 2026
Backport #123640 to 26.9: Give back fully consumed CPU slots when the last running thread is preempted
clickhouse-gh Bot added a commit that referenced this pull request Oct 5, 2026
Backport #123640 to 26.8: Give back fully consumed CPU slots when the last running thread is preempted
clickhouse-gh Bot added a commit that referenced this pull request Oct 6, 2026
Backport #123640 to 26.7: Give back fully consumed CPU slots when the last running thread is preempted
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 6, 2026
…s when the last running thread is preempted
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
…s when the last running thread is preempted
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Oct 9, 2026
…s when the last running thread is preempted
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 17, 2026
`effectiveMaxSlots()` is `current_max_slots - parked_count`, and
`current_max_slots` is the executor's desired thread count. When the
workload grants a query fewer slots than it wants (`max_concurrent_threads`
below `max_threads`), parking all of its threads leaves `allocated` equal to
`effectiveMaxSlots()`, so `parkLease` keeps every slot. With no running
thread nothing reports consumption or spawns a thread, so the slots stay
idle. A `remote()` query whose shard runs on the same server under the same
workload then hangs: the coordinator waits for the shard, and the shard's
thread waits in `renew` for a slot the coordinator holds.

Make `effectiveMaxSlots()` zero while no thread is running or preempted and
at least one is parked, so the last park gives back every held slot. A
worker that times out in preemption while the others are parked enters the
same state with a request still pending; `grantImpl` now returns a grant
that arrives then (the surplus loop of `parkLease` is moved to
`finishSurplusRequests` and called from both). This is the parking
counterpart of ClickHouse#123640.

Tests: a stateless test with a one-slot workload and a loopback `remote()`
GROUP BY (times out at 60 s without the change), and a unit test for the
late grant after a preemption timeout.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.4-must-backport v26.6-must-backport v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants