index-pack: work around LSan threading race with barrier

We sometimes get false positives from our linux-leaks CI job because of a race in LSan itself. The problem is that one thread is still initializing its stack in LSan's code (and allocating memory to do so) while anothe thread calls die(), taking down the whole process and triggering a leak check. The problem is described in more detail in 993d38a066 (index-pack: spawn threads atomically, 2024-01-05), which tried to fix it by pausing worker threads until all calls to pthread_create() had completed. But that's not enough to fix the problem, because the LSan setup code runs in the threads themselves. So even though pthread_create() has returned, we have no idea if all threads actually finished their setup before letting any of them do real work. We can fix that by using a barrier inside the threads themselves, waiting for all of them to hit the start of their main function before any of them proceed. You can test for the race by running: make SANITIZE=leak THREAD_BARRIER_PTHREAD=YesOnLinux cd t ./t5309-pack-delta-cycles.sh --stress which fails quickly before this patch, and should run indefinitely without it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 29, 2024 at 23:29 UTC 526c0a851b14d1bbec4b8d31a23d93ca0eb82637
1 file changed +6
builtin/index-pack.c
+6
@@ -185,6 +185,8 @@ static pthread_mutex_t deepest_delta_mutex;
185
186 static pthread_key_t key;
187
188 +static maybe_thread_barrier_t start_barrier;
189 +
190 static inline void lock_mutex(pthread_mutex_t *mutex)
191 {
192 if (threads_active)
@@ -209,6 +211,7 @@ static void init_thread(void)
211 if (show_stat)
212 pthread_mutex_init(&deepest_delta_mutex, NULL);
213 pthread_key_create(&key, NULL);
214 + maybe_thread_barrier_init(&start_barrier, NULL, nr_threads);
215 CALLOC_ARRAY(thread_data, nr_threads);
216 for (i = 0; i < nr_threads; i++) {
217 thread_data[i].pack_fd = xopen(curr_pack, O_RDONLY);
@@ -231,6 +234,7 @@ static void cleanup_thread(void)
234 for (i = 0; i < nr_threads; i++)
235 close(thread_data[i].pack_fd);
236 pthread_key_delete(key);
237 + maybe_thread_barrier_destroy(&start_barrier);
238 free(thread_data);
239 }
240
@@ -1100,6 +1104,8 @@ static int compare_ref_delta_entry(const void *a, const void *b)
1104
1105 static void *threaded_second_pass(void *data)
1106 {
1107 + if (threads_active)
1108 + maybe_thread_barrier_wait(&start_barrier);
1109 if (data)
1110 set_thread_data(data);
1111 for (;;) {