Repository navigation
Conversation
… output peer getProcessorsProfileLogInfo built parent_ids by following each connected output port to its peer via getInputPort().getProcessor(). A Port keeps its shared State (so it stays isConnected()) and its raw peer processor pointer even after the peer processor is destroyed, so during streaming pipeline teardown (ExecutingGraph::removeNode erases finished processors while survivors still reference them through ports) this dereferenced a freed processor: a heap-use-after-free read. Unlike the debug-only printPipeline variant (ClickHouse#110955), this path is release-reachable: finalizeQueryPipelineBeforeLogging gathers the profile info unconditionally on successful query finish, and logProcessorProfile(context, processors) is also called for scalar-subquery / prepared-set logging. The parent-id computation moves into the free getProcessorsProfileLogInfo(const Processors &), which has the whole processor set: it maps each input port's shared connection id to the owning processor id, then matches each connected output port through that map without dereferencing a peer processor. Output is unchanged for well-formed pipelines. Mirrors the connection-identity approach from ClickHouse#110955 (Port::getConnectionId()). Related: ClickHouse#110955 Related: ClickHouse#110834 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings across 1 full pass + 1 fix round + 1 delta recheck; bounded).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-0:20260719-205500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-0:20260719-205500 |
|
cc @yariks5s @KochetovNicolai — could you review this? It fixes a release-reachable heap-use-after-free in |
|
Workflow [PR], commit [7c5e5ac] Summary: ❌
AI ReviewSummaryThis PR moves Findings❌ Blockers
Tests
Final Verdict
|
| { | ||
| if (!port.isConnected()) | ||
| continue; | ||
| /// Only record the parent if the peer input port belongs to a processor in this set. |
There was a problem hiding this comment.
parent_ids is safe after this change, but the release logging path still hits the same dangling peer when it builds pipeline_dump. logProcessorProfile(context, processors) still calls printPipeline(processors, out, true) (src/Interpreters/ProcessorsProfileLog.cpp:183-189), finalizeQueryPipelineBeforeLogging does the same for normal query finish (src/Interpreters/executeQuery.cpp:636-640), and printPipeline still follows port.getInputPort().getProcessor() after only isConnected() (src/QueryPipeline/printPipeline.h:62-67, 87-93). Because ExecutingGraph::removeNode erases finished processors from the shared Processors list (src/Processors/Executors/ExecutingGraph.cpp:65-67), scalar-subquery logging, prepared-set logging, and query-finish logging can still read a freed peer through the dump path. This needs the same getConnectionId()-based lookup in printPipeline, or the dump needs to be skipped for torn-down processor sets.
There was a problem hiding this comment.
Confirmed the deref is real: printPipeline (printPipeline.h) still reads port.getInputPort().getProcessor() after only isConnected(), and the release pipeline_dump reaches it via logProcessorProfile (ProcessorsProfileLog.cpp:188) and finalizeQueryPipelineBeforeLogging (executeQuery.cpp:639).
That printPipeline fix is owned by sibling PR #110955, which rewrites the same function to the identical getConnectionId() map and drops both peer-deref loops (printPipeline.h:62-67 and 87-93). printPipeline is the single free function shared by the debug dump and the release pipeline_dump, so #110955 closes the release path too.
This PR is scoped to parent_ids in getProcessorsProfileLogInfo; #110955 covers printPipeline. Together they close the full surface. Folding the printPipeline rewrite in here would duplicate #110955 on the same lines (add/add conflict) and re-review an approved change, so keeping the split.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 56/59 (94.92%) · Uncovered code |
CI finish ledger — 7c5e5acCI fully finished: Finish Workflow, Mergeable Check and CH Inc sync all SUCCESS. Every red
Session id: cron:our-pr-ci-monitor:20260720-093000 |
yariks5s
left a comment
There was a problem hiding this comment.
The connection-ID lookup is locally memory-safe, but the surrounding design is risky
- getProcessorsProfileLogInfo silently stops populating parent_ids. Its name and return type still promise complete information
-it changes system.processors_profile_log semantics, connections to processors absent from the supplied set just disappear - adds another hash-map allocation to every successful query unconditional finish path, also when processor-profile logging is disabled
- it works around dangling pointers in selected readers instead of fixing their lifetime
I will get back to this PR later.
|
Thanks, these are fair. Below is what I measured for each point, plus the one design call I would like you to make. 1. 2. First, what the old code did for such a peer was not "complete information", it was a read of freed memory. Under ASAN the unit test that constructs exactly this state (a survivor whose connected output port points at a destroyed peer) reports on the pre-fix binary: and aborts (exit 134). The id it would have pushed is Second, I measured how often this actually changes what gets logged for well-formed pipelines. On a debug build of current master (
Zero out-of-set parents in every shape, including the streaming one, which is the case where That still leaves your design question, and I do not want to decide it unilaterally: for a peer that is genuinely not in the set, should the row omit the connection (what the PR does now) or record a sentinel parent id so the "complete information" contract holds and a consumer can see that an edge was dropped? The sentinel also answers point 1. I have no preference; tell me which you want and I will implement it. 3. Hash map on every successful finish, also when profile logging is off. Correct, and I would rather fix it than defend it: Worth noting the same line does something larger unconditionally today, independent of my change: the 4. Working around dangling pointers instead of fixing their lifetime. This is the one I do not want to answer for you. The mechanism is: Fixing the lifetime properly means deciding who owns the invariant: either One thing you should know before choosing that route: #110955 (approved by @ alexey-milovidov, closes #110834) takes the same connection-identity approach for No rush on any of this. When you come back to it, could you also say whether you would prefer #111019 reopened or a fresh PR? The underlying read is still present on master, so I would like to land something for it either way. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a heap-use-after-free when collecting
system.processors_profile_logparent ids on query finish: a processor could be read through a still-connected output port after its peer processor was already destroyed during pipeline teardown.Description
IProcessor::getProcessorsProfileLogInfo()builtparent_idsby following each connected output port to its peer viaport.getInputPort().getProcessor(). APortkeeps itsshared_ptr<State>(so it staysisConnected()) and its rawprocessorpointer even after the peer processor is destroyed, so during streaming pipeline teardown (ExecutingGraph::removeNodeerases finished processors while survivors still reference them through ports) this dereferenced a freed processor.Unlike the debug-only
printPipelinevariant fixed in #110955, this path is reachable in release builds:finalizeQueryPipelineBeforeLogginggathers the profile info unconditionally on successful query finish, andlogProcessorProfile(context, processors)is also called for scalar-subquery / prepared-set logging.The parent-id computation is moved into the free
getProcessorsProfileLogInfo(const Processors &), which has the whole processor set: it maps each input port's shared connection id to the owning processor id, then matches each connected output port through that map. It never dereferences a peer processor, and produces identicalparent_idsfor well-formed pipelines. This mirrors the connection-identity approach approved in #110955 (Port::getConnectionId()); if #110955 merges first the duplicated accessor drops out cleanly.