Skip to content

Apply -WithMemoryTracking containers to src/Processors/Executors - #124906

Merged
nickitat merged 5 commits into
masterfrom
wmt_processors_executors
Oct 10, 2026
Merged

nickitat merged 5 commits into
masterfrom
wmt_processors_executors

Conversation

@nickitat

@nickitat nickitat commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

Version info

  • Merged into: 26.10.1.2078-master (included in 26.10 and later)

Replace the `std::` containers of the pipeline executor with their
`-WithMemoryTracking` counterparts and add `src/Processors/Executors` to the
style check, so the executor's per-processor bookkeeping is charged to the
query that allocates it.

The executing graph scales with the pipeline: its nodes, edges, per-node
added-edge lists, the processors map and the pending-removal map all grow
with the processor count, as do the per-thread task queues and the thread
stack.

Two containers outside the directory are converted because the executor is
what makes them grow: `Port::UpdateList`, which has no other consumer, and
the `WorkIntervals` aliases filled by `ExecutionThreadContext`. Every
consumer of the latter takes it by const reference or by value with a move,
so no conversion or copy is introduced anywhere.

Kept as `std::` with a reason: `Processors`, which is interchangeable with
the alias in `IProcessor_fwd.h`, and the task `Queue`, whose container is
already a `boost::container::devector` with `AllocatorWithMemoryTracking`.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
@clickhouse-gh

clickhouse-gh Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d086938]

Summary: ✅


AI Review

Summary

This PR replaces most executor-side scratch containers with -WithMemoryTracking variants and extends the style check to src/Processors/Executors, which closes the earlier Updated*Ports / std::stack holes. The remaining problem is that the runtime pipeline-expansion path still allocates through the shared plain-std::list aliases for Processors, InputPorts, and OutputPorts, so a wide lazy/external expansion can still overcommit past the query limit before the new tracked executor containers get involved. Verdict: not ready to merge until that expansion path is covered too, or the PR scope/claim is narrowed accordingly.

Findings

❌ Blockers

  • [src/Processors/IProcessor.h:31] [dismissed by author -- https://github.com/Apply -WithMemoryTracking containers to src/Processors/Executors #124906#discussion_r4232216348] The executor still performs runtime graph growth through plain std::list aliases. IProcessor::PipelineUpdate::to_add / to_remove stay on Processors, and ExecutingGraph::addNode appends every runtime-added processor into that same list, so those list-node allocations still use the non-throwing tracker on the exact lazy/external expansion paths this PR is hardening. The same hole remains for ports: InputPorts / OutputPorts are still plain std::list in src/Processors/Port.h:499, and updatePipeline implementations such as src/Processors/Sources/LazyReadFromFileSource.cpp:81, src/Processors/Sources/DelayedSource.cpp:160, and src/Processors/Transforms/ExternalDistinctTransform.cpp:979 keep growing them while the query is already running. That means a wide runtime expansion can still bypass the query memory limit even after the new tracked vectors/queues are in place.
    Suggested fix: move the shared Processors, InputPorts, and OutputPorts aliases that participate in updatePipeline growth to ListWithMemoryTracking, together with the executor-side storage that reuses Processors (for example PendingRemovalGroup and processors_of_failed_expansion).
Final Verdict

❌ Changes requested: the runtime expansion flow still has real untracked allocation paths through Processors / InputPorts / OutputPorts, so the main overcommit hole is not closed yet.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 9, 2026
`DevectorWithMemoryTracking` joins the other container aliases in
`src/Common`, with `DevectorQueueWithMemoryTracking` for a `std::queue` that
keeps its elements in one.

The pipeline executor had spelled this type out three times, under a local
name one letter away from the unrelated `DequeWithMemoryTracking`, and
`MarkRanges` had spelled it out twice more. All five now use the alias, which
also lets the executor's queues drop their `STYLE_CHECK_ALLOW_STD_CONTAINERS`
exemptions: they are no longer a `std::` container with a tracking allocator
bolted on, but a tracked type of their own.

Converting the six remaining `devector` uses in the executing graph fixes a
real gap rather than a cosmetic one. The pipeline-update stack, `updated_edges`
and `updated_processors` all grow with the graph and were allocated through the
default allocator, so none of it was tracked - and the style check could not
see them, because its pattern only matched `std::` containers. Extend that
pattern to `boost::container::devector`, which fires nowhere else in the linted
directories.

A `devector` object is smaller than a `deque` (32 against 48 bytes) and keeps
its elements contiguous; both allocate nothing while empty.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Oct 9, 2026
Comment thread src/Processors/Port.h Resolved
`IProcessor::UpdatedInputPorts` and `UpdatedOutputPorts` are the buffers
`ExecutingGraph::updateNode` fills before calling `prepare`, and they sit in
the same `Node` struct as the already tracked `post_updated_*_ports`, so
tracking only one of the two left the update path half accounted.

Every override of the two-argument `prepare` spells its parameters with these
aliases and takes them by const reference, so all of them follow the change
with no edit and no copy; each one declares `override`, so the build failing
is what would catch an override silently becoming a hiding overload.

Reported in review of #124906

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-improvement Pull request with some product improvements labels Oct 9, 2026
The std-container style check did not match `std::stack`, so stacks in
linted directories were never swept. One was in the executing graph this
series just put under the check: the traversal stack of `initializeExecution`,
whose default container is a plain `std::deque`.

Add `StackWithMemoryTracking`, a `std::stack` over `DequeWithMemoryTracking`,
alongside `QueueWithMemoryTracking`, and extend the check's pattern to
`std::stack`. Doing so exposes four more untracked stacks in directories that
were already linted, so convert those too, or the check would fail there:
the backtracking stack of `sequenceMatch`, the subnet stack of the IP trie
dictionary, the AST stack of SQL user-defined functions, and the checkpoint
offsets of `PeekableReadBuffer`.

The backing container stays a `deque`, as for `std::stack` by default, so
none of these sites changes how it grows or when references into it are
invalidated.

`std::priority_queue` and `std::forward_list` are not added to the pattern.
The only `priority_queue` in a linted directory sits on a storage adaptor
that does not allocate, and the two `forward_list` uses in `src/IO` need an
alias of their own.

Reported in review of #124906

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01DFFRj3kqWUsA1C3A8DapeT
Comment thread src/Processors/IProcessor.h Resolved
@nickitat

nickitat commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

@Algunenano, mind taking a look?

@nickitat
nickitat marked this pull request as ready for review October 9, 2026 16:41
@Algunenano Algunenano self-assigned this Oct 9, 2026

@Algunenano Algunenano 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.

Only one style exception, so all good. Thanks!

@nickitat
nickitat enabled auto-merge October 9, 2026 17:39
@nickitat
nickitat added this pull request to the merge queue Oct 10, 2026
Merged via the queue into master with commit 28bdac1 Oct 10, 2026
322 of 324 checks passed
@nickitat
nickitat deleted the wmt_processors_executors branch October 10, 2026 22:49
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 10, 2026
valerypetrov pushed a commit to valerypetrov/ClickHouse that referenced this pull request Oct 13, 2026
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]>
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