From 9ac8fb205bbae3b6ae45ad5e69ad8efc687d9587 Mon Sep 17 00:00:00 2001 From: chenBright Date: Sun, 26 Oct 2025 15:11:23 +0800 Subject: [PATCH] Use SetNameSimple instead of SetName to avoid deadlock --- src/brpc/details/usercode_backup_pool.cpp | 2 +- src/bthread/execution_queue.cpp | 2 +- src/bthread/task_control.cpp | 2 +- src/bthread/timer_thread.cpp | 2 +- src/butil/threading/platform_thread.h | 1 + src/butil/threading/platform_thread_freebsd.cc | 5 +++++ src/butil/threading/platform_thread_linux.cc | 4 ++++ src/butil/threading/platform_thread_mac.mm | 5 +++++ src/bvar/collector.cpp | 4 ++-- src/bvar/detail/sampler.cpp | 2 +- src/bvar/variable.cpp | 2 +- 11 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/brpc/details/usercode_backup_pool.cpp b/src/brpc/details/usercode_backup_pool.cpp index 489def454c..338038ae39 100644 --- a/src/brpc/details/usercode_backup_pool.cpp +++ b/src/brpc/details/usercode_backup_pool.cpp @@ -92,7 +92,7 @@ UserCodeBackupPool::UserCodeBackupPool() } static void* UserCodeRunner(void* args) { - butil::PlatformThread::SetName("brpc_user_code_runner"); + butil::PlatformThread::SetNameSimple("brpc_user_code_runner"); static_cast(args)->UserCodeRunningLoop(); return NULL; } diff --git a/src/bthread/execution_queue.cpp b/src/bthread/execution_queue.cpp index 88d96c2b36..ae6f97b40a 100644 --- a/src/bthread/execution_queue.cpp +++ b/src/bthread/execution_queue.cpp @@ -208,7 +208,7 @@ void* ExecutionQueueBase::_execute_tasks(void* arg) { } void* ExecutionQueueBase::_execute_tasks_pthread(void* arg) { - butil::PlatformThread::SetName("ExecutionQueue"); + butil::PlatformThread::SetNameSimple("ExecutionQueue"); auto head = (TaskNode*)arg; auto m = (ExecutionQueueBase*)head->q; m->_current_head = head; diff --git a/src/bthread/task_control.cpp b/src/bthread/task_control.cpp index feeeb7d459..e43ce1ece9 100644 --- a/src/bthread/task_control.cpp +++ b/src/bthread/task_control.cpp @@ -103,7 +103,7 @@ void* TaskControl::worker_thread(void* arg) { std::string worker_thread_name = butil::string_printf( "brpc_wkr:%d-%d", g->tag(), c->_next_worker_id.fetch_add(1, butil::memory_order_relaxed)); - butil::PlatformThread::SetName(worker_thread_name.c_str()); + butil::PlatformThread::SetNameSimple(worker_thread_name.c_str()); } BT_VLOG << "Created worker=" << pthread_self() << " tid=" << g->_tid << " bthread=" << g->main_tid() << " tag=" << g->tag(); diff --git a/src/bthread/timer_thread.cpp b/src/bthread/timer_thread.cpp index bd1f4870c1..813104a452 100644 --- a/src/bthread/timer_thread.cpp +++ b/src/bthread/timer_thread.cpp @@ -121,7 +121,7 @@ inline bool task_greater(const TimerThread::Task* a, const TimerThread::Task* b) } void* TimerThread::run_this(void* arg) { - butil::PlatformThread::SetName("brpc_timer"); + butil::PlatformThread::SetNameSimple("brpc_timer"); static_cast(arg)->run(); return NULL; } diff --git a/src/butil/threading/platform_thread.h b/src/butil/threading/platform_thread.h index 8a1937a4db..fb36d45142 100644 --- a/src/butil/threading/platform_thread.h +++ b/src/butil/threading/platform_thread.h @@ -154,6 +154,7 @@ class BUTIL_EXPORT PlatformThread { // otherwise. This name pointer is not copied internally. Thus, it must stay // valid until the thread ends. static void SetName(const char* name); + static void SetNameSimple(const char* name); // Gets the thread name, if previously set by SetName. static const char* GetName(); diff --git a/src/butil/threading/platform_thread_freebsd.cc b/src/butil/threading/platform_thread_freebsd.cc index 40e15d1e2f..a18264be99 100644 --- a/src/butil/threading/platform_thread_freebsd.cc +++ b/src/butil/threading/platform_thread_freebsd.cc @@ -48,6 +48,11 @@ void PlatformThread::SetName(const char* name) { ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); tracked_objects::ThreadData::InitializeThreadContext(name); + SetNameSimple(name); +} + +// static +void PlatformThread::SetNameSimple(const char* name) { #if !defined(OS_NACL) // On FreeBSD we can get the thread names to show up in the debugger by // setting the process name for the LWP. We don't want to do this for the diff --git a/src/butil/threading/platform_thread_linux.cc b/src/butil/threading/platform_thread_linux.cc index 274e9a8b77..7a70560410 100644 --- a/src/butil/threading/platform_thread_linux.cc +++ b/src/butil/threading/platform_thread_linux.cc @@ -54,6 +54,10 @@ int ThreadNiceValue(ThreadPriority priority) { void PlatformThread::SetName(const char* name) { ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); + SetNameSimple(name); +} +// static +void PlatformThread::SetNameSimple(const char* name) { #if !defined(OS_NACL) // On linux we can get the thread names to show up in the debugger by setting // the process name for the LWP. We don't want to do this for the main diff --git a/src/butil/threading/platform_thread_mac.mm b/src/butil/threading/platform_thread_mac.mm index 22913012d2..a9f74f4857 100644 --- a/src/butil/threading/platform_thread_mac.mm +++ b/src/butil/threading/platform_thread_mac.mm @@ -46,6 +46,11 @@ void InitThreading() { // TODO: add tracked_objects related headers //tracked_objects::ThreadData::InitializeThreadContext(name); + SetNameSimple(name); +} + +// static +void PlatformThread::SetNameSimple(const char* name) { // Mac OS X does not expose the length limit of the name, so // hardcode it. const int kMaxNameLength = 63; diff --git a/src/bvar/collector.cpp b/src/bvar/collector.cpp index d708bd52f2..34713a4a00 100644 --- a/src/bvar/collector.cpp +++ b/src/bvar/collector.cpp @@ -77,13 +77,13 @@ class Collector : public bvar::Reducer { int64_t interval_us); static void* run_grab_thread(void* arg) { - butil::PlatformThread::SetName("bvar_collector_grabber"); + butil::PlatformThread::SetNameSimple("bvar_collector_grabber"); static_cast(arg)->grab_thread(); return NULL; } static void* run_dump_thread(void* arg) { - butil::PlatformThread::SetName("bvar_collector_dumper"); + butil::PlatformThread::SetNameSimple("bvar_collector_dumper"); static_cast(arg)->dump_thread(); return NULL; } diff --git a/src/bvar/detail/sampler.cpp b/src/bvar/detail/sampler.cpp index 2e231d2232..dd6271e7ba 100644 --- a/src/bvar/detail/sampler.cpp +++ b/src/bvar/detail/sampler.cpp @@ -109,7 +109,7 @@ class SamplerCollector : public bvar::Reducer { void run(); static void* sampling_thread(void* arg) { - butil::PlatformThread::SetName("bvar_sampler"); + butil::PlatformThread::SetNameSimple("bvar_sampler"); static_cast(arg)->run(); return NULL; } diff --git a/src/bvar/variable.cpp b/src/bvar/variable.cpp index fe76b3470b..80c3049e6d 100644 --- a/src/bvar/variable.cpp +++ b/src/bvar/variable.cpp @@ -730,7 +730,7 @@ static GFlag s_gflag_bvar_dump_interval("bvar_dump_interval"); static void* dumping_thread(void*) { // NOTE: this variable was declared as static <= r34381, which was // destructed when program exits and caused coredumps. - butil::PlatformThread::SetName("bvar_dumper"); + butil::PlatformThread::SetNameSimple("bvar_dumper"); const std::string command_name = read_command_name(); std::string last_filename; std::string mbvar_last_filename;