Skip to content

Fix a hang of a query that lazily loads a dictionary in a CPU workload - #130133

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:cpu-lease-park-dictionary-load-wait
Nov 8, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:cpu-lease-park-dictionary-load-wait

Conversation

@groeneai

@groeneai groeneai commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #88304
Related: #117711

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

Fixed a hang of a query in a CPU workload that is the first to use a dictionary with a ClickHouse source (dictGet, dictHas with a non-constant key) and runs no more threads than the workload's CPU slots (for example max_concurrent_threads = 1 with max_threads = 1). The waiting query kept its CPU slot, which the dictionary load in the same workload needed; now it releases the slot while it waits.

Description

On 26.10.2.103, with CREATE RESOURCE cpu (MASTER THREAD, WORKER THREAD), a workload one with max_concurrent_threads = 1 and a HASHED dictionary over an aggregating view, SELECT count() FROM numbers(1) WHERE dictHas('d', number + 1) SETTINGS workload = 'one', max_threads = 1 never finishes (6/6; 5/7 with a plain table source, every time on a debug build). Found while working on #88304.

A lazy load runs on an ExternalLoader thread in the caller's thread group, so the source query's executor takes a CPU lease in the caller's workload. The caller's thread waits in ExternalLoader::LoadingDispatcher::loadImpl and keeps its slot; the source executor is preempted after one quantum and waits for that slot forever.

Now loadImpl parks the waiting thread's CPU lease (CPULeaseParkGuard from #117711) for the wait, in both overloads, which also covers a dictionary whose source query reads another dictionary. A NO_WAIT probe and an already loaded object do not park.

Not covered: when the query runs more threads than the workload's CPU slots, parking all of them still frees no slot; #126512 addresses that separately, and this PR does not depend on it; cpu_slot_parking = 0 and separate MASTER / WORKER THREAD resources are unchanged, as in #117711.

Tests: a stateless test. It fails on the master release build of the same commit (6/6 TIMEOUT_EXCEEDED) and on a debug build, and passes 50/50 with random settings and in the ParallelReplicas, DBReplicated, distributed plan, s3 storage and AsyncInsert configurations. The aggregating view makes the source query spend several CPU quanta before its first block, which is what makes the hang deterministic on release builds.


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

Version info

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

groeneai and others added 2 commits November 8, 2026 12:10
A lazy dictionary load started by a query runs on an ExternalLoader thread
that joins a child of the caller's thread group, so the source query's
synchronous executor allocates its CPU lease in the caller's workload. The
caller's executor thread meanwhile waits in
ExternalLoader::LoadingDispatcher::loadImpl and kept its CPU slot. In a
workload with max_concurrent_threads = 1 the source executor is preempted
after its first quantum and waits for that slot forever, so the query hangs
until max_execution_time or KILL QUERY.

Both loadImpl overloads now wait through one helper that parks the waiting
thread's CPU lease with CPULeaseParkGuard (ClickHouse#117711) for the duration of the
wait. Parking may block until the scheduler thread processes a pending
request, so the loader mutex is released around it; unparking never blocks
and runs under the mutex, so the predicate result and `info` are read under
the lock as before. A NO_WAIT probe and an already loaded object do not park.

The regression test uses an aggregating view as the dictionary source: the
hang needs the source executor's first pull to use at least one quantum,
which a plain table source reaches only sometimes on release builds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fold the waitForLoading doc into one line and drop the NO_WAIT comment.

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

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review (3 nits, none in the code path).

# Sev Finding Verdict Evidence / action
1 💡 The changelog entry implied every query in a one-slot workload is fixed; a query that runs more threads than the workload's slots still frees no slot when parked AGREE Entry now states the condition; the wider case is the "Not covered" sentence (#126512)
2 💡 The test's pinned settings carry no one-line reason AGREE Noted, not blocking
3 💡 CacheDictionaryUpdateQueue::waitForCurrentUpdateFinish also waits holding the slot DISAGREE Separate code; its update threads do not join the caller's thread group and the wait has query_wait_timeout_milliseconds; not raised

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-self-slot-58:20261108-121300

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: 05569_cpu_lease_park_dictionary_load fails 6/6 on the master release CI build of the same commit and 4/4 on debug (TIMEOUT_EXCEEDED at 15 s).
b Root cause explained? The caller's executor thread waits in ExternalLoader::LoadingDispatcher::loadImpl holding its CPU slot; the load's source query runs in the caller's thread group, so in the same workload, and is preempted after one quantum waiting for that slot.
c Fix matches root cause? Yes: the wait parks the lease (CPULeaseParkGuard, #117711), in the generic loader layer; no timeout, setting or guard.
d Test intent preserved / new tests added? New SQL test; its source is an aggregating view so the source query uses several quanta before its first block, which makes the hang deterministic on release builds.
e Both directions demonstrated? Yes: fails on base (release and debug), passes 50/50 with random settings on the fix; the fix with cpu_slot_parking = 0 hangs again.
f Fix is general across code paths? Both loadImpl overloads share the helper; a dictionary whose source reads another dictionary is covered. Not covered: max_threads above the slots (#126512), cpu_slot_parking = 0.
g Fix generalizes across inputs (params/datatypes/wrappers)? HASHED, FLAT, SPARSE_HASHED, HASHED_ARRAY (Nullable attribute), COMPLEX_KEY_HASHED (String key), RANGE_HASHED with dictGet/dictHas/dictGetOrDefault: all hang on base, all pass on the fix; max_threads 1, 8, default.
h Backward compatible? (maintainer-approved exception only) Yes: no setting, format or protocol change; the guard is a no-op for a thread without a CPU lease.
i Invariants and contracts preserved? pred is evaluated under the loader mutex before and after the park, so no wakeup is lost and the result is read under the lock; the mutex is released for park (may wait for the scheduler) and held for unpark (never waits); NO_WAIT stays non-blocking.

Session id: cron:clickhouse-impl-self-slot-57:20261108-110800

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

cc @serxa @nihalzp, could you review this and add the can be tested label so CI runs? A query that lazily loads a dictionary waits in ExternalLoader::LoadingDispatcher::loadImpl holding its workload CPU slot, while the load's source query runs in the same thread group and so waits for that slot; the wait now parks the CPU lease with CPULeaseParkGuard from #117711.

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [db02c66]

Summary: ✅


AI Review

Summary

Reviewed PR #130133 at db02c66e0f7fa5c9c98949782df7a793d4d8dfdb. The shared ExternalLoader wait now parks the caller's CPU lease, allowing a dictionary source query in the same workload to make progress. No actionable findings.

The new 05569_cpu_lease_park_dictionary_load regression passed in the current head's Fast test. The PR also reports base failures on release and debug builds and 50 successful runs with the fix; those repetitions were not independently rerun during this review.

Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... labels Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing db02c66e0 with master 7b5f2bf32 (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

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

Job report

@serxa serxa self-assigned this Nov 8, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 8, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

I found the fix good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 8, 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-scheduler Workload scheduler and resource management: CPU/IO/memory scheduling, concurrency control, worklo... pr-bugfix Pull request with bugfix, not backported by default 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.

4 participants