Skip to content

Write processors_profile_log entries for queries that fail with an exception - #121503

Merged
azat merged 9 commits into
ClickHouse:masterfrom
alexandrefimov:processors-profile-log-on-exception
Oct 5, 2026
Merged

azat merged 9 commits into
ClickHouse:masterfrom
alexandrefimov:processors-profile-log-on-exception

Conversation

@alexandrefimov

@alexandrefimov alexandrefimov commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Until now system.processors_profile_log got entries only when a query finished successfully. Now it also gets them for queries that end with ExceptionWhileProcessing, for example MEMORY_LIMIT_EXCEEDED, which are often the most interesting ones.

LocalConnection now stops its executors before onException, as it already does before onFinish, so their threads no longer write the processor counters while those counters are being read.

Related: #106263

cc @azat

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

system.processors_profile_log now has entries for queries that fail with an exception, and new columns exception_code and exception.


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

Version info

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

…exception

Until now `processors_profile_log` was written only from the query finish path,
so queries ending with `ExceptionWhileProcessing` (for example
`MEMORY_LIMIT_EXCEPTION`) had no entries. `BlockIO::onException` now passes the
pipeline, which is not reset yet, to the exception callbacks, and the callback
from `executeQuery` logs its processors.

`LocalConnection` kept its executors alive during `onException`, so their
threads could still write the processors' counters. It now stops them first,
as `finishQuery` does before `onFinish`.

Related: ClickHouse#106263
…iled query

A query that failed because of the memory limit still holds its pipeline's
memory when the exception callback runs. The allocations in
`logProcessorProfile` could then throw again, replace the original exception
and skip the pipeline reset in `BlockIO::onException`.
@azat azat self-assigned this Sep 22, 2026
@azat azat added the can be tested Allows running workflows for external contributors label Sep 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9b30baa]

Summary: ✅


AI Review

Summary

This PR makes system.processors_profile_log emit rows for queries that end in ExceptionWhileProcessing, adds exception_code/exception columns to those rows, and fixes the QueryPipeline ownership ordering that was found during review. The remaining blocker is the exception-path logging itself: it still performs best-effort allocations before BlockIO::resetPipeline(), but now only suppresses the Process tracker, so queries that fail on user/global memory limits can still lose the original exception and skip teardown.

Findings

❌ Blockers

  • [src/Interpreters/executeQuery.cpp:3416] [dismissed by author -- https://github.com/Write processors_profile_log entries for queries that fail with an exception #121503#discussion_r4074432133] LockMemoryExceptionInThread(VariableContext::Process) only blocks the Process and Thread trackers (VariableContext is ordered Global -> User -> Process -> Thread), so this new logProcessorProfile call can still throw again under max_memory_usage_for_user or global memory pressure before BlockIO::onException() reaches resetPipeline(). That still replaces the original failure and can skip pipeline teardown entirely on a common MEMORY_LIMIT_EXCEEDED path.
  • Suggested fix: either widen the suppression to cover the parent trackers for this best-effort logging, or catch/log any exception from logProcessorProfile inside the exception callback so BlockIO::onException() always preserves the original exception and still runs resetPipeline().
Final Verdict

❌ Changes requested — the exception-path processor logging is still not safe under user/global memory-limit failures.

LLVM Coverage Report

Measured on commit 9b30baa.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 92.10% +0.30%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 144/146 (98.63%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 22, 2026

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

Mostly ok, thanks. Please take a look at the comments.

Comment thread src/QueryPipeline/BlockIO.cpp Resolved
Comment thread src/Interpreters/executeQuery.cpp Resolved
Comment thread src/Interpreters/executeQuery.cpp Resolved
Comment thread src/Interpreters/executeQuery.cpp Resolved
- Add `exception_code` and `exception` columns to `system.processors_profile_log`.
- Check that `processors` is set in `QueryPipeline::initialized`: a moved-from pipeline has none.
- Do not block the global memory tracker while logging the processors.
- Pass the settings in the query itself and add a test over HTTP.
@azat

azat commented Sep 22, 2026

Copy link
Copy Markdown
Member

@alexandrefimov the tests has failed, can you please take a look?

@alexandrefimov

Copy link
Copy Markdown
Contributor Author

Thanks, fixed: the test didn't find the query in query_log, because its text there starts with a comment.

Comment thread src/Interpreters/executeQuery.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-system-table system.* tables (introspection, system metadata, performance visibility). label Sep 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9b30baaaf with master 915744a54 (stripped binary size, per-symbol sizes and ThinLTO time; 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

1010 translation units recompiled, 7481 s compile time in total, 1010 of them have a recent master baseline.

Job report

@alexandrefimov

Copy link
Copy Markdown
Contributor Author

CH Inc sync fails with build failed, and I can't see the private log. My guess is the signature change of BlockIO::exception_callbacks (void(bool) -> void(bool, const QueryPipeline &)) breaking a callback in the private code. Could you take a look, or paste the error here?

@azat

azat commented Sep 22, 2026

Copy link
Copy Markdown
Member

CH Inc sync fails with build failed, and I can't see the private log. My guess is the signature change of BlockIO::exception_callbacks (void(bool) -> void(bool, const QueryPipeline &)) breaking a callback in the private code. Could you take a look, or paste the error here?

It was not related to your changes, the private build was broken, it is fixed already, so after you push something here the private CI will be restarted

`QueryPipeline` connected a new processor's ports and only then put it into
`processors`. The `emplace_back` in between allocates, so it can throw, and then
the processor is destroyed while the pipeline keeps ports pointing into it.

`04299_move_assignment_memory_limit` injects a memory tracker failure into an
arbitrary allocation and hit exactly this. Logging the processors of a failed
query then read the destroyed ports and reported `Port does not belong to
Processor`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121503&sha=066218e895a09aff1c95cf6b74fe1abf7bcd3d94&name_0=PR&name_1=Stateless%20tests%20(arm_binary,%20parallel)
ClickHouse#121503
@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Sep 23, 2026
@alexandrefimov

Copy link
Copy Markdown
Contributor Author

The arm_binary failure was real: QueryPipeline connected a processor's ports before putting it into processors, so a throw in between left the pipeline with ports into a destroyed processor. 04299_move_assignment_memory_limit injects an allocation failure and hit exactly that. Fixed by owning the processor first.

Comment thread src/QueryPipeline/QueryPipeline.cpp Resolved
… limit

- Own the processor before connecting it in the remaining places of
  `QueryPipeline`: the `Chain` constructor, `setLimitsAndQuota`, the query
  result cache transforms and sources, and `addExpression`.
- Block `MEMORY_LIMIT_EXCEEDED` from the query's own tracker while logging the
  processors of a failed query. Otherwise the new exception replaces the
  original one and `BlockIO::onException` never resets the pipeline. The lock is
  at `VariableContext::Process`, so the server-wide and per-user limits still
  apply.
@alexandrefimov

Copy link
Copy Markdown
Contributor Author

The bot found two real things: the QueryPipeline fix missed five more places, and logging can hit the query's memory limit again and replace the original exception. I put the lock back, but at VariableContext::Process, so only the query's own tracker is blocked, not the global one.

@alexandrefimov

Copy link
Copy Markdown
Contributor Author

All three failures look unrelated: the fuzzer one is #121464 (fails on master since 2026-09-21), the integration test is retry_ok and also fails on master, and serialization_pool_lookup #0 is rarely unstable on master while the job reports disk-almost-full. The stateless arm_binary shard is green now.

Comment thread src/Interpreters/executeQuery.cpp Resolved
Comment thread src/QueryPipeline/QueryPipeline.cpp Resolved
Comment thread src/QueryPipeline/QueryPipeline.cpp Resolved

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

Sorry for the delay, looks good, thanks!

@azat
azat enabled auto-merge October 5, 2026 08:53
@azat
azat added this pull request to the merge queue Oct 5, 2026
Merged via the queue into ClickHouse:master with commit 4f12852 Oct 5, 2026
149 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 5, 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]>
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 17, 2026
…ecuting graph

The `PipelineExecutor` constructor appended a `printPipeline` dump to any
exception thrown while the `ExecutingGraph` was built. Besides a
`LOGICAL_ERROR` (a malformed pipeline, which the dump is for), the only
error there is a failed allocation, which says nothing about the pipeline,
while the dump costs ~80 bytes per processor.

Since ClickHouse#121503 the exception text is also copied into every
`processors_profile_log` row of the query, so the cost became quadratic in
the number of processors. In `Stress test (amd_tsan)` three cancelled
`urlCluster` secondaries with 25,902 processors each hit an injected memory
limit there, then spent ~9 minutes copying ~55 GB per query into the log
queue and pushed the server over its total memory limit.

Attach the dump only to a `LOGICAL_ERROR`. Add the
`executing_graph_build_fail` failpoint, which raises a memory-limit error
from the graph construction, and a test that such an error carries no dump.

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

can be tested Allows running workflows for external contributors comp-system-table system.* tables (introspection, system metadata, performance visibility). external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements 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