From 250cf48f581e099bb805af93336129493a7428e4 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 13 Sep 2026 15:49:51 +0200 Subject: [PATCH] gh-157415: Fix data race in pthread wrapper Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid a data race if another thread calls PyMem_SetAllocator() in parallel. Co-Authored-by: Nathan Goldbaum --- Python/thread_pthread.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 8d727326faeb70d..e660bca84068604 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -231,7 +231,7 @@ pythread_wrapper(void *arg) pythread_callback *callback = arg; void (*func)(void *) = callback->func; void *func_arg = callback->arg; - PyMem_RawFree(arg); + free(callback); func(func_arg); return NULL; @@ -271,7 +271,9 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id) pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM); #endif - pythread_callback *callback = PyMem_RawMalloc(sizeof(pythread_callback)); + // Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid a + // data race if another thread calls PyMem_SetAllocator() in parallel. + pythread_callback *callback = malloc(sizeof(pythread_callback)); if (callback == NULL) { return -1; @@ -293,7 +295,7 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id) #endif if (status != 0) { - PyMem_RawFree(callback); + free(callback); return -1; } *out_id = th;