Skip to content

Commit 6425c3e

Browse files
committed
Fix the test case (again).
1 parent 2e99092 commit 6425c3e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

Programs/_testembed.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <Python.h>
99
#include "pycore_initconfig.h" // _PyConfig_InitCompatConfig()
1010
#include "pycore_runtime.h" // _PyRuntime
11+
#include "pycore_semaphore.h" // _PySemaphore
1112
#include "pycore_pythread.h" // PyThread_start_joinable_thread()
1213
#include "pycore_import.h" // _PyImport_FrozenBootstrap
1314
#include <inttypes.h>
@@ -2342,26 +2343,32 @@ test_get_incomplete_frame(void)
23422343
}
23432344

23442345
static void
2345-
do_gilstate_ensure(void *unused)
2346+
do_gilstate_ensure(void *semaphore_ptr)
23462347
{
2347-
for (int i = 0; i < 50; ++i) {
2348-
PyGILState_STATE gstate = PyGILState_Ensure();
2349-
assert(PyGILState_Check()); // Yuck
2350-
PyGILState_Release(gstate);
2351-
}
2348+
_PySemaphore *semaphore = (_PySemaphore *)semaphore_ptr;
2349+
// Signal to the calling thread that we've started
2350+
_PySemaphore_Wakeup(semaphore);
2351+
PyGILState_STATE gstate = PyGILState_Ensure(); // This should hang
2352+
assert(NULL);
23522353
}
23532354

23542355
static int
23552356
test_gilstate_after_finalization(void)
23562357
{
23572358
_testembed_Py_Initialize();
2359+
Py_Finalize();
23582360
PyThread_handle_t handle;
23592361
PyThread_ident_t ident;
2360-
if (PyThread_start_joinable_thread(&do_gilstate_ensure, NULL, &ident, &handle) < 0) {
2362+
_PySemaphore semaphore;
2363+
_PySemaphore_Init(&semaphore);
2364+
if (PyThread_start_joinable_thread(&do_gilstate_ensure, &semaphore, &ident, &handle) < 0) {
23612365
return -1;
23622366
}
2363-
Py_Finalize();
2364-
return PyThread_join_thread(handle);
2367+
_PySemaphore_Wait(&semaphore, /*timeout_ns=*/-1, /*detach=*/0);
2368+
// We're now pretty confident that the thread went for
2369+
// PyGILState_Ensure(), but that means it got hung.
2370+
_PySemaphore_Destroy(&semaphore);
2371+
return PyThread_detach_thread(handle);
23652372
}
23662373

23672374
/* *********************************************************

Python/pystate.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,9 @@ unbind_gilstate_tstate(PyThreadState *tstate)
271271
assert(tstate_is_bound(tstate));
272272
// XXX assert(!tstate->_status.active);
273273
assert(tstate->_status.bound_gilstate);
274-
assert(tstate == gilstate_get());
275-
276-
gilstate_clear();
274+
if (tstate == gilstate_get()) {
275+
gilstate_clear();
276+
}
277277
tstate->_status.bound_gilstate = 0;
278278
}
279279

@@ -2695,8 +2695,9 @@ PyGILState_Ensure(void)
26952695

26962696
/* Ensure that _PyEval_InitThreads() and _PyGILState_Init() have been
26972697
called by Py_Initialize() */
2698-
assert(_PyEval_ThreadsInitialized());
2699-
assert(runtime->gilstate.autoInterpreterState != NULL);
2698+
if (!_PyEval_ThreadsInitialized() || runtime->gilstate.autoInterpreterState == NULL) {
2699+
PyThread_hang_thread();
2700+
}
27002701

27012702
PyThreadState *tcur = gilstate_get();
27022703
int has_gil;

0 commit comments

Comments
 (0)