diff --git a/src/Common/ThreadPool.h b/src/Common/ThreadPool.h index ecb03dd9659..4f08427b4b9 100644 --- a/src/Common/ThreadPool.h +++ b/src/Common/ThreadPool.h @@ -163,24 +163,19 @@ public: func = std::forward(func), args = std::make_tuple(std::forward(args)...)] { - try - { - /// Move capture in order to destroy it before `state->set()`. - /// It will guarantee that after ThreadFromGlobalPool::join all resources are destroyed. - auto function = std::move(func); - auto arguments = std::move(args); + SCOPE_EXIT({ + /// Destroy function before exit. + /// It will guarantee that after ThreadFromGlobalPool::join all captured params are destroyed. + func = {}; + state->set(); + }); - /// Thread status holds raw pointer on query context, thus it always must be destroyed - /// before sending signal that permits to join this thread. - DB::ThreadStatus thread_status; - std::apply(function, arguments); - } - catch (...) - { - state->set(); - throw; - } - state->set(); + auto arguments = std::move(args); + + /// Thread status holds raw pointer on query context, thus it always must be destroyed + /// before sending signal that permits to join this thread. + DB::ThreadStatus thread_status; + std::apply(func, arguments); }); }