Skip to content

Fix Crash during Traversal of Execution Graph - #118031

Merged
Michicosun merged 2 commits into
masterfrom
traversal_purge
Sep 5, 2026
Merged

Michicosun merged 2 commits into
masterfrom
traversal_purge

Conversation

@Michicosun

@Michicosun Michicosun commented Sep 4, 2026 •

Copy link
Copy Markdown
Member

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

This patch improves lock lifetime held during graph traversal.

Fixes: #116103


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

Version info

  • Merged into: 26.9.1.773 (included in 26.9 and later)

@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [76d81d5]

Summary: ✅


AI Review

Summary

This PR changes ExecutingGraph::updateNode so UpdatePipeline expansions and ready removals are deferred until the frame-local updated_processors / updated_edges queues are empty, and it reorders removePendingGroup so allocation can still fail before any node or edge is destroyed. The current code looks internally consistent and the two earlier inline concerns are addressed, but the PR still does not provide focused evidence that the claimed cross-thread use-after-free fix and the new allocation-failure path are actually covered, so I cannot sign off yet.

Tests
  • ⚠️ [src/Processors/Executors/Runtime/ExecutingGraph.cpp:502] The behavioral change is the deferred block at the bottom of updateNode, but the PR adds no regression that parks a second executor frame across this gap, and no fault-injection coverage for the throw-before-cleanup path the second commit is explicitly about. The existing gtest_update_pipeline cases only cover single-frame fan-in removal, deferred removal of unfinished processors, and cancellation stranding. Please add a targeted ASan/TSan or deterministic gtest for the cross-frame case, or equivalent focused evidence for why this ordering is correct under allocation failure too.
Final Verdict

Status: ⚠️ Needs changes

LLVM Coverage Report

Measured on commit 76d81d5.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.10% 81.10% +0.00%

Changed lines: Changed C/C++ lines covered: 58/59 (98.31%) · Uncovered code

Full report · Diff report

@Michicosun Michicosun changed the title Traversal purge Execution Graph Traversal Sep 4, 2026
@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 76d81d547 with master da918ab89 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 35a0d6f0c; 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

18 translation units recompiled, 51 s compile time in total, 14 of them have a recent master baseline.

Translation units without a recent master baseline:

  • contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/client/chttp2_connector.cc: 6.3 s

Job report

@Michicosun Michicosun changed the title Execution Graph Traversal Fix Crash during Traversal of Execution Graph Sep 4, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 4, 2026
read_lock.lock();
}

if (updated_processors.empty() && updated_edges.empty())

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.

This bottom-of-loop defer is the whole fix for the new lifetime contract, but the PR still ships without a focused regression that proves it. The existing gtest_update_pipeline cases cover single-frame fan-in removal, deferred removal of unfinished processors, and cancellation stranding; they do not park a second executor frame across this gap, and they do not exercise the allocation-failure path the second commit is explicitly about. Please add a targeted ASan/TSan or deterministic repro here, or equivalent focused evidence for why both the cross-frame case and the throw-before-cleanup path stay covered by this rewrite.

@yariks5s yariks5s self-assigned this Sep 4, 2026

@yariks5s yariks5s left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, though we can add a regression test (optional)

@Michicosun

Copy link
Copy Markdown
Member Author

Don't know how to write a non-hacky test for this, but the fix is straightforward - we do not release the lock while we have pointers to shared state.

@Michicosun
Michicosun added this pull request to the merge queue Sep 5, 2026
Merged via the queue into master with commit 7cf9942 Sep 5, 2026
174 checks passed
@Michicosun
Michicosun deleted the traversal_purge branch September 5, 2026 12:07
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request Sep 12, 2026
IProcessor::work() runs with no graph lock held: PipelineExecutor::executeStepImpl
calls context.executeTask() at PipelineExecutor.cpp:356 and only enters
graph->updateNode at :379. That is why IProcessor.h:189-194 states that work()
cannot access any ports.

Two processors broke that contract. ScatterByPartitionTransform::work() pushed to
its output ports, and OutputPort::pushData -> Port::updateVersion ->
Port::UpdateInfo::update appends to the node's own post_updated_output_ports,
which ExecutingGraph::addEdges wired as that edge's update list. A peer worker
reclaiming a retired processor holds the nodes_mutex write lock and sweeps every
node (removePendingGroup -> removeAffectedEdges), std::erase_if-ing the same
vector. The AST fuzzer reported the resulting data race on amd_tsan; besides the
sanitizer report, a concurrent push_back and erase_if on one std::vector<void *>
can drop or duplicate an edge id or leave a freed Edge * behind. The scatter is
reachable from ordinary SQL: SortingStep::scatterByPartitionIfNeeded inserts it
for any window-function sort with a non-empty PARTITION BY, for hash-sharded
merge joins and for distributed shuffle.

CreatingSetsTransform::work() closed its input port, the same violation on the
set-building path for IN (subquery) and external tables.

Both now do their port work in prepare(), which runs under the graph lock, so
ExecutingGraph needs no change: with the contract honoured, every writer of
post_updated_*_ports runs under nodes_mutex and its write lock is exclusive as
designed. This follows CopyTransform, the contract-conforming fan-out shape.

The scatter keeps the hashing and IColumn::scatter work in work() and hands the
chunks over from prepare(); a single has_output_chunks flag replaces the former
has_data (write-only) and all_outputs_processed pair. prepare() cannot observe a
pulled-but-unscattered chunk, because it only returns Ready right after pulling
and the owning thread runs work() before its next prepare(). Returning PortFull
while chunks are pending preserves the ordering that
optimizeJoinByShards.cpp:549-558 documents as deadlock-relevant: no new input
chunk is consumed until every partition chunk of the previous one is handed over.

CreatingSetsTransform now closes its input one prepare() later on the path that
stops consuming early (the set came from the cache), so the subquery upstream may
produce at most one more chunk, which is discarded when the port closes. That
window already existed between prepare() returning Ready and work() running.
The close cannot be dropped altogether: generate() always returns {}, so
finished_generate is set in the same work() call and the base class returns
through its finished_generate branch, which does not close the input.

An exhaustive scan of the other methods that run outside nodes_mutex found no
further violation: every other void work() body in src/, all 21 onCancel()
overrides, both onAsyncJobReady() overrides and schedule()/scheduleForEvent().

Tests: a TSAN gtest that runs a scatter next to pipeline-update removal churn on
16 threads and reproduces the reported race before the fix; a round-robin case
asserting that a finished output does not wedge the pipeline; and a hash-mode
case asserting that prepare() reports PortFull, rather than consuming another
input chunk, while a scattered chunk is still pending.

Related: ClickHouse#118031
Related: ClickHouse#118393

Co-Authored-By: Claude Opus 5 <[email protected]>
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-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.

3 participants