Repository navigation
Fix logical error in ExecutingGraph after a pipeline expansion failed halfway - #121502
Conversation
…ed halfway
A processor that returns `Status::UpdatePipeline` connects its ports to the
new processors before `ExecutingGraph::updatePipeline` records them, and
recording them allocates (`processors->push_back`, `nodes.emplace_back`,
`processors_map.emplace`, the edge lists), so it can throw on the memory
limit halfway. The graph is then inconsistent with the ports: a live node
is connected to a processor that never became a node. The exception cancels
the executor, but another thread that is already inside `updateNode` takes
the `nodes_mutex` write lock next, expands its own node, walks every node in
`addEdges` and hits that connection:
Logical error: 'Processor ConvertingAggregatedToChunksWithMergingSource_64
was found as input for processor ConvertingAggregatedToChunksTransform_63,
but not found in list of processors'
which aborts the server in debug and sanitizer builds. Seen in the stress
test (arm_msan) with `memory_tracker_fault_probability` on a
`GROUP BY GROUPING SETS` query, where several `AggregatingTransform`s expand
the pipeline concurrently:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=99495&sha=8e4aa21ffdd0b8611618c8e742e10b00af776955&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
(PR #99495, which does not touch
the executor).
`updatePipeline` now marks the graph when it throws while recording an
expansion and keeps the new processors alive, since ports of live nodes point
at them; a later expansion sees the mark and returns `Cancelled` instead of
walking the inconsistent graph. The exception itself still propagates and
cancels the query as before.
The failure is injected through the new failpoint
`executing_graph_add_node_fail`, and the gtest drives `ExecutingGraph`
directly, because the window between the two expansions cannot be hit
reliably through the executor. Without the guard the test reproduces the
logical error above.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [948c453] Summary: ✅
AI ReviewSummaryThis PR hardens Final Verdict
LLVM Coverage ReportMeasured on commit 948c453.
Changed lines: Changed C/C++ lines covered: 26/26 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units9 translation units recompiled, 15 s compile time in total, 9 of them have a recent master baseline. |
When recording the processors of a pipeline expansion throws (an allocation fails on the memory limit, see ClickHouse#124906), `ExecutingGraph::updatePipeline` marks the graph with `expansion_failed` and rethrows. Two things then go wrong. 1. The `nodes_mutex` write lock is released by unwinding before the exception reaches `PipelineExecutor::executeStepImpl`, which cancels the executor. A second thread can take the lock in that window, and `updateNode` returns `Cancelled` while the executor is not finished. `executeStepImpl` skipped `pushTasks` on a non-`Done` status, and `pushTasks` is the only thing that replaces the thread's task, so the loop ran `work` again on a processor that `updateNode` had just prepared. For `MergeSortingTransform` in `Serialize` the next `work` dereferences the `merge_sorter` the previous one reset (SIGSEGV in `MergeSorter::read`). Now the thread drops the task when `updateNode` does not return `Done`, so only the graph can schedule it. 2. The processors of the failed expansion were kept only in the graph, but the ports of the pipeline's own processors point at them, and the pipeline is read after the executor is destroyed: the exception callback logs the processors profile (ClickHouse#121503) after `LocalConnection` and `TCPHandler` have destroyed the executor, and `getProcessorsProfileLogInfo` reads the freed peer through the port (heap-use-after-free on every failed expansion under ASan). Now the processors the graph has not recorded yet are moved into the pipeline's shared processors list, so they live as long as the pipeline. The new test runs the external sort with the `executing_graph_add_node_fail` failpoint: the use-after-free reproduces on its first query under ASan, and the race reproduces in 19 of 20 runs of the test on a debug build without the first change. Closes: ClickHouse#125251 Related: ClickHouse#121502 Related: ClickHouse#121503 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #99495
A processor that returns
Status::UpdatePipelineconnects its ports to the new processors beforeExecutingGraph::updatePipelinerecords them, and recording them allocates, so it can throw on the memory limit halfway. The graph is then inconsistent with the ports: a live node is connected to a processor that never became a node. The exception cancels the executor, but another thread that is already insideupdateNodetakes thenodes_mutexwrite lock next, expands its own node, walks every node inaddEdgesand hits that connection withwhich aborts the server in debug and sanitizer builds. Seen in the stress test (arm_msan) with
memory_tracker_fault_probabilityon aGROUP BY GROUPING SETSquery, where severalAggregatingTransforms expand the pipeline concurrently: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=99495&sha=8e4aa21ffdd0b8611618c8e742e10b00af776955&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 (that PR does not touch the executor; the failure appears only once in 21 days of CI history, so it is rare).updatePipelinenow marks the graph when it throws while recording an expansion and keeps the new processors alive, since ports of live nodes point at them; a later expansion sees the mark and returnsCancelledinstead of walking the inconsistent graph. The exception itself still propagates and cancels the query as before.The failure is injected through the new failpoint
executing_graph_add_node_fail, and the gtest drivesExecutingGraphdirectly, because the window between the two expansions cannot be hit reliably through the executor. Without the guard the test reproduces the logical error above (verified with a control build). A server-level check with the failpoint enabled on theGROUPING SETSquery from the report fails cleanly withMEMORY_LIMIT_EXCEEDEDon every run and logs no logical error.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error (
Processor ... was found as input for processor ..., but not found in list of processors) that could abort the server when a pipeline expansion, such as the merge stage of aGROUP BY, failed on the memory limit while another expansion of the same query was in flight.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121502]
Version info
26.10.1.455-master(included in26.10and later)