Repository navigation
Fix several critical bugs around ThreadPoolCallbackRunnerLocal usage - #95818
Conversation
|
Workflow [PR], commit [7d391a5] Summary: ❌
|
| for (const auto & table_name : table_infos | boost::adaptors::map_keys) | ||
| /// Using a lambda with references is fine, since it only uses `this` and `it.first` which is part of table_infos (`this`) | ||
| /// So they will outlive runner even if an exception is thrown | ||
| for (const auto & it : table_infos) |
There was a problem hiding this comment.
This was fine, but I changed it so it was clear where the ownership comes from. Having operator| with boost made things more complex to understand
| runner.enqueueAndKeepTrack( | ||
| [=, &tables, &mutex, this] | ||
| /// Ensure tables and mutex (capture by reference) outlive runner | ||
| DB::ThreadPoolCallbackRunnerLocal<void> runner(pool, DB::ThreadName::DATALAKE_REST_CATALOG); |
There was a problem hiding this comment.
This fixes a critical bug. tables and mutex had a shorter live than runner, so in case of an exception they would be destroyed before ~runner was called.
| } | ||
| } | ||
|
|
||
| ThreadPoolCallbackRunnerLocal<void> runner(pool, ThreadName::PLAIN_REWRITABLE_META_LOAD); |
There was a problem hiding this comment.
This also fixes another critical issue. Log was used by reference but it had a shorter life span than runner
| /// remote_layout_mutex: Same | ||
| /// In any case we have a try {} catch (...) around runner usage, so exceptions will call runner.waitForAllToFinish() first | ||
| /// Thus the order of destruction of the variables is not important | ||
| runner.enqueueAndKeepTrack([remote_path, object_path = file->getPath(), metadata = file->metadata, &log, &settings, this, &remote_layout, &remote_layout_mutex] |
| /// is called first and all tasks stopped before local variables are being destroyed. | ||
| ThreadPoolCallbackRunnerLocal<void> runner(pool, ThreadName::MERGETREE_PREWARM_CACHE); | ||
|
|
||
| /// Created first, so it outlives runner |
There was a problem hiding this comment.
Another critical issue here. enough_space would be destroyed before runner
| for (size_t row = 0; row < rows; ++row) | ||
| { | ||
| auto key = static_cast<USearchIndex::vector_key_t>(index_size + row); | ||
| /// Passing add_vector_to_index by refence is safe because it outlives the runner |
There was a problem hiding this comment.
Another critical issue. This one closes #95739
|
|
||
| const auto & disks = data_volume->getDisks(); | ||
|
|
||
| const auto & paths = getDataPaths(); |
There was a problem hiding this comment.
Another critical issue here. These variables would be destroyed before runner in case of an exception
|
|
||
| missing_parts.clear(); | ||
|
|
||
| std::mutex missing_parts_mutex; /// Must be created before runner, so it outlives it |
There was a problem hiding this comment.
And another one. The mutex needs to outlive fetch_partition_runner
There was a problem hiding this comment.
Pull request overview
This PR addresses critical lifetime bugs in ThreadPoolCallbackRunnerLocal usage across the codebase. The issue stems from C++ stack unwinding behavior during exceptions: variables declared after the runner are destroyed before it, causing use-after-scope bugs when tasks reference them.
Changes:
- Reordered variable declarations to ensure captured references outlive
ThreadPoolCallbackRunnerLocalinstances - Added explicit capture lists to lambdas instead of implicit
[&]captures - Added explanatory comments documenting lifetime requirements
- Enhanced
threadPoolCallbackRunner.hwith comprehensive safety warnings and deleted overloads to catchstd::reference_wrapperat compile-time
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Common/threadPoolCallbackRunner.h | Added safety documentation and compile-time checks for reference captures |
| src/Storages/StorageDistributed.cpp | Moved paths and last_increment declarations before runner creation |
| src/Storages/StorageReplicatedMergeTree.cpp | Moved missing_parts_mutex before runner and made lambda captures explicit |
| src/Storages/StorageBuffer.cpp | Made lambda captures explicit to avoid implicit reference captures |
| src/Storages/MergeTree/MergeTreeIndexVectorSimilarity.cpp | Moved runner creation after lambda definition |
| src/Storages/MergeTree/MergeTreeData.cpp | Made lambda captures explicit and added lifetime documentation |
| src/Storages/System/StorageSystemDetachedParts.cpp | Added comment explaining safe reference capture |
| src/Interpreters/InterpreterDropQuery.cpp | Made lambda captures explicit with lifetime documentation |
| src/Interpreters/DatabaseCatalog.cpp | Removed implicit [&] capture |
| src/Interpreters/ClusterProxy/distributedIndexAnalysis.cpp | Made lambda captures explicit |
| src/Interpreters/Aggregator.cpp | Made lambda captures explicit with lifetime documentation |
| src/Disks/IDisk.cpp | Added comments explaining recursive capture safety |
| src/Disks/DiskObjectStorage/MetadataStorages/PlainRewritable/MetadataStorageFromPlainRewritableObjectStorage.cpp | Moved runner creation after variable declarations |
| src/Dictionaries/PolygonDictionaryUtils.h | Scoped runner with braces and made captures explicit |
| src/Databases/DatabaseReplicated.cpp | Made lambda captures explicit with lifetime documentation |
| src/Databases/DatabaseBackup.cpp | Scoped runner with braces to control lifetime |
| src/Databases/DataLake/RestCatalog.cpp | Scoped runner with braces ensuring tables and mutex outlive it |
| src/Backups/BackupsWorker.cpp | Made lambda captures explicit with lifetime documentation |
| src/Backups/BackupFileInfo.cpp | Added comment explaining safe reference captures |
| src/Backups/BackupEntriesCollector.cpp | Changed iteration pattern to avoid boost adaptors and made captures safer |
| src/AggregateFunctions/UniqExactSet.h | Added comment explaining safe reference usage |
| /// When creating a runner on stack, you MUST make sure that it's created (and destroyed) before local objects captured by task lambda. | ||
|
|
||
| /// ============================================================================ | ||
| /// CRITICAL SAFETY WARNING: DO NOT CAPTURE LOCAL VARIABLES BY REFERENCE! |
There was a problem hiding this comment.
Corrected 'refernce' to 'reference' in line 360 comment.
| for (size_t row = 0; row < rows; ++row) | ||
| { | ||
| auto key = static_cast<USearchIndex::vector_key_t>(index_size + row); | ||
| /// Passing add_vector_to_index by refence is safe because it outlives the runner |
There was a problem hiding this comment.
Corrected 'refence' to 'reference'.
| /// Passing add_vector_to_index by refence is safe because it outlives the runner | |
| /// Passing add_vector_to_index by reference is safe because it outlives the runner |
| if (runner) | ||
| { | ||
| runner->enqueueAndKeepTrack([&]() | ||
| /// Pasing buf as a reference is fine since it's a reference to this, which outlives the runner |
There was a problem hiding this comment.
Corrected 'Pasing' to 'Passing'.
|
Please also review the changes in the sync PR, there are more fixes there |
9e5869e
|
The main fix that was causing issues was introduced in #96259, so I'm going to remove backports from this as there aren't reports of issues |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix several critical bugs around ThreadPoolCallbackRunnerLocal usage
This PR addresses critical memory safety bugs around
ThreadPoolCallbackRunnerLocalusage throughout the codebase. The code issue is that when lambdas capture local variables by reference ([&]) and an exception occurs during the enqueue loop, stack unwinding happens before the destructor'swaitForAllToFinish()is called, causing use-after-scope bugs when worker threads access destroyed stack variables.Fixes #95739
I'll do the same for the usages in private too
Documentation entry for user-facing changes
Version info
26.2.1.513