diff --git a/src/ipc/capnp/protocol.cpp b/src/ipc/capnp/protocol.cpp index 64b1e9a9f19..e7eaf643011 100644 --- a/src/ipc/capnp/protocol.cpp +++ b/src/ipc/capnp/protocol.cpp @@ -94,7 +94,7 @@ public: void serve(interfaces::Init& init, const std::function& make_stream) override { assert(!m_loop); - mp::g_thread_context.thread_name = mp::ThreadName(m_exe_name); + mp::CurrentThread().thread_name = mp::ThreadName(m_exe_name); mp::LogOptions opts = { .log_fn = IpcLogFn, .log_level = GetRequestedIPCLogLevel() diff --git a/src/ipc/test/fuzz/ipc.cpp b/src/ipc/test/fuzz/ipc.cpp index 1c19faf258a..5935e9afb51 100644 --- a/src/ipc/test/fuzz/ipc.cpp +++ b/src/ipc/test/fuzz/ipc.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -78,10 +79,10 @@ static void initialize_ipc() static const auto testing_setup = MakeNoLogFileContext<>(); (void)testing_setup; - // Ensure g_thread_context is destroyed after the IPC setup, since C++ - // destroys thread_local objects in reverse construction order. - mp::ThreadContext& thread_context{mp::g_thread_context}; - (void)thread_context; + // Ensure the thread's ThreadContext is created before the IPC setup, so + // it is destroyed after it, since C++ destroys thread_local objects in + // reverse construction order. + mp::CurrentThread(); thread_local static IpcFuzzSetup ipc; // NOLINT(bitcoin-nontrivial-threadlocal) g_ipc = &ipc; diff --git a/src/ipc/util.h b/src/ipc/util.h index 3fd3ff160f3..6352f981746 100644 --- a/src/ipc/util.h +++ b/src/ipc/util.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -52,6 +53,11 @@ inline SocketId StartSpawned(const std::string& connect_info) if (!socket) throw std::invalid_argument(strprintf("Invalid socket descriptor '%s'", connect_info)); return *socket; } + +inline ThreadContext& CurrentThread() +{ + return g_thread_context; +} #endif } // namespace mp