]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MEDIUM: thread: use pthread_self() not ha_pthread[tid] in set_affinity
authorWilly Tarreau <w@1wt.eu>
Wed, 12 Mar 2025 14:54:36 +0000 (15:54 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 12 Mar 2025 14:59:23 +0000 (15:59 +0100)
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.

src/thread.c

index fa47cd67d41e559e72e8b2f629200399ff75b705..e54a32ef93e0d6bb2b7e271c534f87ccb46054a4 100644 (file)
@@ -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 */