summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorKumar Aditya <59607654+kumaraditya303@users.noreply.github.com>2022-08-19 19:43:00 (GMT)
committerGitHub <noreply@github.com>2022-08-19 19:43:00 (GMT)
commite0d54a4a799dae4ebdd72a16bcf287ed62ae2972 (patch)
treecba0d40312a87597a6ac68ebe1b1051a45b8617d /Python
parent822955c16654c22c10a993f5a94bbb68b857a150 (diff)
downloadcpython-e0d54a4a799dae4ebdd72a16bcf287ed62ae2972.zip
cpython-e0d54a4a799dae4ebdd72a16bcf287ed62ae2972.tar.gz
cpython-e0d54a4a799dae4ebdd72a16bcf287ed62ae2972.tar.bz2
GH-96071: fix deadlock in PyGILState_Ensure (GH-96124)
Alternative of #96107
Diffstat (limited to 'Python')
-rw-r--r--Python/pystate.c27
1 files changed, 16 insertions, 11 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index 642d680..a11f162 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -810,7 +810,15 @@ new_threadstate(PyInterpreterState *interp)
{
PyThreadState *tstate;
_PyRuntimeState *runtime = interp->runtime;
-
+ // We don't need to allocate a thread state for the main interpreter
+ // (the common case), but doing it later for the other case revealed a
+ // reentrancy problem (deadlock). So for now we always allocate before
+ // taking the interpreters lock. See GH-96071.
+ PyThreadState *new_tstate = alloc_threadstate();
+ int used_newtstate;
+ if (new_tstate == NULL) {
+ return NULL;
+ }
/* We serialize concurrent creation to protect global state. */
HEAD_LOCK(runtime);
@@ -822,18 +830,15 @@ new_threadstate(PyInterpreterState *interp)
if (old_head == NULL) {
// It's the interpreter's initial thread state.
assert(id == 1);
-
+ used_newtstate = 0;
tstate = &interp->_initial_thread;
}
else {
// Every valid interpreter must have at least one thread.
assert(id > 1);
assert(old_head->prev == NULL);
-
- tstate = alloc_threadstate();
- if (tstate == NULL) {
- goto error;
- }
+ used_newtstate = 1;
+ tstate = new_tstate;
// Set to _PyThreadState_INIT.
memcpy(tstate,
&initial._main_interpreter._initial_thread,
@@ -844,11 +849,11 @@ new_threadstate(PyInterpreterState *interp)
init_threadstate(tstate, interp, id, old_head);
HEAD_UNLOCK(runtime);
+ if (!used_newtstate) {
+ // Must be called with lock unlocked to avoid re-entrancy deadlock.
+ PyMem_RawFree(new_tstate);
+ }
return tstate;
-
-error:
- HEAD_UNLOCK(runtime);
- return NULL;
}
PyThreadState *