Fix background thread initialization race

This commit is contained in:
Andrei Pechkurov 2026-01-09 21:55:45 +02:00 committed by Guangli Dai
parent d4908fe44a
commit 4d0ffa075b
3 changed files with 190 additions and 2 deletions

View file

@ -547,8 +547,13 @@ background_thread_create_locked(tsd_t *tsd, unsigned arena_ind) {
bool need_new_thread;
malloc_mutex_lock(tsd_tsdn(tsd), &info->mtx);
/*
* The last check is there to leave Thread 0 creation entirely
* to the initializing thread (arena 0).
*/
need_new_thread = background_thread_enabled()
&& (info->state == background_thread_stopped);
&& (info->state == background_thread_stopped)
&& (thread_ind != 0 || arena_ind == 0);
if (need_new_thread) {
background_thread_init(tsd, info);
}
@ -560,7 +565,6 @@ background_thread_create_locked(tsd_t *tsd, unsigned arena_ind) {
/* Threads are created asynchronously by Thread 0. */
background_thread_info_t *t0 = &background_thread_info[0];
malloc_mutex_lock(tsd_tsdn(tsd), &t0->mtx);
assert(t0->state == background_thread_started);
pthread_cond_signal(&t0->cond);
malloc_mutex_unlock(tsd_tsdn(tsd), &t0->mtx);