Skip to content

Fix deadlock in ProcessList::killAllQueries with distributed query plans - #128832

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix-kill-all-queries-deadlock
Nov 11, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix-kill-all-queries-deadlock

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

ProcessList::killAllQueries marked all queries with is_cancelling first, then cancelled them one by one, and unmarked them only at the end. Since #120949 each local task of a distributed query plan has its own process list entry, and cancelling the query joins the task threads (ReadFromDistributedPlanSource::onCancel -> DistributedQueryPlanExecutorLocal::cleanup -> joinAllThreads). A task thread that finishes destroys its ProcessListEntry, whose destructor waits while the entry is marked; the entry stays marked until all cancellations are done, so the cancellation never finishes. The server hung on shutdown forever (and OOMCanary, which also calls killAllQueries, would hang too).

Now killAllQueries marks, cancels and unmarks the queries one at a time, as killQuery and sendCancelToPostgreSQLQuery do, and skips the queries that finished since the list was taken.

Found by La Casa Del Dolor (amd_debug), where the server did not shut down: the main thread was in ProcessList::killAllQueries -> DistributedQueryPlanExecutorLocal::joinAllThreads, and the DistQueryTask threads in ProcessListEntry::~ProcessListEntry.
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c081bf46d4ef8e43e4916890b638c3139f538720&name_0=MasterCI&name_1=La%20Casa%20Del%20Dolor%20%28amd_debug%29

Checked locally: a server running SELECT sum(a.x + b.x) FROM big AS a JOIN big AS b ON a.y % 10 = b.y % 10 SETTINGS make_distributed_plan = 1, distributed_plan_execute_locally = 1 (8 process list entries) still ran 90 s after SIGTERM without the fix (shutdown_wait_unfinished = 5); with the fix it cancelled the query and exited normally in 5 s. Added an integration test to test_shutdown_wait_unfinished_queries that shuts the server down while such a query runs and checks that it stops without being force-killed; it fails without the fix and passes with it.

Related: #120949
Related: #128774

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 the server hanging forever on shutdown while a query with make_distributed_plan = 1 is running.

🤖 Generated with Claude Code


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

Version info

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

…plans

`killAllQueries` marked all queries with `is_cancelling` first, then cancelled
them one by one, and unmarked them only at the end. Cancelling a query with a
distributed query plan joins the threads of its local tasks, and each of them
has its own process list entry, whose destructor waits while the entry is
marked. So the cancellation never finished: on shutdown (and in `OOMCanary`)
the server hung forever. Mark, cancel and unmark the queries one at a time, as
`killQuery` does.

Found by `La Casa Del Dolor (amd_debug)` (the server did not shut down):
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c081bf46d4ef8e43e4916890b638c3139f538720&name_0=MasterCI&name_1=La%20Casa%20Del%20Dolor%20%28amd_debug%29

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

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fa6fd89]

Summary: ✅


AI Review

Summary

ProcessList::killAllQueries now marks, cancels and unmarks queries individually, allowing local distributed task entries to finish destruction during cancellation. No actionable findings remain. The earlier regression-coverage finding is addressed: test_shutdown_kill_distributed_plan_query passes with the fix, and the baseline job log confirms failure when shutdown requires a force kill. Both the test and cancellation implementation match the current head.

Missing context / blind spots
  • ⚠️ CI for current head fa6fd894 is still running; integration results remain pending. Completed integration jobs will close this validation gap. No local build or test rerun was performed.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 3, 2026
Comment thread src/Interpreters/ProcessList.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fa6fd8944 with master 1dc6f8d2b (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.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.69, programs/clickhouse-keeper ×0.70 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.9 s 9.5 s +6.8 s (+252%)
programs/clickhouse-keeper _GLOBAL__sub_I_TargetLibraryInfo.cpp 4.8 s 9.0 s +5.6 s (+166%)
Compile time of recompiled translation units

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

Job report

alexey-milovidov and others added 2 commits November 4, 2026 09:27
…an executed locally

`killAllQueries` on shutdown hung on such a query: its local tasks have their own process list
entries, and cancelling the query joins their threads. The test starts the query, waits until the
local tasks read, shuts the server down and checks that it stops gracefully. Without the fix the
helper has to kill the server after `shutdown_wait_unfinished`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/Interpreters/ProcessList.cpp
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master and resolved the include conflict in src/Interpreters/ProcessList.cpp (kept both <Common/typeid_cast.h> from master and <algorithm> from this PR).

CI failures on the previous heads are unrelated to this change, which only touches ProcessList::killAllQueries (used at shutdown and by SYSTEM kill paths, not on any query path):

@groeneai, investigate the join_nullable_keys_small_table perf instability: link above, and provide a fix in a separate PR. If the fix is already in progress, link it here.

@groeneai

groeneai commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

The fix is in progress: #125542.

Cause: #4 (and #8, the same query with parallel_hash) is a LEFT JOIN where almost no left row matches, so JoinCommon::filterWithBlanks fills the right-side Nullable key with defaults one row at a time (ColumnNullable::insertDefault plus the nested insertDefault, two virtual calls per row). That loop is about half of the query's CPU, and its speed depends on the code placement of the particular build. In the 1817a5b run on arm, the fill frames account for 575 of the 735 extra CPU samples (ColumnNullable::insertDefault: 823 -> 1473).

Breadth (CIDB query_metrics_v2 and perf_flamegraph_stacks_v1, arm, last 30 days): #4 moved by more than 10% in 36 of about 9,900 runs. 12 of them are runs of two PRs that do change this path (both faster). The other 24 are 17 unrelated PRs and 6 master-vs-master runs, in both directions, because the reference binary is sometimes the slow one: master 4575290 is +27.7% as the candidate (report) and -20.5% as the reference of the next master run (report). In all 24 the fill frames move in the same direction as the query time.

#125542 fills runs of all-blank 64-row blocks with one insertManyDefaults call (and adds ColumnNullable::insertManyDefaults). In its arm Performance Comparison (report) the fill frames drop from 1321 CPU samples to 1, #4 goes from 0.508 s to 0.256 s and #8 from 0.606 s to 0.339 s, so the loop whose speed flips no longer runs for these queries. The PR is mergeable with current master and is waiting for review.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master again (no conflicts; src/Interpreters/ProcessList.cpp still compiles) to re-run CI. The only red job on 576d786 was Build profile diff, which failed on CI infrastructure (LogClusterNotReady: the endpoint never became ready while querying the log cluster), not on this change.

@groeneai, thanks for the analysis of join_nullable_keys_small_table #4 — tracking the fix in #125542; nothing to change in this PR for it.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only red job on 5af3c16 is Build profile diff again, and it failed on CI infrastructure: LogClusterNotReady: the endpoint never became ready while it queried the CI logs cluster. That has nothing to do with this change. The fix for classifying this outage is in progress in #129675 and has not landed in master yet, so merging master again would not clear it. All other checks pass, and there are no unresolved review threads.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (no conflicts; src/Interpreters/ProcessList.cpp still compiles) to re-run CI. The only red job on 5af3c16 was Build profile diff. It failed on CI infrastructure (LogClusterNotReady: the endpoint never became ready) and is unrelated to this change. #129675 is tracking it. There are no unresolved review threads.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Amazing!

@alexey-milovidov alexey-milovidov self-assigned this Nov 11, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 11, 2026
Merged via the queue into master with commit c44be8a Nov 11, 2026
188 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-kill-all-queries-deadlock branch November 11, 2026 12:57
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. 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.

3 participants