diff --git a/src/Storages/MergeTree/MergeTreeData.cpp b/src/Storages/MergeTree/MergeTreeData.cpp index 8ca976a343d..e79425b5398 100644 --- a/src/Storages/MergeTree/MergeTreeData.cpp +++ b/src/Storages/MergeTree/MergeTreeData.cpp @@ -2405,8 +2405,6 @@ void MergeTreeData::prewarmCaches(ThreadPool & pool, MarkCachePtr mark_cache, Pr return to_tuple(lhs) > to_tuple(rhs); }); - ThreadPoolCallbackRunnerLocal runner(pool, "PrewarmCaches"); - double marks_ratio_to_prewarm = getContext()->getServerSettings()[ServerSetting::mark_cache_prewarm_ratio]; double index_ratio_to_prewarm = getContext()->getServerSettings()[ServerSetting::primary_index_cache_prewarm_ratio]; @@ -2418,6 +2416,10 @@ void MergeTreeData::prewarmCaches(ThreadPool & pool, MarkCachePtr mark_cache, Pr columns_to_prewarm_marks = getColumnsToPrewarmMarks(*getSettings(), metadata_snaphost->getColumns().getAllPhysical()); } + /// Allocate runner on stack after all used local variables to make its destructor + /// is called first and all tasks stopped before local variables are being destroyed. + ThreadPoolCallbackRunnerLocal runner(pool, "PrewarmCaches"); + for (const auto & part : data_parts) { bool added_task = false;