From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Fri, 24 Oct 2025 08:17:26 +0000 (+0200) Subject: [3.13] gh-137093: Fix race condition in `test_embed.test_bpo20891` (GH-137094) (GH... X-Git-Tag: v3.13.10~222 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=050b9dda0432f2903c70fa2ff53305976b284e27;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-137093: Fix race condition in `test_embed.test_bpo20891` (GH-137094) (GH-140524) (#140527) [3.14] gh-137093: Fix race condition in `test_embed.test_bpo20891` (GH-137094) (GH-140524) Use a `PyEvent` instead of a lock to fix a race on the free-threaded build. (cherry picked from commit 9b451fb457a5de9ed535a0e2f41161dfaa9a419a) (cherry picked from commit 6efd78d7ab01c5daf2197c8c9e8f2db046e6d8f1) Co-authored-by: Kumar Aditya Co-authored-by: Peter Bierma --- diff --git a/Programs/_testembed.c b/Programs/_testembed.c index 0ff2e85a4be2..4d8452503e47 100644 --- a/Programs/_testembed.c +++ b/Programs/_testembed.c @@ -412,9 +412,9 @@ static int test_pre_initialization_sys_options(void) /* bpo-20891: Avoid race condition when initialising the GIL */ -static void bpo20891_thread(void *lockp) +static void bpo20891_thread(void *eventp) { - PyThread_type_lock lock = *((PyThread_type_lock*)lockp); + PyEvent *event = (PyEvent *)eventp; PyGILState_STATE state = PyGILState_Ensure(); if (!PyGILState_Check()) { @@ -423,8 +423,7 @@ static void bpo20891_thread(void *lockp) } PyGILState_Release(state); - - PyThread_release_lock(lock); + _PyEvent_Notify(event); } static int test_bpo20891(void) @@ -434,27 +433,17 @@ static int test_bpo20891(void) /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not crash. */ - PyThread_type_lock lock = PyThread_allocate_lock(); - if (!lock) { - error("PyThread_allocate_lock failed!"); - return 1; - } _testembed_Py_InitializeFromConfig(); + PyEvent event = {0}; - unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &lock); + unsigned long thrd = PyThread_start_new_thread(bpo20891_thread, &event); if (thrd == PYTHREAD_INVALID_THREAD_ID) { error("PyThread_start_new_thread failed!"); return 1; } - PyThread_acquire_lock(lock, WAIT_LOCK); - - Py_BEGIN_ALLOW_THREADS - /* wait until the thread exit */ - PyThread_acquire_lock(lock, WAIT_LOCK); - Py_END_ALLOW_THREADS - PyThread_free_lock(lock); + PyEvent_Wait(&event); Py_Finalize();