aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/Python/pystate.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/tools/python3/Python/pystate.c')
-rw-r--r--contrib/tools/python3/Python/pystate.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/contrib/tools/python3/Python/pystate.c b/contrib/tools/python3/Python/pystate.c
index 7acf4f9854..93fbbaaf01 100644
--- a/contrib/tools/python3/Python/pystate.c
+++ b/contrib/tools/python3/Python/pystate.c
@@ -2282,12 +2282,18 @@ PyGILState_Release(PyGILState_STATE oldstate)
/* can't have been locked when we created it */
assert(oldstate == PyGILState_UNLOCKED);
// XXX Unbind tstate here.
+ // gh-119585: `PyThreadState_Clear()` may call destructors that
+ // themselves use PyGILState_Ensure and PyGILState_Release, so make
+ // sure that gilstate_counter is not zero when calling it.
+ ++tstate->gilstate_counter;
PyThreadState_Clear(tstate);
+ --tstate->gilstate_counter;
/* Delete the thread-state. Note this releases the GIL too!
* It's vital that the GIL be held here, to avoid shutdown
* races; see bugs 225673 and 1061968 (that nasty bug has a
* habit of coming back).
*/
+ assert(tstate->gilstate_counter == 0);
assert(current_fast_get(runtime) == tstate);
_PyThreadState_DeleteCurrent(tstate);
}