Skip to content

Report a distributed plan's root cause, not its exchange teardown error - #118417

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-distributed-plan-error-propagation-race
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-distributed-plan-error-propagation-race

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 6, 2026 •

Copy link
Copy Markdown
Member

Related: #118218

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 failing distributed-plan query sometimes reporting Net Exception: Failed to receive <stream> from <host>:<port>, peer closed connection instead of the error that actually failed it. make_distributed_plan is experimental.

Description

04648_distributed_plan_task_error_propagation asserts that a task failure recorded while the initiator is still dispatching a stage reaches the client as CANNOT_SCHEDULE_TASK. It began failing on 2026-09-06 with

Code: 1000. DB::Exception: Net Exception: Failed to receive final_result__0_0 from 127.0.0.1:9223, peer closed connection. (POCO_EXCEPTION)

ReadFromDistributedPlanSource::tryGenerate ran cleanupLocked before rethrowing. That cleanup reaches ExchangeConnections::cleanupQuery, which erases the query's pending connection entries; destroying a FutureConnection drops the last reference to the accepted StreamSocket and closes it. (Its cancel is a no-op once the connection has paired - the close comes from the erase, not from cancel.) The peer of that socket is a streaming exchange source of the same pipeline, parked in tryReadHeader, which then throws POCO_EXCEPTION.

PipelineExecutor rethrows the exception of the first node in graph order that holds one, not the one raised first. So once both the driver and the exchange source hold an exception, which one the client sees is decided by node order, and the teardown's network error can displace the root cause.

The fix rethrows before the cleanup on both paths and leaves the teardown to onCancel. onCancel is reached through PipelineExecutor::cancel, which calls tasks.finish() before graph->cancel(), so no source is scheduled again to record the teardown's error - and a source can only become runnable once that teardown closes its socket.

Measured, with a local instrumented build that forces the CI ordering (a delay either side of the teardown, and the teardown itself behind an env var), logging every graph node that holds an exception:

cleanup before throw (before):   idx=0 ReadFromDistributedPlanSource code=439
                                 idx=7 StreamingExchangeSource(final_result__0_0) code=1000
cleanup deferred to onCancel:    idx=0 ReadFromDistributedPlanSource code=439
                                 (exchange source records nothing)

Three runs each. The competing exception no longer exists, so node order stops mattering. Note that on an uninstrumented local build the failure does not reproduce at all: the driver happens to sit at node 0 and wins the rethrow either way, which is why this needed the forced ordering to observe.

The same runs also failed 04340_distributed_plan_status_reenqueue_fault, which is the second half of the same defect rather than an independent flake. 04648 is a .sql test, so the unexpected error aborted it before its SYSTEM DISABLE FAILPOINT, leaving the server-wide distributed_plan_record_failure_while_starting_tasks armed for the rest of the job. Every later distributed-plan query then failed at task start, so 04340 never reached a status re-enqueue and reported reenqueue fault handled: 0 eight minutes later. That fail point is now ONCE - the test injects exactly one failure - so a single injection cannot leak into unrelated tests. Verified directly: with the fail point armed the query returns CANNOT_SCHEDULE_TASK, and the next query succeeds with no SYSTEM DISABLE FAILPOINT in between.

No new test: 04648 is already the regression test for this, and the change is what makes it deterministic. Both tests pass locally, and 200 consecutive injected-failure runs all report CANNOT_SCHEDULE_TASK.

Onset in CI, Stateless tests (arm_asan_ubsan, azure, sequential), both tests:

window runs failures
2026-08-07 .. 2026-09-05 ~9000 0
2026-09-06, before 00:41 256 0
2026-09-06, after 00:41 84 5

Every failing run reports the same two tests together. The ordering was racy in every version that has it, so this pins the ordering rather than reverting whatever tipped the timing; the linked pull request is the cancellation change that merged at that boundary, though it does not touch this code path.

One trade-off worth a reviewer's attention: deferring the teardown moves cleanup inside ExecutingGraph::cancel, which holds processors_mutex, and running_tasks.cancel waits for workers to reach a terminal state. Every other cancellation path (KILL, LIMIT, another processor throwing) already ran the cleanup from onCancel in exactly that position, so this is not a new hazard - but it is newly on the "the driver itself threw" path.

Closes: #118831
Closes: #118832
Related: #118626

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=f0346e63cbb865a81ad922cdc3c4c8bfbfffdb23&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20sequential%29

Still failing on master at the same rate three days later, and the server log of that run confirms the whole chain in one place - Stateless tests (arm_asan_ubsan, azure, sequential) for 1dd29f8: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=1dd29f83596b117a895338eabb68bcec75042885&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20sequential%29

18:57:20.491178 StreamingExchangeSource: Connecting to 127.0.0.1:9223 ... final_result__0_0
18:57:20.491648 ExchangeConnections: Adding connection for query id 6b6bf2f3-... final_result__0_0
18:57:20.491785 ExchangeConnections: Cleaning up 1 pending exchange connections for query id 6b6bf2f3-...
18:57:20.551924 executeQuery: Code: 1000 ... peer closed connection. (POCO_EXCEPTION)
                              (from StreamingExchangeSource::tryReadHeader)

CANNOT_SCHEDULE_TASK never reaches the client in that run. Eight minutes later, at 19:05:50, all three of 04340's queries fail with Code: 439. DB::Exception: Injected task failure while starting tasks - 04648's leaked fail point - so no status re-enqueue ever happens and 04340 prints reenqueue fault handled: 0. Across the last three days on master, every failing run of that job reports exactly these two tests, and no other test hit the leaked fail point only because no other distributed-plan test ran inside the leak window.

Note that the revert of the linked cancellation change (#118525, merged 2026-09-07) did not help: 23 failures per test on 2026-09-08 against 27 on 2026-09-07.


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

`04648_distributed_plan_task_error_propagation` asserts that a task failure recorded while the
initiator is still dispatching a stage reaches the client as `CANNOT_SCHEDULE_TASK`. It started
failing instead with `Code: 1000. DB::Exception: Net Exception: Failed to receive
final_result__0_0 from 127.0.0.1:9223, peer closed connection. (POCO_EXCEPTION)`.

`ReadFromDistributedPlanSource::tryGenerate` ran `cleanupLocked` before rethrowing. That cleanup
reaches `ExchangeConnections::cleanupQuery`, which cancels the query's pending exchange
connections and so closes the sockets that the streaming exchange sources of the *same* pipeline
are reading. Those sources then throw a bare network error, and `PipelineExecutor` rethrows the
exception of the first node in graph order that holds one - not the one raised first - so the
teardown's error could replace the failure the query should report. Whether the source got that
far before the driver finished unwinding is a race, which is why the test failed only sometimes.

Rethrow before the cleanup on both paths and let `onCancel` do the teardown. `onCancel` is reached
through `PipelineExecutor::cancel`, which finishes the task queue before cancelling the
processors, so no source runs again to record the teardown's error - and a source can only become
runnable once that teardown closes its socket. The ordering now holds by construction.

The same runs also failed `04340_distributed_plan_status_reenqueue_fault`, which is the second
half of the same defect. `04648` is a `.sql` test, so the unexpected error aborted it before its
`SYSTEM DISABLE FAILPOINT`, leaving the server-wide
`distributed_plan_record_failure_while_starting_tasks` armed for the rest of the job. Every later
distributed-plan query then failed at task start, so `04340` never reached a status re-enqueue and
reported `reenqueue fault handled: 0` eight minutes later. Make that fail point `ONCE` - the test
injects exactly one failure - so a single injection can no longer leak into unrelated tests.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=f0346e63cbb865a81ad922cdc3c4c8bfbfffdb23&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20azure%2C%20sequential%29
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [933a279]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL

AI Review

Summary

This PR fixes the distributed-plan failure-ordering bug by deferring ReadFromDistributedPlanSource cleanup until pipeline cancellation, so the query keeps reporting the first recorded failure instead of a later exchange-teardown POCO_EXCEPTION. It also makes distributed_plan_record_failure_while_starting_tasks one-shot so 04648_distributed_plan_task_error_propagation cannot leak its enabled failpoint into later tests. I did not find an actionable correctness or safety issue in the current patch.

Missing context / blind spots
  • ⚠️ Full PR CI was still in progress during review; only the current Praktika summary / build-profile report was available. The remaining test shards would provide the broader distributed-plan coverage for this change.
Final Verdict

✅ No actionable findings at the current head.

LLVM Coverage Report

Measured on commit 933a279.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.70% -0.10%
Branches 81.20% 81.20% +0.00%

Changed lines: Changed C/C++ lines covered: 14/14 (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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Sep 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 933a27919 with master ca3603d1f (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e373496b0; 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

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

Job report

@alexey-milovidov
alexey-milovidov marked this pull request as draft September 6, 2026 16:20
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review September 6, 2026 16:52

@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

@davenger

davenger commented Sep 8, 2026

Copy link
Copy Markdown
Member

@groeneai check this fix #118390 it should cover more cases.
But switching the failtpoint from REGUAL to ONCE still looks valid

@groeneai

groeneai commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

Checked #118390 at d41641c93: it covers this case by a different route. The cleanup stays before the rethrow and the recorded failure becomes authoritative instead (rethrowIfFailed() after cleanupLocked(), plus markExecutionFinished() inside it), so node order in PipelineExecutor no longer decides which error the client sees. It also covers the path this PR does not touch, where the result reader loses its connection after execute() returns, and it reports a lost peer as EXCHANGE_PEER_DISCONNECTED instead of a bare POCO_EXCEPTION. The ReadFromDistributedPlanSource reordering here is redundant once that lands.

The fail point flip is not. ONCE(distributed_plan_record_failure_while_starting_tasks) exists only here; #118390 leaves that entry REGULAR and adds REGULAR(distributed_plan_delay_root_cause_report) right after it. 04648 arms the fail point, runs one query, then disables it, so ONCE is exact for it, and what it closes is measurable: over the last four days on master, 04340 never failed without 04648 failing in the same job (10 jobs with both, 8 with 04648 alone, 0 with 04340 alone).

Where that one line lands is your call. I can push exactly it to #118390 if you want it there; I have not touched that branch since you took it over and will not without your go-ahead. This PR reduced to the one line does the same job.

One related thing in #118390: 05137_distributed_plan_reports_root_cause.sql also arms a REGULAR fail point across two queries, so an unexpected error on the first one aborts the file before SYSTEM DISABLE FAILPOINT and leaves every later failing task sleeping a second for the rest of the job. ONCE is not a drop-in there, since the injection point is reached for consequence failures too and would spend the one-shot arm on one of them. Flagging it, not asking for a change.

@davenger

davenger commented Sep 8, 2026

Copy link
Copy Markdown
Member

The fail point flip is not. ONCE(distributed_plan_record_failure_while_starting_tasks) exists only here; #118390 leaves that entry REGULAR and adds REGULAR(distributed_plan_delay_root_cause_report) right after it. 04648 arms the fail point, runs one query, then disables it, so ONCE is exact for it, and what it closes is measurable: over the last four days on master, 04340 never failed without 04648 failing in the same job (10 jobs with both, 8 with 04648 alone, 0 with 04340 alone).

@groeneai I changed both failpoints to ONCE in #118390

@davenger

davenger commented Sep 9, 2026

Copy link
Copy Markdown
Member

Superseded by #118390

@davenger davenger closed this Sep 9, 2026
@clickhouse-gh clickhouse-gh Bot added comp-distributed-query-exchange Distributed Query Execution with Exchange operators: cloud-native query distribution using stream... and removed comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. labels Nov 22, 2026
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 comp-distributed-query-exchange Distributed Query Execution with Exchange operators: cloud-native query distribution using stream... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: 04340_distributed_plan_status_reenqueue_fault Flaky test: 04648_distributed_plan_task_error_propagation

3 participants