Skip to content

Fix query hang in local execution of distributed plans - #111762

Merged
alexey-milovidov merged 9 commits into
masterfrom
davenger/fix-in-memory-exchange-deadlock
Aug 1, 2026
Merged

alexey-milovidov merged 9 commits into
masterfrom
davenger/fix-in-memory-exchange-deadlock

Conversation

@davenger

@davenger davenger commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

With distributed_plan_execute_locally = 1 the initiator pipeline contains a source that reads the result from an in-memory exchange, next to the source that starts the producing tasks. The exchange reader blocked the pipeline thread indefinitely waiting for a chunk. When the pipeline executor scheduled the reader before the source that starts the producers (with max_threads = 1, or more generally when such readers occupy all pipeline threads), the producers never started and the query hung forever. Queries over a Merge table of Distributed tables hit this regularly because one pipeline holds several driver/reader pairs, e.g. 04367_distributed_plan_merge_scatter_multishard hangs this way with max_threads = 1.

The reader now waits with a short timeout and, when no data arrived yet, pushes a chunk with no rows instead of blocking. Pushing (rather than just yielding) matters: a source that returns no output stays ready and is rescheduled immediately, monopolizing the thread, while pushed output makes the port full and lets the executor run other processors. The end-of-data protocol of the exchange is unchanged.

The new test hangs on the code without the fix in 4 out of 4 runs and passes in about a second (debug build) with it.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Fix a query hang in local execution of distributed query plans (distributed_plan_execute_locally = 1): the in-memory exchange reader could block the only pipeline thread before the sources producing its data were started.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.8.1.566 (included in 26.8 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3ead130]


AI Review

Summary

This PR tackles the local distributed-plan hang by turning in-memory exchange reads into timed polls, filtering synthetic zero-row chunks before they reach the client, and running fuzzed queries under their own thread group. The empty-Data packet leak looks addressed, but the deadlock fix is still incomplete for zero-column exchange streams, and the new stateless reproducer still does not provide deterministic fail-on-master / pass-on-PR evidence.

Findings

❌ Blockers

  • [src/QueryPipeline/DistributedPlanExecutor.cpp:388-390] The timeout branch still fabricates Chunk(getPort().getHeader().cloneEmptyColumns(), 0). For a zero-column stream that becomes Chunk({}, 0), which is empty(), so ISource::work never sets has_input and the source immediately goes back to Ready instead of yielding the pipeline thread. SELECT count()-style local stages can therefore still monopolize the only executor thread and hang in the same way this PR is trying to fix. Suggested fix: use a wake-up marker that is non-empty even for header-less streams (for example a dedicated ChunkInfo tick stripped by a later transform), or make SourceFromInMemoryExchange genuinely async instead of encoding scheduling in data.
Tests
  • ⚠️ [tests/queries/0_stateless/04628_distributed_plan_local_execution_merge_no_deadlock.sql:7] The reproducer is still probabilistic: the bugfix-validation thread for this exact test remains open because both functional validation jobs still marked 04628_distributed_plan_local_execution_merge_no_deadlock as xfail, so the PR still lacks deterministic proof that it fails on master and passes here. Please add the smallest focused test that forces the reader-before-producer schedule, ideally covering the zero-column timeout path that InMemoryExchangeStreamWithoutColumns does not exercise.
Final Verdict

Changes requested: the deadlock fix is not complete for zero-column in-memory exchanges, and the regression evidence is still not deterministic.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 24, 2026
…bugprone-optional-value-conversion`)

Co-Authored-By: Claude Fable 5 <[email protected]>
davenger and others added 2 commits July 24, 2026 14:11
…change path as empty `Data` packets

Co-Authored-By: Claude Fable 5 <[email protected]>
/// output makes the port full and lets other processors run.
auto chunk = exchange->getChunk(std::chrono::milliseconds(10));
if (!chunk)
return Chunk(getPort().getHeader().cloneEmptyColumns(), 0);

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.

Chunk(getPort().getHeader().cloneEmptyColumns(), 0) only creates a real "push" when the exchange header has at least one column. For a zero-column stream this becomes Chunk({}, 0), which is empty(), so ISource::work leaves has_input = false and the source goes straight back to Ready without ever filling the output port.

That matters here because zero-column exchange streams are already a supported case for SELECT count() (NativeCompressedSink/NativeCompressedSource special-case empty headers in src/Processors/Sinks/NativeCompressedSink.cpp and src/Processors/Sources/NativeCompressedSource.cpp). A local distributed-plan count() stage can therefore still monopolize the only pipeline thread and hang in the same way this PR is trying to fix.

This needs a wake-up marker that also works for header-less streams, or an actually async source, before the deadlock is fully fixed.

@clickhouse-gh clickhouse-gh Bot removed the pr-bugfix Pull request with bugfix, not backported by default label Jul 31, 2026
@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 31, 2026
-- With distributed_plan_execute_locally = 1 the initiator pipeline reads the result from an
-- in-memory exchange. If that reader blocked the pipeline thread instead of yielding, the source
-- that starts the producing tasks could never run and the query hung. Whether that happens depends
-- on processor scheduling order, so the queries are repeated several times.

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.

Bugfix validation is showing that this still is not a real reproducer for the old deadlock: both per-arch functional validation jobs mark 04628_distributed_plan_local_execution_merge_no_deadlock as xfail, and the Finish Workflow summary says no architecture produced the expected "fails on master, passes on the PR" result.

Repeating the query a few times is still probabilistic, so we do not actually have regression evidence for the behavior the PR claims to fix. Please make the bad schedule deterministic here, or move the coverage into a lower-level executor test that can force the reader/driver ordering.

@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 36 queries analysed

This PR changes in-memory exchange scheduling for distributed local plan execution (distributed_plan_execute_locally), adds a zero-row-chunk filter, and pins a thread group for AST-fuzzer queries — none of which is exercised by a standard single-node ClickBench SELECT. The only flagged query, clickbench Q15, shows a -11.2% wall-time improvement but essentially identical CPU time (2633 vs 2631 ms), so there is no work-backed cause; the deterministic gates correctly suppressed it as within master's variance band. No real per-query regression or improvement is attributable to this change.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 589612ff-8a08-4073-aae6-4e674a08778c
  • MIRAI run: 66cffa29-6d04-49b9-878c-3e5f2a300563
  • PR check IDs:
    • clickbench_1171252_1785537508
    • clickbench_1171258_1785537508
    • clickbench_1171270_1785537508
    • tpch_adapted_1_official_1171282_1785537508
    • tpch_adapted_1_official_1171291_1785537508
    • tpch_adapted_1_official_1171321_1785537509

@CheSema CheSema self-assigned this Jul 31, 2026
@davenger
davenger added this pull request to the merge queue Jul 31, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 31, 2026
@davenger
davenger added this pull request to the merge queue Jul 31, 2026
@davenger
davenger removed this pull request from the merge queue due to a manual request Jul 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

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

Full report · Diff report

@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Aug 1, 2026
@alexey-milovidov
alexey-milovidov merged commit 1886146 into master Aug 1, 2026
175 of 177 checks passed
@alexey-milovidov
alexey-milovidov deleted the davenger/fix-in-memory-exchange-deadlock branch August 1, 2026 02:27
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 1, 2026
alexey-milovidov added a commit that referenced this pull request Aug 2, 2026
Pulls in the merged fix from #111762 (`Fix query hang in local execution of
distributed plans`), which closed the flaky-test issue #112301 for
`04367_distributed_plan_merge_scatter_multishard` - the only red check on
this branch. The merge is clean and additive: none of this test-only PR's
own files changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release pr-not-for-changelog This PR should not be mentioned in the changelog 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.

Flaky test: 04367_distributed_plan_merge_scatter_multishard

4 participants