https://clickhouse-test-reports.s3.yandex.net/27793/ac5ac0a106a6c2217210bb358e3d8e97ed3a898d/stress_test_(thread).html
https://clickhouse-test-reports.s3.yandex.net/27793/ac5ac0a106a6c2217210bb358e3d8e97ed3a898d/stress_test_(thread)/stderr.log

```
==================
WARNING: ThreadSanitizer: use of an invalid mutex (e.g. uninitialized or destroyed) (pid=440)
    #0 pthread_mutex_lock <null> (clickhouse+0x9847b76)
    #1 Poco::MutexImpl::lockImpl() obj-x86_64-linux-gnu/../contrib/poco/Foundation/include/Poco/Mutex_POSIX.h:59:6 (clickhouse+0x9a369e0)
    #2 Poco::Mutex::lock() obj-x86_64-linux-gnu/../contrib/poco/Foundation/include/Poco/Mutex.h:207:2 (clickhouse+0x18db4523)
    #3 Poco::ScopedLock<Poco::Mutex>::ScopedLock(Poco::Mutex&) obj-x86_64-linux-gnu/../contrib/poco/Foundation/include/Poco/ScopedLock.h:38:10 (clickhouse+0x18db4523)
    #4 Poco::Logger::get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) obj-x86_64-linux-gnu/../contrib/poco/Foundation/src/Logger.cpp:285:20 (clickhouse+0x18db4523)
    #5 DB::tryLogCurrentException(char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) obj-x86_64-linux-gnu/../src/Common/Exception.cpp:183:33 (clickhouse+0x9913c6f)
    #6 DB::tryLogException(std::exception_ptr, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) obj-x86_64-linux-gnu/../src/Common/Exception.cpp:440:9 (clickhouse+0x9916356)
    #7 ThreadPoolImpl<std::__1::thread>::~ThreadPoolImpl() obj-x86_64-linux-gnu/../src/Common/ThreadPool.cpp:197:9 (clickhouse+0x99436e5)
    #8 std::__1::default_delete<GlobalThreadPool>::operator()(GlobalThreadPool*) const obj-x86_64-linux-gnu/../contrib/libcxx/include/memory:1397:5 (clickhouse+0x9940319)
    #9 std::__1::unique_ptr<GlobalThreadPool, std::__1::default_delete<GlobalThreadPool> >::reset(GlobalThreadPool*) obj-x86_64-linux-gnu/../contrib/libcxx/include/memory:1658:7 (clickhouse+0x9940319)
    #10 std::__1::unique_ptr<GlobalThreadPool, std::__1::default_delete<GlobalThreadPool> >::~unique_ptr() obj-x86_64-linux-gnu/../contrib/libcxx/include/memory:1612:19 (clickhouse+0x9940319)
    #11 cxa_at_exit_wrapper(void*) <null> (clickhouse+0x986748f)

  Location is global 'Poco::Logger::_mapMtx' of size 40 at 0x00001c613328 (clickhouse+0x00001c613328)

  Mutex M0 (0x00001c613328) created at:
    [failed to restore the stack]

SUMMARY: ThreadSanitizer: use of an invalid mutex (e.g. uninitialized or destroyed) (/usr/bin/clickhouse+0x9847b76) in pthread_mutex_lock
==================
```

Looking at the backtrace it seems that `_mapMtx` is destroyed before `GlobalThreadPool` so when `GlobalThreadPool -> ThreadPoolImpl` tries to log an exception and needs to get a logger it ends up using an invalid mutex.

I'm not sure how to reproduce it, but a solution could be to force `_mapMtx` to be initialized before `GlobalThreadPool::the_instance` as they will be destroyed in reverse order.
}
            else
            {
                
                return;
            }
        }
        if (!need_shutdown)
        {
            try
            {
                ALLOW_ALLOCATIONS_IN_SCOPE;
                CurrentMetrics::Increment metric_active_threads(
                    std::is_same_v<Thread, std::thread> ? CurrentMetrics::GlobalThreadActive : CurrentMetrics::LocalThreadActive);
                job();
                
                
                job = {};
            }
            catch (...)
            {
                ALLOW_ALLOCATIONS_IN_SCOPE;
                
                
                job = {};
                {