Skip to content

Spawn executor threads for CPU slots granted after query start - #125394

Closed
scanhex12 wants to merge 5 commits into
masterfrom
concurrency-control-spawn-on-granted-slots
Closed

scanhex12 wants to merge 5 commits into
masterfrom
concurrency-control-spawn-on-granted-slots

Conversation

@scanhex12

@scanhex12 scanhex12 commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

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):

A query that started while CPU slots were busy now grows its number of threads when other queries release their slots. Previously such slots stayed granted to the query but unused until it finished, and other queries could not use them either.


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

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8f074a8]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, 2/4) FAIL
test_read_temporary_tables_on_failure/test.py::test_different_versions FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR makes running pipelines acquire CPU slots granted after startup and restores retries after lease downscaling. Block merge: the latest retirement synchronization introduces a query deadlock. Focused regression coverage is also missing.

Missing context / blind spots
  • ⚠️ No runtime reproduction was executed. The deadlock is established by tracing the PR-head executor, lease allocator, and bounded thread-pool scheduling; a focused concurrent run should validate its fix.
Findings

❌ Blockers

  • PipelineExecutor.cpp:570–571: Worker retirement can deadlock against replacement scheduling. With max_threads = 2, worker 1 times out in CPULeaseAllocation::renew, releasing its lease ID. Before that worker exits, worker 0 can handle positive spawn_count, acquire spawn_mutex, and schedule a replacement. ThreadPool counts running and queued jobs against its capacity of two, so scheduling waits for an existing job to finish. Worker 1 now needs the same mutex before returning, creating a permanent cycle. Query cancellation does not release these waits. Keep retirement independent of a mutex held across blocking pool scheduling while preserving retry-state ordering. Inline finding.

⚠️ Majors

  • PipelineExecutor.cpp:538–545: [dismissed by author -- https://github.com/Spawn executor threads for CPU slots granted after query start #125394#discussion_r4247784493] Late-grant recovery still lacks regression coverage. The thread was silently resolved, but the PR still changes only two executor files; the existing lazy-allocation tests verify low initial allocation, not growth after contention ends, so they cannot detect loss of this behavior.
Tests
  • ⚠️ Add a focused regression showing that an already-running query increases its workers after a competing query releases slots, including recovery after lease downscaling without fresh positive spawn_count.
  • ⚠️ Cover replacement while a retiring worker still occupies a full thread pool, demonstrating that worker retirement and query cancellation complete.
Final Verdict

Status: ❌ Block. Eliminate the retirement/scheduling deadlock and add focused regression coverage for late grants and worker replacement.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 12, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... label Oct 12, 2026
@scanhex12 scanhex12 added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Oct 12, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8f074a876 with master 7f11488e0 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 4f752f7e6; 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

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

Job report

@scanhex12
scanhex12 requested a review from Michicosun October 13, 2026 07:34
…rows

`CPUSlotsAllocation::tryAcquire` throws `RESOURCE_ACCESS_DENIED` when an
outstanding CPU request fails, for example after its workload is dropped.
The call was outside the `try`, so the graph was not cancelled and processors
blocked in `work` did not get the cancellation signal.

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

@serxa serxa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try to address this issue from a different PoV - prevent it from happening in the first place in pushTasks. It might have some performance implications, so let's keep this PR open for now, while I'm experimenting.

@alexey-milovidov alexey-milovidov self-assigned this Nov 1, 2026
When a worker thread fails to renew its CPU lease and downscales, the query
is no longer at full capacity, but `slots_outstanding` could stay `false`
after `spawnThreads` had reached full capacity earlier. Slots granted later
(e.g. when a competing query finishes) would then stay unused if no new
demand came from `pushTasks`. Set `slots_outstanding` after `downscale`,
under `spawn_mutex` so a concurrent `spawnThreads` cannot overwrite it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment on lines +570 to +571
std::lock_guard lock(spawn_mutex);
slots_outstanding.store(true, std::memory_order_relaxed);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Do not block retiring workers on the spawning mutex

This introduces a deadlock when a timed-out worker is replaced before its pool job exits. For example, with max_threads = 2, worker 1 times out in CPULeaseAllocation::renew, which releases its lease ID before returning false. Worker 0 can then handle a positive spawn_count, take spawn_mutex, acquire the replacement lease, and call spawnThreads. The pool created in initializeExecution limits running plus queued jobs to num_threads, so scheduleOrThrowOnError waits for one of the two existing jobs to finish. Worker 1 now waits here for the same spawn_mutex before it can return: worker 0 waits for worker 1 to exit, and worker 1 waits for worker 0 to unlock. Query cancellation does not release either wait.

Please keep worker retirement independent of a mutex held across blocking pool scheduling, while preserving the retry-state ordering (for example, use a generation counter for downscaling or change how replacement jobs are scheduled).

@serxa serxa closed this Nov 5, 2026
@serxa

serxa commented Nov 5, 2026

Copy link
Copy Markdown
Member

#125901 covers the issue

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-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants