Skip to content

Commit 7c04882

Browse files
vstinnerngoldbaum
andauthored
[3.14] gh-157415: Fix data race in pthread wrapper (#157430) (#159547)
Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid a data race if another thread calls PyMem_SetAllocator() in parallel. (cherry picked from commit d1878cc) Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
1 parent 8e4b285 commit 7c04882

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎Python/thread_pthread.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pythread_wrapper(void *arg)
237237
pythread_callback *callback = arg;
238238
void (*func)(void *) = callback->func;
239239
void *func_arg = callback->arg;
240-
PyMem_RawFree(arg);
240+
free(callback);
241241

242242
func(func_arg);
243243
return NULL;
@@ -277,7 +277,9 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
277277
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
278278
#endif
279279

280-
pythread_callback *callback = PyMem_RawMalloc(sizeof(pythread_callback));
280+
// Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid a
281+
// data race if another thread calls PyMem_SetAllocator() in parallel.
282+
pythread_callback *callback = malloc(sizeof(pythread_callback));
281283

282284
if (callback == NULL) {
283285
return -1;
@@ -299,7 +301,7 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
299301
#endif
300302

301303
if (status != 0) {
302-
PyMem_RawFree(callback);
304+
free(callback);
303305
return -1;
304306
}
305307
*out_id = th;

0 commit comments

Comments
 (0)