Skip to content

Commit 050b9dd

Browse files
miss-islingtonkumaraditya303ZeroIntensity
authored
[3.13] pythongh-137093: Fix race condition in test_embed.test_bpo20891 (pythonGH-137094) (pythonGH-140524) (python#140527)
[3.14] pythongh-137093: Fix race condition in `test_embed.test_bpo20891` (pythonGH-137094) (pythonGH-140524) Use a `PyEvent` instead of a lock to fix a race on the free-threaded build. (cherry picked from commit 9b451fb) (cherry picked from commit 6efd78d) Co-authored-by: Kumar Aditya <[email protected]> Co-authored-by: Peter Bierma <[email protected]>
1 parent 1cc2c95 commit 050b9dd

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

Programs/_testembed.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,9 @@ static int test_pre_initialization_sys_options(void)
412412

413413

414414
/* bpo-20891: Avoid race condition when initialising the GIL */
415-
static void bpo20891_thread(void *lockp)
415+
static void bpo20891_thread(void *eventp)
416416
{
417-
PyThread_type_lock lock = *((PyThread_type_lock*)lockp);
417+
PyEvent *event = (PyEvent *)eventp;
418418

419419
PyGILState_STATE state = PyGILState_Ensure();
420420
if (!PyGILState_Check()) {
@@ -423,8 +423,7 @@ static void bpo20891_thread(void *lockp)
423423
}
424424

425425
PyGILState_Release(state);
426-
427-
PyThread_release_lock(lock);
426+
_PyEvent_Notify(event);
428427
}
429428

430429
static int test_bpo20891(void)
@@ -434,27 +433,17 @@ static int test_bpo20891(void)
434433

435434
/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
436435
crash. */
437-
PyThread_type_lock lock = PyThread_allocate_lock();
438-
if (!lock) {
439-
error("PyThread_allocate_lock failed!");
440-
return 1;
441-
}
442436

443437
_testembed_Py_InitializeFromConfig();
438+
PyEvent event = {0};
444439

445-
unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock);
440+
unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &event);
446441
if (thrd == PYTHREAD_INVALID_THREAD_ID) {
447442
error("PyThread_start_new_thread failed!");
448443
return 1;
449444
}
450-
PyThread_acquire_lock(lock, WAIT_LOCK);
451-
452-
Py_BEGIN_ALLOW_THREADS
453-
/* wait until the thread exit */
454-
PyThread_acquire_lock(lock, WAIT_LOCK);
455-
Py_END_ALLOW_THREADS
456445

457-
PyThread_free_lock(lock);
446+
PyEvent_Wait(&event);
458447

459448
Py_Finalize();
460449

0 commit comments

Comments
 (0)