Skip to content

Commit d1878cc

Browse files
vstinnerngoldbaum
andauthored
gh-157415: Fix data race in pthread wrapper (#157430)
Use free() instead of PyMem_RawFree() in pythread_wrapper() to avoid a data race if another thread calls PyMem_SetAllocator() in parallel. Reenable @nomemtest tests on TSAN. Co-authored-by: Nathan Goldbaum <nathan.goldbaum@gmail.com>
1 parent 86c1792 commit d1878cc

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

‎Lib/test/support/__init__.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,11 +1373,7 @@ def internal(*args, **kwargs):
13731373
import_module('_testcapi')
13741374
return test(*args, **kwargs)
13751375

1376-
use_tsan = check_sanitizer(thread=True)
1377-
reason ='not working with thread sanitizer (gh-157415)'
1378-
skip_if_tsan = unittest.skipIf(use_tsan, reason)
1379-
1380-
return cpython_only(skip_if_tsan(internal))
1376+
return cpython_only(internal)
13811377

13821378
def bigaddrspacetest(f):
13831379
"""Decorator for tests that fill the address space."""

‎Python/thread_pthread.h‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pythread_wrapper(void *arg)
231231
pythread_callback *callback = arg;
232232
void (*func)(void *) = callback->func;
233233
void *func_arg = callback->arg;
234-
PyMem_RawFree(arg);
234+
free(callback);
235235

236236
func(func_arg);
237237
return NULL;
@@ -271,7 +271,9 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
271271
pthread_attr_setscope(&attrs, PTHREAD_SCOPE_SYSTEM);
272272
#endif
273273

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

276278
if (callback == NULL) {
277279
return -1;
@@ -293,7 +295,7 @@ do_start_joinable_thread(void (*func)(void *), void *arg, pthread_t* out_id)
293295
#endif
294296

295297
if (status != 0) {
296-
PyMem_RawFree(callback);
298+
free(callback);
297299
return -1;
298300
}
299301
*out_id = th;

0 commit comments

Comments
 (0)