Skip to content

Use SetNameSimple instead of SetName to avoid deadlock#3127

Merged
chenBright merged 1 commit intoapache:masterfrom
chenBright:fix_thread_setname
Oct 27, 2025
Merged

Use SetNameSimple instead of SetName to avoid deadlock#3127
chenBright merged 1 commit intoapache:masterfrom
chenBright:fix_thread_setname

Conversation

@chenBright
Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve #3104

Problem Summary:

What is changed and the side effects?

Changed:

Side effects:

  • Performance effects:

  • Breaking backward compatibility:


Check List:

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a deadlock issue (#3104) by replacing PlatformThread::SetName calls with a new SetNameSimple variant across various thread initialization functions. The change introduces SetNameSimple as a lighter-weight alternative that sets only the OS-level thread name without the additional overhead of ThreadIdNameManager operations that could cause deadlocks.

  • Introduces SetNameSimple method that bypasses ThreadIdNameManager and tracked objects
  • Refactors SetName to call SetNameSimple internally, maintaining existing functionality
  • Updates all thread entry points to use SetNameSimple instead of SetName

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/butil/threading/platform_thread.h Declares new SetNameSimple static method
src/butil/threading/platform_thread_linux.cc Implements SetNameSimple and refactors SetName to use it
src/butil/threading/platform_thread_mac.mm Implements SetNameSimple and refactors SetName to use it
src/butil/threading/platform_thread_freebsd.cc Implements SetNameSimple and refactors SetName to use it
src/bvar/variable.cpp Updates bvar_dumper thread to use SetNameSimple
src/bvar/detail/sampler.cpp Updates bvar_sampler thread to use SetNameSimple
src/bvar/collector.cpp Updates collector grabber and dumper threads to use SetNameSimple
src/bthread/timer_thread.cpp Updates brpc_timer thread to use SetNameSimple
src/bthread/task_control.cpp Updates worker threads to use SetNameSimple
src/bthread/execution_queue.cpp Updates ExecutionQueue thread to use SetNameSimple
src/brpc/details/usercode_backup_pool.cpp Updates user code runner thread to use SetNameSimple

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@yanglimingcn
Copy link
Copy Markdown
Contributor

why ThreadIdNameManager case deadlock

@chenBright
Copy link
Copy Markdown
Contributor Author

chenBright commented Oct 27, 2025

why ThreadIdNameManager case deadlock

Still not sure why. It's strange that new ThreadIdNameManager blocks and causes other threads to wait for the ThreadIdNameManager to be constructed.

@wwbmmm
Copy link
Copy Markdown
Contributor

wwbmmm commented Oct 27, 2025

LGTM

1 similar comment
@yanglimingcn
Copy link
Copy Markdown
Contributor

LGTM

@chenBright chenBright merged commit 1152b80 into apache:master Oct 27, 2025
16 checks passed
@chenBright chenBright deleted the fix_thread_setname branch October 27, 2025 13:00
@chentingkai00301
Copy link
Copy Markdown

chentingkai00301 commented Nov 12, 2025

why ThreadIdNameManager case deadlock

In the sampling thread, after SetName enters the lock, the main thread performs a fork. When the child process acquires memory, it is locked, causing all threads in the child process to get stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TimerThread 没有正常初始卡在butil::PlatformThread::SetName("brpc_timer")

5 participants