Skip to content

Fix deadlock in non-memory-efficient merge of two-level aggregation blocks - #123352

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-merge-blocks-nested-pool-deadlock
Oct 2, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-merge-blocks-nested-pool-deadlock

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Aggregator::mergeBlocks (used by MergingAggregatedTransform, e.g. for distributed queries with distributed_aggregation_memory_efficient = 0) ran its per-bucket merges as max_threads jobs on the aggregator's thread_pool. Merging two large two-level uniqExact states inside these jobs (UniqExactSet::merge with a thread pool) schedules nested jobs on the same pool and waits for them. The pool's queue size equals max_threads, and running jobs count against it, so when all outer jobs reached such a merge at the same time, the nested jobs could never be scheduled: the query hung forever and could not be cancelled (all AggregatorPool threads waiting in ThreadPoolCallbackRunnerLocal::enqueueAndGiveOwnership, TCPHandler waiting in PullingAsyncPipelineExecutor::cancel).

The per-bucket merges now run on a separate pool, so thread_pool stays available for the nested jobs (which do not nest further).

The added test hangs on master every time and passes with the fix.

The issue was found while working on the parallel merge of keyed uniqExact states, which makes the nested scheduling much more frequent; that PR includes the same fix.

Related: #117153

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 possible hang of a distributed GROUP BY query with uniqExact and distributed_aggregation_memory_efficient = 0 when merging large states of two-level aggregation.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.1320-master (included in 26.10 and later)

…locks

`Aggregator::mergeBlocks` (used by `MergingAggregatedTransform`, e.g. for distributed queries with
`distributed_aggregation_memory_efficient = 0`) ran its per-bucket merges as `max_threads` jobs on
the aggregator's `thread_pool`. Merging two large two-level `uniqExact` states inside these jobs
(`UniqExactSet::merge` with a thread pool) schedules nested jobs on the same pool and waits for them.
The pool's queue size equals `max_threads`, and running jobs count against it, so when all outer
jobs reached such a merge at once, the nested jobs could never be scheduled: the query hung
forever and could not be cancelled.

The per-bucket merges now run on a separate pool.

Related: #117153

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

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d720baf]

Summary: ✅


AI Review

Summary

This PR breaks the deadlock between Aggregator::mergeBlocks and nested two-level uniqExact state merges by moving the per-bucket outer merge work onto a separate pool, and the new stateless regression test reproduces the exact distributed two-level GROUP BY hang the description claims. I do not see any unresolved correctness, cancellation, or safety issues in the current diff, and the Bug Fix changelog metadata matches the user-visible impact.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit d720baf.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). labels Oct 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d720baff7 with master 38fd308a8 (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, 79 s compile time in total, 7 of them have a recent master baseline.

Job report

@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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Oct 2, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 2, 2026
Merged via the queue into master with commit 5c55105 Oct 2, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-merge-blocks-nested-pool-deadlock branch October 2, 2026 19:54
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-aggregation GROUP BY / aggregation framework (hash tables, spilling, external aggregation). 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.

2 participants