https://s3.amazonaws.com/clickhouse-test-reports/38541/9b4da86e2e5cf74a8b5b1205bbabc909e357d654/stress_test__thread__actions_.html

```
==================
WARNING: ThreadSanitizer: data race (pid=644)
  Read of size 8 at 0x7f6bea8098b0 by thread T298:
    #0 std::__1::unique_ptr<DB::CompletedPipelineExecutor::Data, std::__1::default_delete<DB::CompletedPipelineExecutor::Data> >::operator*() const build_docker/../contrib/libcxx/include/__memory/unique_ptr.h:280:20 (clickhouse+0x1bc95bb8) (BuildId: 2e314254bf8f29c3)
    #1 DB::CompletedPipelineExecutor::execute()::$_0::operator()() const build_docker/../src/Processors/Executors/CompletedPipelineExecutor.cpp:77:28 (clickhouse+0x1bc95bb8)
    #2 decltype(static_cast<DB::CompletedPipelineExecutor::execute()::$_0&>(fp)()) std::__1::__invoke_constexpr<DB::CompletedPipelineExecutor::execute()::$_0&>(DB::CompletedPipelineExecutor::execute()::$_0&) build_docker/../contrib/libcxx/include/type_traits:3648:23 (clickhouse+0x1bc95bb8)
    #3 decltype(auto) std::__1::__apply_tuple_impl<DB::CompletedPipelineExecutor::execute()::$_0&, std::__1::tuple<>&>(DB::CompletedPipelineExecutor::execute()::$_0&, std::__1::tuple<>&, std::__1::__tuple_indices<>) build_docker/../contrib/libcxx/include/tuple:1595:1 (clickhouse+0x1bc95bb8)
    #4 decltype(auto) std::__1::apply<DB::CompletedPipelineExecutor::execute()::$_0&, std::__1::tuple<>&>(DB::CompletedPipelineExecutor::execute()::$_0&, std::__1::tuple<>&) build_docker/../contrib/libcxx/include/tuple:1604:1 (clickhouse+0x1bc95bb8)
    #5 ThreadFromGlobalPool::ThreadFromGlobalPool<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'()::operator()() build_docker/../src/Common/ThreadPool.h:188:13 (clickhouse+0x1bc95bb8)
    #6 decltype(static_cast<DB::CompletedPipelineExecutor::execute()::$_0>(fp)()) std::__1::__invoke<ThreadFromGlobalPool::ThreadFromGlobalPool<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'()&>(DB::CompletedPipelineExecutor::execute()::$_0&&) build_docker/../contrib/libcxx/include/type_traits:3640:23 (clickhouse+0x1bc95bb8)
    #7 void std::__1::__invoke_void_return_wrapper<void, true>::__call<ThreadFromGlobalPool::ThreadFromGlobalPool<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'()&>(ThreadFromGlobalPool::ThreadFromGlobalPool<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'()&) build_docker/../contrib/libcxx/include/__functional/invoke.h:61:9 (clickhouse+0x1bc95bb8)
    #8 std::__1::__function::__default_alloc_func<ThreadFromGlobalPool::ThreadFromGlobalPool<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'(), void ()>::operator()() build_docker/../contrib/libcxx/include/__functional/function.h:230:12 (clickhouse+0x1bc95bb8)
    #9 void std::__1::__function::__policy_invoker<void ()>::__call_impl<std::__1::__function::__default_alloc_func<ThreadFromGlobalPool::ThreadFromGlobalPool<DB::CompletedPipelineExecutor::execute()::$_0>(DB::CompletedPipelineExecutor::execute()::$_0&&)::'lambda'(), void ()> >(std::__1::__function::__policy_storage const*) build_docker/../contrib/libcxx/include/__functional/function.h:711:16 (clickhouse+0x1bc95bb8)
    #10 std::__1::__function::__policy_func<void ()>::operator()() const build_docker/../contrib/libcxx/include/__functional/function.h:843:16 (clickhouse+0xb4745f7) (BuildId: 2e314254bf8f29c3)
    #11 std::__1::function<void ()>::operator()() const build_docker/../contrib/libcxx/include/__functional/function.h:1184:12 (clickhouse+0xb4745f7)
    #12 ThreadPoolImpl<std::__1::thread>::worker(std::__1::__list_iterator<std::__1::thread, void*>) build_docker/../src/Common/ThreadPool.cpp:281:17 (clickhouse+0xb4745f7)
    #13 void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'()::operator()() const build_docker/../src/Common/ThreadPool.cpp:143:73 (clickhouse+0xb477d30) (BuildId: 2e314254bf8f29c3)
    #14 decltype(static_cast<void>(fp)()) std::__1::__invoke<void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'()>(void&&) build_docker/../contrib/libcxx/include/type_traits:3640:23 (clickhouse+0xb477d30)
    #15 void std::__1::__thread_execute<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'()>(std::__1::tuple<void, void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'()>&, std::__1::__tuple_indices<>) build_docker/../contrib/libcxx/include/thread:282:5 (clickhouse+0xb477d30)
    #16 void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'()> >(void*) build_docker/../contrib/libcxx/include/thread:293:5 (clickhouse+0xb477d30)

  Previous write of size 8 at 0x7f6bea8098b0 by thread T1640:
    #0 std::__1::unique_ptr<DB::CompletedPipelineExecutor::Data, std::__1::default_delete<DB::CompletedPipelineExecutor::Data> >::reset(DB::CompletedPipelineExecutor::Data*) build_docker/../contrib/libcxx/include/__memory/unique_ptr.h:313:20 (clickhouse+0x1bc95831) (BuildId: 2e314254bf8f29c3)
    #1 std::__1::unique_ptr<DB::CompletedPipelineExecutor::Data, std::__1::default_delete<DB::CompletedPipelineExecutor::Data> >::~unique_ptr() build_docker/../contrib/libcxx/include/__memory/unique_ptr.h:269:19 (clickhouse+0x1bc95831)
    #2 DB::CompletedPipelineExecutor::~CompletedPipelineExecutor() build_docker/../src/Processors/Executors/CompletedPipelineExecutor.cpp:113:1 (clickhouse+0x1bc95831)
    #3 DB::TCPHandler::runImpl() build_docker/../src/Server/TCPHandler.cpp:376:13 (clickhouse+0x1bc2658b) (BuildId: 2e314254bf8f29c3)
    #4 DB::TCPHandler::run() build_docker/../src/Server/TCPHandler.cpp:1797:9 (clickhouse+0x1bc38b07) (BuildId: 2e314254bf8f29c3)
    #5 Poco::Net::TCPServerConnection::start() build_docker/../contrib/poco/Net/src/TCPServerConnection.cpp:43:3 (clickhouse+0x1ecb8c82) (BuildId: 2e314254bf8f29c3)
    #6 Poco::Net::TCPServerDispatcher::run() build_docker/../contrib/poco/Net/src/TCPServerDispatcher.cpp:115:20 (clickhouse+0x1ecb94f2) (BuildId: 2e314254bf8f29c3)
    #7 Poco::PooledThread::run() build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:199:14 (clickhouse+0x1ef047d5) (BuildId: 2e314254bf8f29c3)
    #8 Poco::(anonymous namespace)::RunnableHolder::run() build_docker/../contrib/poco/Foundation/src/Thread.cpp:55:11 (clickhouse+0x1ef02b0f) (BuildId: 2e314254bf8f29c3)
    #9 Poco::ThreadImpl::runnableEntry(void*) build_docker/../contrib/poco/Foundation/src/Thread_POSIX.cpp:345:27 (clickhouse+0x1ef0124b) (BuildId: 2e314254bf8f29c3)

  Location is stack of thread T1640.

  Thread T298 'ThreadPool' (tid=1088, running) created by thread T286 at:
    #0 pthread_create <null> (clickhouse+0xb31eb6d) (BuildId: 2e314254bf8f29c3)
    #1 std::__1::__libcpp_thread_create(unsigned long*, void* (*)(void*), void*) build_docker/../contrib/libcxx/include/__threading_support:375:10 (clickhouse+0xb477620) (BuildId: 2e314254bf8f29c3)
    #2 std::__1::thread::thread<void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'(), void>(void&&) build_docker/../contrib/libcxx/include/thread:309:16 (clickhouse+0xb477620)
    #3 void ThreadPoolImpl<std::__1::thread>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>) build_docker/../src/Common/ThreadPool.cpp:143:35 (clickhouse+0xb472de2) (BuildId: 2e314254bf8f29c3)
    #4 ThreadPoolImpl<std::__1::thread>::scheduleOrThrow(std::__1::function<void ()>, int, unsigned long) build_docker/../src/Common/ThreadPool.cpp:175:5 (clickhouse+0xb479032) (BuildId: 2e314254bf8f29c3)
    #5 ThreadFromGlobalPool::ThreadFromGlobalPool<void ThreadPoolImpl<ThreadFromGlobalPool>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>)::'lambda0'()>(void&&) build_docker/../src/Common/ThreadPool.h:169:38 (clickhouse+0xb479032)
    #6 void ThreadPoolImpl<ThreadFromGlobalPool>::scheduleImpl<void>(std::__1::function<void ()>, int, std::__1::optional<unsigned long>) build_docker/../src/Common/ThreadPool.cpp:143:35 (clickhouse+0xb4750e4) (BuildId: 2e314254bf8f29c3)
    #7 ThreadPoolImpl<ThreadFromGlobalPool>::scheduleOrThrowOnError(std::__1::function<void ()>, int) build_docker/../src/Common/ThreadPool.cpp:163:5 (clickhouse+0xb474ddf) (BuildId: 2e314254bf8f29c3)
    #8 DB::ParallelFormattingOutputFormat::scheduleFormatterThreadForUnitWithNumber(unsigned long, unsigned long) build_docker/../src/Processors/Formats/Impl/ParallelFormattingOutputFormat.h:251:14 (clickhouse+0x1bdb98f2) (BuildId: 2e314254bf8f29c3)
    #9 DB::ParallelFormattingOutputFormat::addChunk(DB::Chunk, DB::ParallelFormattingOutputFormat::ProcessingUnitType, bool) build_docker/../src/Processors/Formats/Impl/ParallelFormattingOutputFormat.cpp:66:9 (clickhouse+0x1bdb859a) (BuildId: 2e314254bf8f29c3)
    #10 DB::ParallelFormattingOutputFormat::writeSuffix() build_docker/../src/Processors/Formats/Impl/ParallelFormattingOutputFormat.h:126:9 (clickhouse+0x1bdbaa82) (BuildId: 2e314254bf8f29c3)
    #11 DB::IOutputFormat::writeSuffixIfNot() build_docker/../src/Processors/Formats/IOutputFormat.h:102:13 (clickhouse+0x1bcb4816) (BuildId: 2e314254bf8f29c3)
    #12 DB::IOutputFormat::finalize() build_docker/../src/Processors/Formats/IOutputFormat.cpp:124:5 (clickhouse+0x1bcb4816)
    #13 DB::IOutputFormat::work() build_docker/../src/Processors/Formats/IOutputFormat.cpp:75:9 (clickhouse+0x1bcb4816)
    #14 DB::executeJob(DB::ExecutingGraph::Node*, DB::ReadProgressCallback*) build_docker/../src/Processors/Executors/ExecutionThreadContext.cpp:47:26 (clickhouse+0x1bca4ad0) (BuildId: 2e314254bf8f29c3)
    #15 DB::ExecutionThreadContext::executeTask() build_docker/../src/Processors/Executors/ExecutionThreadContext.cpp:86:9 (clickhouse+0x1bca4ad0)
    #16 DB::PipelineExecutor::executeStepImpl(unsigned long, std::__1::atomic<bool>*) build_docker/../src/Processors/Executors/PipelineExecutor.cpp:222:26 (clickhouse+0x1bc97be0) (BuildId: 2e314254bf8f29c3)
    #17 DB::PipelineExecutor::executeSingleThread(unsigned long) build_docker/../src/Processors/Executors/PipelineExecutor.cpp:187:5 (clickhouse+0x1bc97098) (BuildId: 2e314254bf8f29c3)
    #18 DB::PipelineExecutor::executeImpl(unsigned long) build_docker/../src/Processors/Executors/PipelineExecutor.cpp:331:9 (clickhouse+0x1bc97098)
    #19 DB::PipelineExecutor::execute(unsigned long) build_docker/../src/Processors/Executors/PipelineExecutor.cpp:88:9 (clickhouse+0x1bc96998) (BuildId: 2e314254bf8f29c3)
    #20 DB::CompletedPipelineExecutor::execute() build_docker/../src/Processors/Executors/CompletedPipelineExecutor.cpp:98:18 (clickhouse+0x1bc955f7) (BuildId: 2e314254bf8f29c3)
    #21 DB::executeQuery(DB::ReadBuffer&, DB::WriteBuffer&, bool, std::__1::shared_ptr<DB::Context>, std::__1::function<void (std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)>, std::__1::optional<DB::FormatSettings> const&) build_docker/../src/Interpreters/executeQuery.cpp:1246:22 (clickhouse+0x1adc1482) (BuildId: 2e314254bf8f29c3)
    #22 DB::HTTPHandler::processQuery(DB::HTTPServerRequest&, DB::HTMLForm&, DB::HTTPServerResponse&, DB::HTTPHandler::Output&, std::__1::optional<DB::CurrentThread::QueryScope>&) build_docker/../src/Server/HTTPHandler.cpp:829:5 (clickhouse+0x1bbcedc4) (BuildId: 2e314254bf8f29c3)
    #23 DB::HTTPHandler::handleRequest(DB::HTTPServerRequest&, DB::HTTPServerResponse&) build_docker/../src/Server/HTTPHandler.cpp:972:9 (clickhouse+0x1bbd3067) (BuildId: 2e314254bf8f29c3)
    #24 DB::HTTPServerConnection::run() build_docker/../src/Server/HTTP/HTTPServerConnection.cpp:65:34 (clickhouse+0x1bc4250b) (BuildId: 2e314254bf8f29c3)
    #25 Poco::Net::TCPServerConnection::start() build_docker/../contrib/poco/Net/src/TCPServerConnection.cpp:43:3 (clickhouse+0x1ecb8c82) (BuildId: 2e314254bf8f29c3)
    #26 Poco::Net::TCPServerDispatcher::run() build_docker/../contrib/poco/Net/src/TCPServerDispatcher.cpp:115:20 (clickhouse+0x1ecb94f2) (BuildId: 2e314254bf8f29c3)
    #27 Poco::PooledThread::run() build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:199:14 (clickhouse+0x1ef047d5) (BuildId: 2e314254bf8f29c3)
    #28 Poco::(anonymous namespace)::RunnableHolder::run() build_docker/../contrib/poco/Foundation/src/Thread.cpp:55:11 (clickhouse+0x1ef02b0f) (BuildId: 2e314254bf8f29c3)
    #29 Poco::ThreadImpl::runnableEntry(void*) build_docker/../contrib/poco/Foundation/src/Thread_POSIX.cpp:345:27 (clickhouse+0x1ef0124b) (BuildId: 2e314254bf8f29c3)

  Thread T1640 'TCPHandler' (tid=6952, running) created by thread T250 at:
    #0 pthread_create <null> (clickhouse+0xb31eb6d) (BuildId: 2e314254bf8f29c3)
    #1 Poco::ThreadImpl::startImpl(Poco::SharedPtr<Poco::Runnable, Poco::ReferenceCounter, Poco::ReleasePolicy<Poco::Runnable> >) build_docker/../contrib/poco/Foundation/src/Thread_POSIX.cpp:202:6 (clickhouse+0x1ef00c82) (BuildId: 2e314254bf8f29c3)
    #2 Poco::Thread::start(Poco::Runnable&) build_docker/../contrib/poco/Foundation/src/Thread.cpp:128:2 (clickhouse+0x1ef0250c) (BuildId: 2e314254bf8f29c3)
    #3 Poco::PooledThread::start() build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:85:10 (clickhouse+0x1ef065e6) (BuildId: 2e314254bf8f29c3)
    #4 Poco::ThreadPool::getThread() build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:461:14 (clickhouse+0x1ef065e6)
    #5 Poco::ThreadPool::startWithPriority(Poco::Thread::Priority, Poco::Runnable&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) build_docker/../contrib/poco/Foundation/src/ThreadPool.cpp:365:2 (clickhouse+0x1ef069e7) (BuildId: 2e314254bf8f29c3)
    #6 Poco::Net::TCPServerDispatcher::enqueue(Poco::Net::StreamSocket const&) build_docker/../contrib/poco/Net/src/TCPServerDispatcher.cpp:152:17 (clickhouse+0x1ecb99ee) (BuildId: 2e314254bf8f29c3)
    #7 Poco::Net::TCPServer::run() build_docker/../contrib/poco/Net/src/TCPServer.cpp:148:21 (clickhouse+0x1ecb8507) (BuildId: 2e314254bf8f29c3)
    #8 Poco::(anonymous namespace)::RunnableHolder::run() build_docker/../contrib/poco/Foundation/src/Thread.cpp:55:11 (clickhouse+0x1ef02b0f) (BuildId: 2e314254bf8f29c3)
    #9 Poco::ThreadImpl::runnableEntry(void*) build_docker/../contrib/poco/Foundation/src/Thread_POSIX.cpp:345:27 (clickhouse+0x1ef0124b) (BuildId: 2e314254bf8f29c3)

SUMMARY: ThreadSanitizer: data race build_docker/../contrib/libcxx/include/__memory/unique_ptr.h:280:20 in std::__1::unique_ptr<DB::CompletedPipelineExecutor::Data, std::__1::default_delete<DB::CompletedPipelineExecutor::Data> >::operator*() const
==================
```

cc: @KochetovNicolai 
#include <Processors/Executors/CompletedPipelineExecutor.h>
#include <Processors/Executors/PipelineExecutor.h>
#include <QueryPipeline/QueryPipeline.h>
#include <QueryPipeline/ReadProgressCallback.h>
#include <Poco/Event.h>
#include <Common/setThreadName.h>
#include <Common/ThreadPool.h>
#include <iostream>
namespace DB
{
namespace ErrorCodes
{
    extern const int LOGICAL_ERROR;
}
struct CompletedPipelineExecutor::Data
{
    PipelineExecutorPtr executor;
    std::exception_ptr exception;
    std::atomic_bool is_finished = false;
    std::atomic_bool has_exception = false;
    ThreadFromGlobalPool thread;
    Poco::Event finish_event;
    ~Data()
    {
        if (thread.joinable())
            thread.join();
    }
};
static void threadFunction(CompletedPipelineExecutor::Data & data, ThreadGroupStatusPtr thread_group, size_t num_threads)