Skip to content

Reintroduce borrowed threadgroup async uaf fix - #109891

Closed
filimonov wants to merge 34 commits into
ClickHouse:masterfrom
filimonov:reintroduce-borrowed-threadgroup-async-uaf-fix
Closed

filimonov wants to merge 34 commits into
ClickHouse:masterfrom
filimonov:reintroduce-borrowed-threadgroup-async-uaf-fix

Conversation

@filimonov

@filimonov filimonov commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Reintroduce #108988

Related: #107030
Related: #108577
CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105890&sha=3dc0e76362eb18e27f1fffcd3f61ca9f13725fe8&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%201%2F2%29

Fixes a use-after-free risk in asynchronous work scheduled while a borrowed ThreadGroup is current.

Borrowed ThreadGroup objects used by materialized view and async insert flush paths point their performance_counters and memory_tracker to the parent query group, so they are valid only while that parent group is alive. Async callbacks could capture such a borrowed group and later attach it on a pool thread after the parent query group had finished.

Instead of keeping the parent ThreadGroup alive with a shared_ptr, this change keeps borrowed accounting scoped. Borrowed groups are marked explicitly, async callback capture drops borrowed groups, and thread pool callback runners capture the normalized group at task enqueue time rather than when a potentially long-lived runner object is created.

This preserves synchronous borrowed accounting, but async work started from a borrowed scope runs under normal thread/global accounting instead of writing into, or prolonging the lifetime of, an already finished query group.

Full ASAN reports https://gist.github.com/filimonov/1ec59047c65e3a5367c5c83f6021cc27

Compared to #108988 - added one commit with code comments + fix of the test failure #109841

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixes a memory safety issue where asynchronous work scheduled from materialized view processing could keep using query-level accounting after the query had finished.

…rowed `ThreadGroup` scopes"

This reverts commit f1a27b6.
@azat azat self-assigned this Jul 9, 2026
@azat azat added the can be tested Allows running workflows for external contributors label Jul 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0278b0c]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
Build ClickHouse FAIL
Build (arm_tidy) FAIL
Build ClickHouse FAIL cidb
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 9, 2026
Comment thread src/Common/threadPoolCallbackRunner.h Resolved
filimonov and others added 2 commits July 9, 2026 18:05
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Test introduced by ClickHouse#109111

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@filimonov
filimonov force-pushed the reintroduce-borrowed-threadgroup-async-uaf-fix branch from 1663dee to ddd0ed9 Compare July 9, 2026 17:00
@filimonov

Copy link
Copy Markdown
Contributor Author

Dropping a borrowed ThreadGroup to nullptr fixes the lifetime hazard, but it also changes where the callback's captured objects are destroyed.

Yes, if an async worker frees memory that was allocated under the parent query, the accounting can skew.

But the skew looks safe enough: nullptr here just means the global counter, which is the parent of every per-query counter anyway, so nothing is lost at the global level — only the per-query attribution is temporarily off. A real "reparent" of the already-accounted bytes isn't practical (the MemoryTracker isn't per-object, so there's nothing to transfer cleanly), and leaving the UAF in place is much worse.

Alternatives, none of them free:

  • Extend the lifetime of the parent counters instead — very simple, but high risk of turning it into a leak.
  • Capture a weak_ptr to the parent group and try to route the accounting through it when still alive — doable, but that's another layer of complexity.

@filimonov

Copy link
Copy Markdown
Contributor Author

praktica failed to recognize the root cause - server died due to the logical error assert:

Details

2026.07.09 21:16:14.562632 [ 20792 ] {48f4aa83-e954-4e4c-806f-77b83e02be1f} <Fatal> : Stack trace (when copying this message, always include the lines below):

0. contrib/llvm-project/libcxx/include/__exception/exception.h:115:14: Poco::Exception::Exception(String&&, int) @ 0x00000000358b81f3
1. src/Common/Exception.cpp:139:7: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000001be87770
2. src/Common/Exception.h:171:100: DB::Exception::Exception(String&&, int, String, bool) @ 0x00000000119b5119
3. src/Common/Exception.h:57:54: DB::Exception::Exception(PreformattedMessage&&, int) @ 0x00000000119b4bc9
4. src/Common/Exception.h:189:77: DB::Exception::Exception<String, String const&, String, String const&, String>(int, FormatStringHelperImpl<std::type_identity<String>::type, std::type_identity<String const&>::type, std::type_identity<String>::type, std::type_identity<String const&>::type, std::type_identity<String>::type>, String&&, String const&, String&&, String const&, String&&) @ 0x000000001332884b
5. src/Functions/FunctionBinaryArithmetic.h:3166:19: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::executeImpl2(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, DB::PODArray<char8_t, 4096ul, Allocator<false, false>, 63ul, 64ul> const*, DB::PODArray<char8_t, 4096ul, Allocator<false, false>, 63ul, 64ul>*) const @ 0x0000000018d2d646
6. src/Functions/FunctionBinaryArithmetic.h:2986:16: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::executeImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x0000000018d29dcd
7. src/Functions/FunctionBinaryArithmetic.h:3303:22: DB::FunctionBinaryArithmeticWithConstants<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::executeImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x0000000018d28480
8. src/Functions/IFunction.h:532:16: DB::IFunction::executeImplDryRun(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x00000000119b3711
9. src/Functions/IFunctionAdaptors.cpp:16:22: DB::FunctionToExecutableFunctionAdaptor::executeDryRunImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x000000002151bc41
10. src/Functions/IFunction.cpp:426:15: DB::IExecutableFunction::executeWithoutLowCardinalityColumns(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x00000000215113e3
11. src/Functions/IFunction.cpp:514:24: DB::IExecutableFunction::executeWithoutSparseColumns(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x000000002151341a
12. src/Functions/IFunction.cpp:0: DB::IExecutableFunction::executeWithoutReplicatedColumns(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x0000000021515197
13. src/Functions/IFunction.cpp:0: DB::IExecutableFunction::execute(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x00000000215144ed
14. src/Interpreters/ActionsDAG.cpp:1286:57: DB::executeActionForPartialResult(DB::ActionsDAG::Node const*, std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>>, unsigned long, bool) @ 0x0000000022f20250
15. src/Interpreters/ActionsDAG.cpp:1568:52: DB::ActionsDAG::evaluatePartialResult(std::unordered_map<DB::ActionsDAG::Node const*, DB::ColumnWithTypeAndName, std::hash<DB::ActionsDAG::Node const*>, std::equal_to<DB::ActionsDAG::Node const*>, std::allocator<std::pair<DB::ActionsDAG::Node const* const, DB::ColumnWithTypeAndName>>>&, std::vector<DB::ActionsDAG::Node const*, std::allocator<DB::ActionsDAG::Node const*>> const&, unsigned long, DB::PartialEvaluationParameters) @ 0x0000000022f1ed6e
16. src/Interpreters/ActionsDAG.cpp:1402:26: DB::ActionsDAG::updateHeader(DB::Block const&) const @ 0x0000000022f1d617
17. src/Processors/Transforms/ExpressionTransform.cpp:15:23: DB::ExpressionTransform::transformHeader(DB::Block const&, DB::ActionsDAG const&) @ 0x000000002ebecfd9
18. src/Processors/QueryPlan/ExpressionStep.cpp:70:39: DB::ExpressionStep::ExpressionStep(std::shared_ptr<DB::Block const>, DB::ActionsDAG) @ 0x000000002eeda126
19. contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:770:30: DB::InterpreterSelectQuery::executeImpl(DB::QueryPlan&, std::optional<DB::Pipe>) @ 0x0000000024235694
20. src/Interpreters/InterpreterSelectQuery.cpp:1206:5: DB::InterpreterSelectQuery::buildQueryPlan(DB::QueryPlan&) @ 0x000000002423317b
21. src/Interpreters/InterpreterSelectWithUnionQuery.cpp:345:38: DB::InterpreterSelectWithUnionQuery::buildQueryPlan(DB::QueryPlan&) @ 0x000000002426ea32
22. src/Interpreters/InterpreterSelectWithUnionQuery.cpp:421:5: DB::InterpreterSelectWithUnionQuery::execute() @ 0x000000002426f76f
23. src/Interpreters/executeQuery.cpp:1863:40: DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum, std::unique_ptr<DB::ReadBuffer, std::default_delete<DB::ReadBuffer>>&, boost::intrusive_ptr<DB::IAST>&, std::shared_ptr<DB::ImplicitTransactionControlExecutor>, std::function<void ()>, DB::QueryResultDetails&) @ 0x000000002468e32d
24. src/Interpreters/executeQuery.cpp:2339:11: DB::executeQuery(std::basic_string_view<char, std::char_traits<char>>, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum) @ 0x0000000024687bc3
25. src/Server/TCPHandler.cpp:889:68: DB::TCPHandler::runImpl() @ 0x000000002e689f8f
26. src/Server/TCPHandler.cpp:3210:9: DB::TCPHandler::run() @ 0x000000002e6ad12b
27. base/poco/Net/src/TCPServerConnection.cpp:40:3: Poco::Net::TCPServerConnection::start() @ 0x0000000035976b47
28. base/poco/Net/src/TCPServerDispatcher.cpp:115:42: Poco::Net::TCPServerDispatcher::run() @ 0x0000000035977155
29. base/poco/Foundation/src/ThreadPool.cpp:207:14: Poco::PooledThread::run() @ 0x0000000035913aff
30. base/poco/Foundation/src/Thread_POSIX.cpp:341:27: Poco::ThreadImpl::runnableEntry(void*) @ 0x00000000359111d1
31. __covrec_3FAEAB4F4AAAECB7u @ 0x0000000000094ac3

2026.07.09 21:16:14.562878 [ 718 ] {} <Fatal> BaseDaemon: ########## Short fault info ############
2026.07.09 21:16:14.562906 [ 718 ] {} <Fatal> BaseDaemon: (version 26.7.1.1, build id: 7E04F9618B53DDF08E2149D2B8907CC693B9B108, git hash: 518609647a883decd56aaf09bbc9f3351b1e82ac, architecture: x86_64) (from thread 20792) Received signal 6 (internal)
2026.07.09 21:16:14.562922 [ 718 ] {} <Fatal> BaseDaemon: Signal description: Aborted
2026.07.09 21:16:14.562933 [ 718 ] {} <Fatal> BaseDaemon: Sent by tkill.
2026.07.09 21:16:14.562970 [ 718 ] {} <Fatal> BaseDaemon: Stack trace: 0x00007fc74152d9fd 0x00007fc7414d9476 0x00007fc7414bf7f3 0x0000555d31ad3a09 0x0000555d31ad461d 0x0000555d31ad48fc 0x0000555d27602119 0x0000555d27601bc9 0x0000555d28f7584b 0x0000555d2e97a646 0x0000555d2e976dcd 0x0000555d2e975480 0x0000555d27600711 0x0000555d37168c41 0x0000555d3715e3e3 0x0000555d3716041a 0x0000555d37162197 0x0000555d371614ed 0x0000555d38b6d250 0x0000555d38b6bd6e 0x0000555d38b6a617 0x0000555d44839fd9 0x0000555d44b27126 0x0000555d39e82694 0x0000555d39e8017b 0x0000555d39ebba32 0x0000555d39ebc76f 0x0000555d3a2db32d 0x0000555d3a2d4bc3 0x0000555d442d6f8f 0x0000555d442fa12b 0x0000555d4b5c3b47 0x0000555d4b5c4155 0x0000555d4b560aff 0x0000555d4b55e1d1 0x00007fc74152bac3 0x00007fc7415bd8d0
2026.07.09 21:16:14.562993 [ 718 ] {} <Fatal> BaseDaemon: ########################################
2026.07.09 21:16:14.563049 [ 718 ] {} <Fatal> BaseDaemon: (version 26.7.1.1, build id: 7E04F9618B53DDF08E2149D2B8907CC693B9B108, git hash: 518609647a883decd56aaf09bbc9f3351b1e82ac) (from thread 20792) (query_id: 48f4aa83-e954-4e4c-806f-77b83e02be1f) (query: SELECT n * 0.123 FROM (SELECT * FROM tbl_03007_1)) Received signal Aborted (6)
2026.07.09 21:16:14.563063 [ 718 ] {} <Fatal> BaseDaemon: Sent by tkill.
2026.07.09 21:16:14.563121 [ 718 ] {} <Fatal> BaseDaemon: Stack trace: 0x00007fc74152d9fd 0x00007fc7414d9476 0x00007fc7414bf7f3 0x0000555d31ad3a09 0x0000555d31ad461d 0x0000555d31ad48fc 0x0000555d27602119 0x0000555d27601bc9 0x0000555d28f7584b 0x0000555d2e97a646 0x0000555d2e976dcd 0x0000555d2e975480 0x0000555d27600711 0x0000555d37168c41 0x0000555d3715e3e3 0x0000555d3716041a 0x0000555d37162197 0x0000555d371614ed 0x0000555d38b6d250 0x0000555d38b6bd6e 0x0000555d38b6a617 0x0000555d44839fd9 0x0000555d44b27126 0x0000555d39e82694 0x0000555d39e8017b 0x0000555d39ebba32 0x0000555d39ebc76f 0x0000555d3a2db32d 0x0000555d3a2d4bc3 0x0000555d442d6f8f 0x0000555d442fa12b 0x0000555d4b5c3b47 0x0000555d4b5c4155 0x0000555d4b560aff 0x0000555d4b55e1d1 0x00007fc74152bac3 0x00007fc7415bd8d0
2026.07.09 21:16:14.563178 [ 718 ] {} <Fatal> BaseDaemon: 3. __covrec_EBCB5326BEDD7A3u @ 0x00000000000969fd
2026.07.09 21:16:14.563224 [ 718 ] {} <Fatal> BaseDaemon: 4. gsignal @ 0x0000000000042476
2026.07.09 21:16:14.563248 [ 718 ] {} <Fatal> BaseDaemon: 5. __covrec_538097C0B65CE060 @ 0x00000000000287f3
2026.07.09 21:16:14.643450 [ 718 ] {} <Fatal> BaseDaemon: 6. src/Common/Exception.cpp:60:5: DB::abortOnFailedAssertion(String const&, std::basic_string_view<char, std::char_traits<char>>, void* const*, unsigned long, unsigned long) @ 0x000000001be86a09
2026.07.09 21:16:14.725769 [ 718 ] {} <Fatal> BaseDaemon: 7. src/Common/Exception.cpp:93:13: DB::handle_error_code(String const&, std::basic_string_view<char, std::char_traits<char>>, int, bool, std::vector<void*, std::allocator<void*>> const&) @ 0x000000001be8761d
2026.07.09 21:16:14.844226 [ 718 ] {} <Fatal> BaseDaemon: 8. src/Common/Exception.cpp:146:19: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000001be878fc
2026.07.09 21:16:14.866874 [ 718 ] {} <Fatal> BaseDaemon: 9. src/Common/Exception.h:171:100: DB::Exception::Exception(String&&, int, String, bool) @ 0x00000000119b5119
2026.07.09 21:16:14.888416 [ 718 ] {} <Fatal> BaseDaemon: 10. src/Common/Exception.h:57:54: DB::Exception::Exception(PreformattedMessage&&, int) @ 0x00000000119b4bc9
2026.07.09 21:16:15.092121 [ 718 ] {} <Fatal> BaseDaemon: 11. src/Common/Exception.h:189:77: DB::Exception::Exception<String, String const&, String, String const&, String>(int, FormatStringHelperImpl<std::type_identity<String>::type, std::type_identity<String const&>::type, std::type_identity<String>::type, std::type_identity<String const&>::type, std::type_identity<String>::type>, String&&, String const&, String&&, String const&, String&&) @ 0x000000001332884b
2026.07.09 21:16:15.237181 [ 718 ] {} <Fatal> BaseDaemon: 12. src/Functions/FunctionBinaryArithmetic.h:3166:19: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::executeImpl2(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, DB::PODArray<char8_t, 4096ul, Allocator<false, false>, 63ul, 64ul> const*, DB::PODArray<char8_t, 4096ul, Allocator<false, false>, 63ul, 64ul>*) const @ 0x0000000018d2d646
2026.07.09 21:16:15.486882 [ 718 ] {} <Fatal> BaseDaemon: 13. src/Functions/FunctionBinaryArithmetic.h:2986:16: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::executeImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x0000000018d29dcd
2026.07.09 21:16:15.748733 [ 718 ] {} <Fatal> BaseDaemon: 14. src/Functions/FunctionBinaryArithmetic.h:3303:22: DB::FunctionBinaryArithmeticWithConstants<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::executeImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x0000000018d28480
2026.07.09 21:16:15.771199 [ 718 ] {} <Fatal> BaseDaemon: 15. src/Functions/IFunction.h:532:16: DB::IFunction::executeImplDryRun(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x00000000119b3711
2026.07.09 21:16:16.063830 [ 718 ] {} <Fatal> BaseDaemon: 16. src/Functions/IFunctionAdaptors.cpp:16:22: DB::FunctionToExecutableFunctionAdaptor::executeDryRunImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long) const @ 0x000000002151bc41
2026.07.09 21:16:16.385758 [ 718 ] {} <Fatal> BaseDaemon: 17. src/Functions/IFunction.cpp:426:15: DB::IExecutableFunction::executeWithoutLowCardinalityColumns(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x00000000215113e3
2026.07.09 21:16:16.719990 [ 718 ] {} <Fatal> BaseDaemon: 18. src/Functions/IFunction.cpp:514:24: DB::IExecutableFunction::executeWithoutSparseColumns(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x000000002151341a
2026.07.09 21:16:17.036663 [ 718 ] {} <Fatal> BaseDaemon: 19. src/Functions/IFunction.cpp:0: DB::IExecutableFunction::executeWithoutReplicatedColumns(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x0000000021515197
2026.07.09 21:16:17.366065 [ 718 ] {} <Fatal> BaseDaemon: 20. src/Functions/IFunction.cpp:0: DB::IExecutableFunction::execute(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&, std::shared_ptr<DB::IDataType const> const&, unsigned long, bool) const @ 0x00000000215144ed
2026.07.09 21:16:17.986396 [ 718 ] {} <Fatal> BaseDaemon: 21. src/Interpreters/ActionsDAG.cpp:1286:57: DB::executeActionForPartialResult(DB::ActionsDAG::Node const*, std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>>, unsigned long, bool) @ 0x0000000022f20250
2026.07.09 21:16:18.453048 [ 718 ] {} <Fatal> BaseDaemon: 22. src/Interpreters/ActionsDAG.cpp:1568:52: DB::ActionsDAG::evaluatePartialResult(std::unordered_map<DB::ActionsDAG::Node const*, DB::ColumnWithTypeAndName, std::hash<DB::ActionsDAG::Node const*>, std::equal_to<DB::ActionsDAG::Node const*>, std::allocator<std::pair<DB::ActionsDAG::Node const* const, DB::ColumnWithTypeAndName>>>&, std::vector<DB::ActionsDAG::Node const*, std::allocator<DB::ActionsDAG::Node const*>> const&, unsigned long, DB::PartialEvaluationParameters) @ 0x0000000022f1ed6e
2026.07.09 21:16:18.978682 [ 2909 ] {ef7d6af9-c616-417c-9d40-e16af1871f66} <Error> executeQuery: Code: 43. DB::Exception: Illegal types Int256 and Float64 of arguments of function multiply: While processing n * 0.123. (ILLEGAL_TYPE_OF_ARGUMENT) (version 26.7.1.1) (from [::1]:50318) (comment: 03915_exchange_tables_race.sh-test_0byokpds) (query 1, line 1) (in query: SELECT n * 0.123 FROM (SELECT * FROM tbl_03007_1)), Stack trace (when copying this message, always include the lines below):

0. contrib/llvm-project/libcxx/include/__exception/exception.h:115:14: Poco::Exception::Exception(String&&, int) @ 0x00000000358b81f3
1. src/Common/Exception.cpp:139:7: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000001be87770
2. src/Common/Exception.h:171:100: DB::Exception::Exception(String&&, int, String, bool) @ 0x00000000119b5119
3. src/Common/Exception.h:57:54: DB::Exception::Exception(PreformattedMessage&&, int) @ 0x00000000119b4bc9
4. src/Common/Exception.h:189:77: DB::Exception::Exception<String, String, String>(int, FormatStringHelperImpl<std::type_identity<String>::type, std::type_identity<String>::type, std::type_identity<String>::type>, String&&, String&&, String&&) @ 0x0000000011b1ffbd
5. src/Functions/FunctionBinaryArithmetic.h:2453:15: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::getReturnTypeImplStatic2(std::vector<std::shared_ptr<DB::IDataType const>, AllocatorWithMemoryTracking<std::shared_ptr<DB::IDataType const>>> const&, std::shared_ptr<DB::Context const>) @ 0x0000000018f7099a
6. src/Functions/FunctionBinaryArithmetic.h:2234:16: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::getReturnTypeImplStatic(std::vector<std::shared_ptr<DB::IDataType const>, AllocatorWithMemoryTracking<std::shared_ptr<DB::IDataType const>>> const&, std::shared_ptr<DB::Context const>) @ 0x0000000018f6f8fa
7. src/Functions/IFunction.h:448:16: DB::IFunctionOverloadResolver::getReturnTypeImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&) const @ 0x000000001231fbcc
8. src/Functions/IFunction.cpp:772:45: DB::IFunctionOverloadResolver::getReturnType(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&) const @ 0x0000000021515fcd
9. src/Functions/IFunction.cpp:822:28: DB::IFunctionOverloadResolver::build(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&) const @ 0x0000000021516ac5
10. src/Interpreters/ActionsDAG.cpp:443:36: DB::ActionsDAG::addFunction(std::shared_ptr<DB::IFunctionOverloadResolver> const&, std::vector<DB::ActionsDAG::Node const*, std::allocator<DB::ActionsDAG::Node const*>>, String) @ 0x0000000022f11921
11. src/Interpreters/ActionsVisitor.cpp:484:50: DB::ScopeStack::addFunction(std::shared_ptr<DB::IFunctionOverloadResolver> const&, std::vector<String, std::allocator<String>> const&, String) @ 0x00000000240c14c3
12. src/Interpreters/ActionsMatcher.h:178:27: DB::ActionsMatcher::Data::addFunction(std::shared_ptr<DB::IFunctionOverloadResolver> const&, std::vector<String, std::allocator<String>> const&, String) @ 0x00000000240cbfba
13. src/Interpreters/ActionsVisitor.cpp:1167:14: DB::ActionsMatcher::visit(DB::ASTFunction const&, boost::intrusive_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0x00000000240c5d96
14. src/Interpreters/ActionsVisitor.cpp:0: DB::ActionsMatcher::visit(DB::ASTExpressionList&, boost::intrusive_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0x00000000240c9193
15. src/Interpreters/InDepthNodeVisitor.h:71:13: DB::InDepthNodeVisitor<DB::ActionsMatcher, true, false, boost::intrusive_ptr<DB::IAST> const>::doVisit(boost::intrusive_ptr<DB::IAST> const&) @ 0x00000000240bbb9c
16. src/Interpreters/InDepthNodeVisitor.h:61:9: DB::ExpressionAnalyzer::getRootActions(boost::intrusive_ptr<DB::IAST> const&, bool, DB::ActionsDAG&, bool) @ 0x000000002409f9ce
17. src/Interpreters/ExpressionAnalyzer.cpp:1655:5: DB::SelectQueryExpressionAnalyzer::appendSelect(DB::ExpressionActionsChain&, bool) @ 0x00000000240ad655
18. src/Interpreters/ExpressionAnalyzer.cpp:2246:24: DB::ExpressionAnalysisResult::ExpressionAnalysisResult(DB::SelectQueryExpressionAnalyzer&, std::shared_ptr<DB::StorageInMemoryMetadata const> const&, bool, bool, bool, std::shared_ptr<DB::FilterDAGInfo> const&, std::shared_ptr<DB::FilterDAGInfo> const&, DB::Block const&) @ 0x00000000240b3526
19. src/Interpreters/InterpreterSelectQuery.cpp:1281:23: DB::InterpreterSelectQuery::getSampleBlockImpl() @ 0x0000000024238129
20. src/Interpreters/InterpreterSelectQuery.cpp:1020:55: DB::InterpreterSelectQuery::InterpreterSelectQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context> const&, std::optional<DB::Pipe>, std::shared_ptr<DB::IStorage> const&, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&, std::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::shared_ptr<DB::PreparedSets>)::$_0::operator()(bool) const @ 0x000000002422dc21
21. src/Interpreters/InterpreterSelectQuery.cpp:1030:5: DB::InterpreterSelectQuery::InterpreterSelectQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context> const&, std::optional<DB::Pipe>, std::shared_ptr<DB::IStorage> const&, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&, std::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::shared_ptr<DB::PreparedSets>) @ 0x0000000024227b52
22. src/Interpreters/InterpreterSelectQuery.cpp:337:7: DB::InterpreterSelectQuery::InterpreterSelectQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context> const&, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&) @ 0x000000002422319e
23. contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:770:30: DB::InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(boost::intrusive_ptr<DB::IAST> const&, std::vector<String, std::allocator<String>> const&) @ 0x000000002426d8d3
24. src/Interpreters/InterpreterSelectWithUnionQuery.cpp:183:13: DB::InterpreterSelectWithUnionQuery::InterpreterSelectWithUnionQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context>, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&) @ 0x000000002426c14e
25. contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:770:30: std::unique_ptr<DB::IInterpreter, std::default_delete<DB::IInterpreter>> std::__function::__policy_func<std::unique_ptr<DB::IInterpreter, std::default_delete<DB::IInterpreter>> (DB::InterpreterFactory::Arguments const&)>::__call_func[abi:fqe220101]<DB::registerInterpreterSelectWithUnionQuery(DB::InterpreterFactory&)::$_0>(std::__function::__policy_storage const*, DB::InterpreterFactory::Arguments const&) @ 0x000000002426fcad
26. contrib/llvm-project/libcxx/include/__functional/function.h:502:12: ? @ 0x000000002420a395
27. src/Interpreters/executeQuery.cpp:1800:66: DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum, std::unique_ptr<DB::ReadBuffer, std::default_delete<DB::ReadBuffer>>&, boost::intrusive_ptr<DB::IAST>&, std::shared_ptr<DB::ImplicitTransactionControlExecutor>, std::function<void ()>, DB::QueryResultDetails&) @ 0x000000002468dbb4
28. src/Interpreters/executeQuery.cpp:2339:11: DB::executeQuery(std::basic_string_view<char, std::char_traits<char>>, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum) @ 0x0000000024687bc3
29. src/Server/TCPHandler.cpp:889:68: DB::TCPHandler::runImpl() @ 0x000000002e689f8f
30. src/Server/TCPHandler.cpp:3210:9: DB::TCPHandler::run() @ 0x000000002e6ad12b
31. base/poco/Net/src/TCPServerConnection.cpp:40:3: Poco::Net::TCPServerConnection::start() @ 0x0000000035976b47

2026.07.09 21:16:18.978730 [ 30605 ] {bd909124-7030-4a3d-8fc7-ca10bf6561f6} <Error> executeQuery: Code: 43. DB::Exception: Illegal types Int256 and Float64 of arguments of function multiply: While processing n * 0.123. (ILLEGAL_TYPE_OF_ARGUMENT) (version 26.7.1.1) (from [::1]:50338) (comment: 03915_exchange_tables_race.sh-test_0byokpds) (query 1, line 1) (in query: SELECT n * 0.123 FROM (SELECT * FROM tbl_03007_1)), Stack trace (when copying this message, always include the lines below):

0. contrib/llvm-project/libcxx/include/__exception/exception.h:115:14: Poco::Exception::Exception(String&&, int) @ 0x00000000358b81f3
1. src/Common/Exception.cpp:139:7: DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000001be87770
2. src/Common/Exception.h:171:100: DB::Exception::Exception(String&&, int, String, bool) @ 0x00000000119b5119
3. src/Common/Exception.h:57:54: DB::Exception::Exception(PreformattedMessage&&, int) @ 0x00000000119b4bc9
4. src/Common/Exception.h:189:77: DB::Exception::Exception<String, String, String>(int, FormatStringHelperImpl<std::type_identity<String>::type, std::type_identity<String>::type, std::type_identity<String>::type>, String&&, String&&, String&&) @ 0x0000000011b1ffbd
5. src/Functions/FunctionBinaryArithmetic.h:2453:15: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::getReturnTypeImplStatic2(std::vector<std::shared_ptr<DB::IDataType const>, AllocatorWithMemoryTracking<std::shared_ptr<DB::IDataType const>>> const&, std::shared_ptr<DB::Context const>) @ 0x0000000018f7099a
6. src/Functions/FunctionBinaryArithmetic.h:2234:16: DB::FunctionBinaryArithmetic<DB::MultiplyImpl, DB::NameMultiply, true, true, false>::getReturnTypeImplStatic(std::vector<std::shared_ptr<DB::IDataType const>, AllocatorWithMemoryTracking<std::shared_ptr<DB::IDataType const>>> const&, std::shared_ptr<DB::Context const>) @ 0x0000000018f6f8fa
7. src/Functions/IFunction.h:448:16: DB::IFunctionOverloadResolver::getReturnTypeImpl(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&) const @ 0x000000001231fbcc
8. src/Functions/IFunction.cpp:772:45: DB::IFunctionOverloadResolver::getReturnType(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&) const @ 0x0000000021515fcd
9. src/Functions/IFunction.cpp:822:28: DB::IFunctionOverloadResolver::build(std::vector<DB::ColumnWithTypeAndName, AllocatorWithMemoryTracking<DB::ColumnWithTypeAndName>> const&) const @ 0x0000000021516ac5
10. src/Interpreters/ActionsDAG.cpp:443:36: DB::ActionsDAG::addFunction(std::shared_ptr<DB::IFunctionOverloadResolver> const&, std::vector<DB::ActionsDAG::Node const*, std::allocator<DB::ActionsDAG::Node const*>>, String) @ 0x0000000022f11921
11. src/Interpreters/ActionsVisitor.cpp:484:50: DB::ScopeStack::addFunction(std::shared_ptr<DB::IFunctionOverloadResolver> const&, std::vector<String, std::allocator<String>> const&, String) @ 0x00000000240c14c3
12. src/Interpreters/ActionsMatcher.h:178:27: DB::ActionsMatcher::Data::addFunction(std::shared_ptr<DB::IFunctionOverloadResolver> const&, std::vector<String, std::allocator<String>> const&, String) @ 0x00000000240cbfba
13. src/Interpreters/ActionsVisitor.cpp:1167:14: DB::ActionsMatcher::visit(DB::ASTFunction const&, boost::intrusive_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0x00000000240c5d96
14. src/Interpreters/ActionsVisitor.cpp:0: DB::ActionsMatcher::visit(DB::ASTExpressionList&, boost::intrusive_ptr<DB::IAST> const&, DB::ActionsMatcher::Data&) @ 0x00000000240c9193
15. src/Interpreters/InDepthNodeVisitor.h:71:13: DB::InDepthNodeVisitor<DB::ActionsMatcher, true, false, boost::intrusive_ptr<DB::IAST> const>::doVisit(boost::intrusive_ptr<DB::IAST> const&) @ 0x00000000240bbb9c
16. src/Interpreters/InDepthNodeVisitor.h:61:9: DB::ExpressionAnalyzer::getRootActions(boost::intrusive_ptr<DB::IAST> const&, bool, DB::ActionsDAG&, bool) @ 0x000000002409f9ce
17. src/Interpreters/ExpressionAnalyzer.cpp:1655:5: DB::SelectQueryExpressionAnalyzer::appendSelect(DB::ExpressionActionsChain&, bool) @ 0x00000000240ad655
18. src/Interpreters/ExpressionAnalyzer.cpp:2246:24: DB::ExpressionAnalysisResult::ExpressionAnalysisResult(DB::SelectQueryExpressionAnalyzer&, std::shared_ptr<DB::StorageInMemoryMetadata const> const&, bool, bool, bool, std::shared_ptr<DB::FilterDAGInfo> const&, std::shared_ptr<DB::FilterDAGInfo> const&, DB::Block const&) @ 0x00000000240b3526
19. src/Interpreters/InterpreterSelectQuery.cpp:1281:23: DB::InterpreterSelectQuery::getSampleBlockImpl() @ 0x0000000024238129
20. src/Interpreters/InterpreterSelectQuery.cpp:1020:55: DB::InterpreterSelectQuery::InterpreterSelectQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context> const&, std::optional<DB::Pipe>, std::shared_ptr<DB::IStorage> const&, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&, std::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::shared_ptr<DB::PreparedSets>)::$_0::operator()(bool) const @ 0x000000002422dc21
21. src/Interpreters/InterpreterSelectQuery.cpp:1030:5: DB::InterpreterSelectQuery::InterpreterSelectQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context> const&, std::optional<DB::Pipe>, std::shared_ptr<DB::IStorage> const&, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&, std::shared_ptr<DB::StorageInMemoryMetadata const> const&, std::shared_ptr<DB::PreparedSets>) @ 0x0000000024227b52
22. src/Interpreters/InterpreterSelectQuery.cpp:337:7: DB::InterpreterSelectQuery::InterpreterSelectQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context> const&, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&) @ 0x000000002422319e
23. contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:770:30: DB::InterpreterSelectWithUnionQuery::buildCurrentChildInterpreter(boost::intrusive_ptr<DB::IAST> const&, std::vector<String, std::allocator<String>> const&) @ 0x000000002426d8d3
24. src/Interpreters/InterpreterSelectWithUnionQuery.cpp:183:13: DB::InterpreterSelectWithUnionQuery::InterpreterSelectWithUnionQuery(boost::intrusive_ptr<DB::IAST> const&, std::shared_ptr<DB::Context>, DB::SelectQueryOptions const&, std::vector<String, std::allocator<String>> const&) @ 0x000000002426c14e
25. contrib/llvm-project/libcxx/include/__memory/unique_ptr.h:770:30: std::unique_ptr<DB::IInterpreter, std::default_delete<DB::IInterpreter>> std::__function::__policy_func<std::unique_ptr<DB::IInterpreter, std::default_delete<DB::IInterpreter>> (DB::InterpreterFactory::Arguments const&)>::__call_func[abi:fqe220101]<DB::registerInterpreterSelectWithUnionQuery(DB::InterpreterFactory&)::$_0>(std::__function::__policy_storage const*, DB::InterpreterFactory::Arguments const&) @ 0x000000002426fcad
26. contrib/llvm-project/libcxx/include/__functional/function.h:502:12: ? @ 0x000000002420a395
27. src/Interpreters/executeQuery.cpp:1800:66: DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum, std::unique_ptr<DB::ReadBuffer, std::default_delete<DB::ReadBuffer>>&, boost::intrusive_ptr<DB::IAST>&, std::shared_ptr<DB::ImplicitTransactionControlExecutor>, std::function<void ()>, DB::QueryResultDetails&) @ 0x000000002468dbb4
28. src/Interpreters/executeQuery.cpp:2339:11: DB::executeQuery(std::basic_string_view<char, std::char_traits<char>>, std::shared_ptr<DB::Context>, DB::QueryFlags, DB::QueryProcessingStage::Enum) @ 0x0000000024687bc3
29. src/Server/TCPHandler.cpp:889:68: DB::TCPHandler::runImpl() @ 0x000000002e689f8f
30. src/Server/TCPHandler.cpp:3210:9: DB::TCPHandler::run() @ 0x000000002e6ad12b
31. base/poco/Net/src/TCPServerConnection.cpp:40:3: Poco::Net::TCPServerConnection::start() @ 0x0000000035976b47

I guess it's #109747

The second failure is known flaky, created the issue: #109992

filimonov and others added 2 commits July 10, 2026 19:02
`EXPLAIN ANALYZE` (added on master) created its borrowed accounting group with
`std::make_shared<ThreadGroup>(outer_thread_group)`, but this branch makes the
borrowing `ThreadGroup` constructor private so borrowed groups are created only
through factories. The clean merge therefore failed to compile with `calling a
private constructor of class 'DB::ThreadGroup'` in `InterpreterExplainQuery.cpp`.

Add a `createForExplainAnalyze` factory (matching `createForMaterializedView` /
`createForFlushAsyncInsertQueue`) and use it at the call site.

CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109891&sha=966af26cfddc8ac9295d63d83ed671f2789be86f&name_0=PR&name_1=Fast%20test

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…wed-threadgroup-async-uaf-fix

Conflict in src/Interpreters/InterpreterExplainQuery.cpp: master reverted the
`EXPLAIN ANALYZE` feature in 93ee2f3 ("Revert 'Explain analyze'", PR ClickHouse#106586),
removing the borrowed-`ThreadGroup` caller that our build fix added
`createForExplainAnalyze` for. Followed the revert: took master's version of the
file and dropped the now-orphaned `createForExplainAnalyze` factory.
@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.90% 85.80% -0.10%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 169/170 (99.41%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Jul 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @vdimir

Hey @groeneai, please investigate the root cause of this crash, verify whether @vdimir is the right person to fix it, and suggest a solution.

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @KochetovNicolai

Hey @groeneai, please investigate the root cause of this crash, verify whether @KochetovNicolai is the right person to fix it, and suggest a solution.

@groeneai

Copy link
Copy Markdown
Collaborator

Investigated per the crash-classifier ping. Short version: the flagged crash is not caused by this PR, and both prior bot inline concerns are now resolved.

Flagged crash STID 2508-2ef2 is unrelated to this PR. Logical error: '!part.empty()' fires in FunctionBinaryArithmetic<MultiplyImpl>::executeImpl2 via executeImplDryRun (scalar arithmetic constant-folding dry run), per the AST fuzzer report. This PR touches only src/Common/ThreadGroupSwitcher.*, ThreadStatus.h, threadPoolCallbackRunner.h, ThreadStatusExt.cpp and tests, nothing under src/Functions/. The STID is a pre-existing trunk fuzzer finding: 39 hits across 23 unrelated PRs in the last 30 days. It happened to reproduce on this run. So by component the crash belongs to scalar functions / arithmetic, not thread-group management, and it should not block this PR.

Owner verification. For the fuzzer STID above, the classifier's route to comp-query-execution is a misattribution; that crash is arithmetic dry-run. For the PR change itself (borrowed-ThreadGroup async lifetime), comp-query-execution / thread accounting is correct, so @ vdimir is a reasonable reviewer, and @ azat already merged the prior iterations (#108988, #108887) and is engaged.

UAF root cause (for the record). Borrowed groups (createForMaterializedView / createForFlushAsyncInsertQueue) alias the parent query group's performance_counters / memory_tracker via raw pointers, valid only while the parent lives. An async callback that captured such a group at runner-construction time could re-attach it on a pool thread after the parent query finished and its group was freed, dereferencing freed parent counters. The fix marks borrowed groups (isBorrowed), drops them to nullptr in getCurrentThreadGroupForAsyncCallback, and captures the group at enqueue time instead of runner-construction. Correct scoping.

Inline concern 1 (memory accounting skew): safe, agree with @ filimonov. A nullptr group routes frees to total_memory_tracker, the global parent of every per-query tracker (CurrentThread::getMemoryTracker() returns nullptr with no group, so CurrentMemoryTracker::free falls through to global). Nothing is lost globally; only per-query attribution is briefly off for async-freed blocks. A true reparent is not practical (MemoryTracker is not per-object), and the alternatives are worse (parent-lifetime extension risks a leak; weak_ptr routing adds a layer). Resolved.

Inline concern 2 (EXPLAIN ANALYZE under-reports peak memory): now moot. The 2026-07-14 merge into this branch pulled in "Revert 'Explain analyze' (#106586)" (93ee2f3, merged to master 07-12). EXPLAIN ANALYZE, createForExplainAnalyze, analyze_thread_group and the getPeak()-based reporting are all gone from current HEAD, so there is no analysis group to under-report. The temporary createForExplainAnalyze factory added only to fix the post-merge build is gone too. No action needed.

Net: the fix is sound and correctly scoped; the flagged fuzzer STID is a separate pre-existing arithmetic issue.

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @davenger

Hey @groeneai, please investigate the root cause of this crash, verify whether @davenger is the right person to fix it, and suggest a solution.

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @novikd

Hey @groeneai, please investigate the root cause of this crash, verify whether @novikd is the right person to fix it, and suggest a solution.

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @vdimir

Hey @groeneai, please investigate the root cause of this crash, verify whether @vdimir is the right person to fix it, and suggest a solution.

1 similar comment
@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @vdimir

Hey @groeneai, please investigate the root cause of this crash, verify whether @vdimir is the right person to fix it, and suggest a solution.

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

🎉 Crash classifier applied: comp-query-execution, the issue goes to @davenger

Hey @groeneai, please investigate the root cause of this crash, verify whether @davenger is the right person to fix it, and suggest a solution.

@azat azat unassigned novikd and yariks5s Aug 12, 2026
…witch as memory_tracker

`ProcessList::insert` re-points the current group's `memory_tracker` at the
inserted query's user, but the `ProfileEvents` carrier went through
`setUserCounters`, whose chain-walk lands on the OUTER query's counters for a
borrowed `createForFlushAsyncInsertQueue` group and rebinds them to the flushed
user - so a `SYSTEM FLUSH ASYNC INSERT QUEUE` by user A flushing inserts of
user B stole A's later user-level `ProfileEvents` (visible in
`system.user_processes`), hopping between users when one wrapper query flushed
several. A borrowed group now re-points its own counters at the user, exactly
like `memory_tracker`.

New gtest `FlushForAnotherUserDoesNotStealFlushingUsersProfileEvents` asserts
through `ProcessList::getUserInfo` (the backing of `system.user_processes`)
that the wrapper query's events stay with user A and the flush's with user B;
verified to fail without the fix and to compile against the merge base.
Comment thread src/Interpreters/ProcessList.cpp Resolved
@groeneai

Copy link
Copy Markdown
Collaborator

Fix for #114367 is up: #114506.

Root cause: a QueryRunner table started its threads workers at CREATE and each parked forever on an empty queue. A ThreadPool worker is itself a job on the global pool, so those slots were held for the table's lifetime and the pool's idle-reclaim path was never reached. Twelve idle threads = 1024 tables exhaust the 10000-slot pool, which is what produced the CANNOT_SCHEDULE_TASK storm here.

The private queue and the parked-worker layer are gone; each query is scheduled on the pool directly, so an idle table holds no threads. threads keeps its meaning and the capacity bound stays threads + max_queue_size.

…entity switch as memory_tracker"

This reverts commit 10b764f.

The conditional re-pointing of a borrowed group's own counters in
`ProcessList::insert` detached the async-insert flush group from the
wrapper query's counters, so a `SYSTEM FLUSH ASYNC INSERT QUEUE` query
stopped accumulating the flush's `ProfileEvents` - breaking
`03779_async_insert_quorum` reproducibly in 8 stateless CI jobs
(`quorum_parts: 0` instead of `2`): this attribution to the wrapper
query is deliberate master behavior, introduced together with the test
by commit `7dfe727452a1` ("quorum with async inserts").

The cross-user `ProfileEvents` rebinding that the reverted commit tried
to fix is pre-existing on master - the flush group's counters chain and
the `setUserCounters` walk are identical there - and is out of scope for
this PR; fixing it requires a design that preserves the wrapper-query
attribution asserted by `03779_async_insert_quorum`.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109891&sha=10b764f1b81803abddd132cd4ab73d720c4cf6e8&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29
PR: ClickHouse#109891
…re order

`~ProcessListEntry` recorded a query group with live async-callback
companions into `lingering_query_groups` only when that query was the
user's last: a query whose borrowed-scope async work was still pending
slipped through when another query of the same user was still running,
and the later exit of that other query reset `user_memory_tracker` -
clearing `max_memory_usage_for_user` for exactly the pending async work.

Record the departing group whenever it has live companions, keeping the
reset attempt itself gated on the user's query list draining. The list
is pruned on every append (factored into `pruneLingeringQueryGroups`), so
it cannot grow for a user whose query count never drains to zero.

New gtest `UserTrackerResetDeferredWhenQueryWithAsyncWorkLeavesFirst`:
two same-user queries, the one with gated late async work leaves first;
asserts the user limit still applies to the late allocation. Verified to
fail without this fix and to pass with it; all 16 borrowed-group gtests
pass.

PR: ClickHouse#109891
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Status update (2026-08-13), pushed 84e984af78c3:

  • Fixed the real regression behind the 8 red stateless jobs (03779_async_insert_quorum, quorum_parts: 0 instead of 2): reverted 10b764f1b818 in af757e32adef. Re-pointing the borrowed flush group's own ProfileEvents counters detached it from the wrapper SYSTEM FLUSH ASYNC INSERT QUEUE query's counters, breaking the attribution that master deliberately added in 7dfe727452a1 together with the test. The cross-user rebinding that commit tried to fix is identical on master (pre-existing, out of scope here) — details on the inline thread. Verified the test now passes 4/4 locally in a stateless-config environment.
  • Addressed the new review Major (lingering user-tracker reset depended on query departure order) in 84e984af78c3, with the requested two-query regression gtest — verified to fail without the fix; all 16 borrowed-group gtests pass.
  • Merged master (61f10b7b7d0a) — cures the inconclusive Bugfix validation (unit tests) red, which came from submodule drift (contrib/AMQP-CPP, contrib/llvm-project, contrib/musl) between the merge base and the checkout; the PR touches no submodules.
  • AST fuzzer (amd_debug, targeted, old_compatibility) — Logical error: 'IDENTIFIER is not a table expression' in RemoveUnusedProjectionColumnsPass on a fuzzed remote(view(...)) ... RIGHT JOIN ... USING: unrelated to this PR (thread-group accounting); it hits many PRs and master itself in CI history, and the exact fix is already in flight in Keep a table function argument marked unresolved when the node is analyzed twice #113400.

Remaining: fresh CI on the new revision, then human merge.

Comment thread src/Interpreters/ThreadStatusExt.cpp Resolved
Comment thread src/Common/threadPoolCallbackRunner.h Resolved
Comment thread src/Interpreters/ProcessList.cpp Resolved
Normalize `ExternalLoader` async captures through `getCurrentThreadGroupForAsyncCallback`, so an on-demand external load started from a borrowed scope cannot retain raw borrowed accounting pointers.

When the final async-callback companion is released, retry the deferred `ProcessList` user-tracker reset. This clears stale user peak memory without resetting trackers while late work can still charge them. Add regression coverage for the completion-time cleanup.

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

Too complex, let me take a look

@azat

azat commented Aug 15, 2026

Copy link
Copy Markdown
Member

Will be superseded by #114940

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-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants