Skip to content

Commit

Permalink
Fix pthread_setname_np race condition in thread_base::event_loop
Browse files Browse the repository at this point in the history
The thread->m_thread member is set via pthread_create(3). However,
it is not guaranteed that pthread_create writes this member before
it returns. Since thread_base::event_loop is the function executed
by pthread_create, we cannot assume that it is set. Instead, use
pthread_self() which is guaranteed to be set.
  • Loading branch information
nmeum authored and rakshasa committed Feb 18, 2025
1 parent fe624b9 commit 7a3493e
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/torrent/utils/thread_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ thread_base::event_loop(thread_base* thread) {
#if defined(HAS_PTHREAD_SETNAME_NP_DARWIN)
pthread_setname_np(thread->name());
#elif defined(HAS_PTHREAD_SETNAME_NP_GENERIC)
pthread_setname_np(thread->m_thread, thread->name());
// Cannot use thread->m_thread here as it may not be set before pthread_create returns.
pthread_setname_np(pthread_self(), thread->name());
#endif

lt_log_print(torrent::LOG_THREAD_NOTICE, "%s: Starting thread.", thread->name());
Expand Down

0 comments on commit 7a3493e

Please sign in to comment.