From: Willy Tarreau Date: Wed, 12 Mar 2025 14:54:36 +0000 (+0100) Subject: BUG/MEDIUM: thread: use pthread_self() not ha_pthread[tid] in set_affinity X-Git-Tag: v3.2-dev8~107 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=12383fd9f5b3614dbffec6260b82659c3c5fd0df;p=thirdparty%2Fhaproxy.git BUG/MEDIUM: thread: use pthread_self() not ha_pthread[tid] in set_affinity A bug was uncovered by the work on NUMA. It only triggers in the CI with libmusl due to a race condition. What happens is that the call to set_thread_cpu_affinity() is done very early in the polling loop, and that it relies on ha_pthread[tid] instead of pthread_self(). The problem is that ha_pthread[tid] is only set by the return from pthread_create(), which might happen later depending on the number of CPUs available to run the starting thread. Let's just use pthread_self() here. ha_pthread[] is only used to send signals between threads, there's no point in using it here. This can be backported to 2.6. --- diff --git a/src/thread.c b/src/thread.c index fa47cd67d..e54a32ef9 100644 --- a/src/thread.c +++ b/src/thread.c @@ -287,14 +287,14 @@ void set_thread_cpu_affinity() thread_affinity_policy_data_t cpu_set = { j - 1 }; thread_port_t mthread; - mthread = pthread_mach_thread_np(ha_pthread[tid]); + mthread = pthread_mach_thread_np(pthread_self()); thread_policy_set(mthread, THREAD_AFFINITY_POLICY, (thread_policy_t)&cpu_set, 1); set &= ~(1UL << (j - 1)); } # else struct hap_cpuset *set = &cpu_map[tgid - 1].thread[ti->ltid]; - pthread_setaffinity_np(ha_pthread[tid], sizeof(set->cpuset), &set->cpuset); + pthread_setaffinity_np(pthread_self(), sizeof(set->cpuset), &set->cpuset); # endif } #endif /* USE_CPU_AFFINITY */