Repository navigation
Fix a logical error when a processor's updatePipeline throws while connecting the new processors - #121712
Fix a logical error when a processor's updatePipeline throws while connecting the new processors#121712groeneai wants to merge 3 commits into
updatePipeline throws while connecting the new processors#121712Conversation
ExecutingGraph::updatePipeline called the processor's virtual updatePipeline() outside the try whose catch sets expansion_failed. Every production override connects a port of the live cur_node to a brand-new processor inside that virtual and keeps allocating afterwards, so the graph is already inconsistent with the ports before the virtual returns, while the guarded region opened only after it. A throw in that window left expansion_failed false, the guard in updateNode was not armed, and the next thread to take the nodes_mutex write lock walked every node in addEdges, where addEdge failed its processors_map lookup and raised Processor ... was found as output for processor ..., but not found in list of processors replacing the original error (a memory limit, in the reported case). PR ClickHouse#121502 closed the recording half of the same window one day earlier; its boundary was drawn one step too early rather than deliberately, as its own comment says: it starts at "From here on every step that records them". Moving the call inside the existing try covers the connect phase with the same catch. All 11 production overrides of the virtual reach the graph through this one call site, so none of them has to change. Six keep the new processors in a member, and for those the mark is all that is needed; five keep them in a local, and for those this turns a read of a freed peer in addEdges into a clean cancellation. update is default-constructed and move-assigned, both allocation-free for two std::lists, and on the connect-phase path update.to_add is empty, so the splice is a deliberate no-op: the new processors stay owned by the throwing processor, whose own node is live, which is why they need no rescue list there. The exception still propagates and still cancels the query. The regression test extends the file ClickHouse#121502 added. Its expander connects its sink, keeps owning it the way the production overrides do, and then throws MEMORY_LIMIT_EXCEEDED, so no failpoint is needed and the window is exact. Reverting only the hoist makes it reproduce the logical error above; the pre-existing test for the recording window passes either way. Closes: ClickHouse#121696 Related: ClickHouse#121502 Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Two independent passes before opening: my own cold read of the resulting code (3 findings), then an
Severity: ❌ blocker / I re-derived the change's two load-bearing claims rather than inheriting them: the census is 11 production Session id: cron:clickhouse-review-slot-9:20260923-064300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20260923-061000 |
|
Workflow [PR], commit [7f75e56] Summary: ✅
AI ReviewSummaryThis PR moves Findings❌ Blockers
Final Verdict
LLVM Coverage ReportMeasured on commit 7f75e56.
Changed lines: Changed C/C++ lines covered: 5/5 (100.00%) · Uncovered code |
| try | ||
| { | ||
| update = cur_node.processor()->updatePipeline(); |
There was a problem hiding this comment.
This still only rescues processors that have already made it into the returned PipelineUpdate. A production override like ExternalDistinctTransform::updatePipeline still builds the expansion in a local Processors (ExternalDistinctTransform.cpp:610), wires prepared.source / prepared.sink into this first (ExternalDistinctTransform.cpp:600-604), and then can still throw on later inputs.emplace_back / outputs.emplace_back / processors.emplace_back calls (ExternalDistinctTransform.cpp:621-647). On that path update here is still empty, so the catch marks expansion_failed but the connected children are destroyed during unwinding and the surviving ports point at freed peers. We already have generic traversals like IProcessor::getProcessorsProfileLogInfo() (IProcessor.cpp:174-179) that dereference connected outputs unconditionally, so this is still a dangling-port / UAF risk after the logical-error fix.
Can we either pin not-yet-returned children before any connect happens, or narrow the claim/comment and add coverage for the local-owned expansion shape separately?
There was a problem hiding this comment.
Agreed on the shape, with two corrections to the citations, measured on this head.
The local is ExternalDistinctTransform.cpp:610, moved out only in the return at :656. The connects that hand a live node a peer it does not own are :588 (connect(*output, inputs.back()), reached through connectMerge) and :602 (connect(outputs.back(), prepared.sink->getPort())); on a run where merge_registration is already set, :600 hands the live merger a dangling input peer too, so prepared.source goes into the merger's inputs rather than into this. The allocating steps after those connects are :621, :625 and :643. Census: 5 of the 11 production overrides keep the new processors in a local (the four lazy-read sources plus this one), and this is the only one that connects a this output port to one of them, which matters because the traversal you name walks outputs only.
Where I do not follow is the consequence after this change. A connected port's peer processor is reachable only through Port::getInputPort() / getOutputPort() (the peer is a raw pointer, Port.h:288/:412, while state is a shared_ptr that outlives it), or through an Edge, and no edge exists for a connection made during the failed expansion because addEdges is what would have created it. Every unconditional peer dereference in src/:
ExecutingGraph.cpp:140and:153, in theaddEdgeswalk over every node (:248-252). That is the read the mark prevents. Before this PR the nextupdateNodereached:153on the live transform's new output port, which passes the skip at:150-151, and dereferenced the freed sink there beforeaddEdgecould report the missing processor. So the hoist removes that read rather than leaving it.IProcessor.cpp:178, the one you name. Its callers arefinalizeQueryPipelineBeforeLogging(executeQuery.cpp:719), reached only fromBlockIO::onFinish(BlockIO.cpp:96-98), and three set / scalar-subquery sites (PreparedSets.cpp:742,evaluateScalarSubqueryIfNeeded.cpp:374,ExecuteScalarSubqueriesVisitor.cpp:267), each straight-line after a pipeline that completed.BlockIO::onException(:109-120) runs the exception callbacks, whose signature takes no pipeline, and thenresetPipeline; it never callsfinalize_query_pipeline, andlogQueryExceptionreferences no profile info. Measured on this build rather than only read: withlog_processors_profiles=1and explicit query ids, a successful query wrote 47 rows tosystem.processors_profile_log, while the same shape failing during execution onmax_memory_usage(MEMORY_LIMIT_EXCEEDED,While executing AggregatingTransform) and athrowIffailure wrote 0 rows each, withsystem.query_logconfirming all three ran and how each ended.IQueryPlanStep.cpp,AnalyzePlanStats.cpp,Pipe.cpp,QueryPipeline.cpp,printPipeline.cpp:75: pipeline build andEXPLAIN, never with a runtime expansion in flight. The templateprintPipelineis already hardened for a destroyed peer, see the comment above its input-connection map.
So the dangling port is latent rather than reachable, and pinning the children before the connect means changing those five overrides, or the interface they share, which is a different defect from the single caller-side boundary this PR moves.
On coverage: the mark sits in the caller, one site that all 11 overrides go through, so it does not depend on an override's ownership shape. A local-owned variant of the new gtest would assert the same branch with a negative control that has to read freed memory in order to fail; the committed one uses the member-owning shape, where the unfixed tree gives a deterministic logical error instead.
You are right about one thing in the comment: "keep the new processors alive" is exact only for the processors the call already returned. If a reviewer wants that clause narrowed I will push it; on its own it did not look worth restarting a CI run that is still in flight.
Build profile diff (arm_release)Commit See the job log for details. |
CI finish ledger - 5f83d44Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Shards 1/6, 2/6, 3/6, 5/6 and 6/6 all passed on this head. Session id: cron:our-pr-ci-monitor:20260923-190314 |
…graph-mark-connect-phase-throw
The PR workflow run 37635300084 for 348019e ended with 9 jobs abandoned by GitHub during the 2026-10-07 Actions outage, and its Unit tests (tsan) check is stuck queued, so the run can never finish. No code change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #121696
Related: #121502
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 output for processor ..., but not found in list of processors) that replaced the original error when a query failed on the memory limit while a processor was connecting the new processors of a pipeline expansion.Description
On 26.9
ExecutingGraph::updatePipelinecalls the processor'supdatePipeline()outside anytry, so a query that hitsmax_memory_usageinside a pipeline expansion while another thread expands a second node fails withLogical error: 'Processor ... was found as output for processor ..., but not found in list of processors'instead ofMEMORY_LIMIT_EXCEEDED.#121502 closed half of that window. The overrides connect a live node's port to a new processor inside the virtual, and most of them keep allocating afterwards, so the graph can already be inconsistent with the ports before the virtual returns, while the
trythat setsexpansion_failedopened only after it. A throw in between left the mark unset, so the next thread taking thenodes_mutexwrite lock walked every node inaddEdgesand tripped over the connection. The boundary was one step early, not deliberate: #121502's comment opens "From here on every step that records them".The fix moves the call inside the existing
try: one site, which all 11 overrides go through. The exception still propagates and cancels the query.Validation: a new gtest whose expander connects its sink and then throws
MEMORY_LIMIT_EXCEEDED, so the window is exact and needs no failpoint. Reverting the hoist reproduces the logical error above; with it both tests pass, 50 repeats. #121502'sGROUPING SETSquery, run 50 times withmemory_tracker_fault_probability, givesMEMORY_LIMIT_EXCEEDEDevery time with no logical error; that is sampling, the gtest is the proof.Same signature, on a build predating #121502 whose stack cannot tell the two windows apart: https://github.com/ClickHouse/ClickHouse/actions/runs/35612819197/job/106453836325
ExternalDistinctTransform::updatePipelinealso holds some of its new processors in a local, so a throw there leaves one of its own output ports pointing at a freed processor. That is a pre-existing property of that override, not something the mark fixes, and I am not changing it here.Workflow [PR]
Sync PR [sync-upstream/pr/121712]