summaryrefslogtreecommitdiffstats
path: root/Python/pylifecycle.c
diff options
context:
space:
mode:
authorEric Snow <ericsnowcurrently@gmail.com>2023-05-15 19:59:26 (GMT)
committerGitHub <noreply@github.com>2023-05-15 19:59:26 (GMT)
commit26baa747c2ebc2beeff769bb07b5fb5a51ad5f4b (patch)
treee54b52c2c950d59a20f4b0658d8eb2af3adec246 /Python/pylifecycle.c
parentcb88ae635e96d7020ba6187bcfd45ace4dcd8395 (diff)
downloadcpython-26baa747c2ebc2beeff769bb07b5fb5a51ad5f4b.zip
cpython-26baa747c2ebc2beeff769bb07b5fb5a51ad5f4b.tar.gz
cpython-26baa747c2ebc2beeff769bb07b5fb5a51ad5f4b.tar.bz2
gh-104341: Adjust tstate_must_exit() to Respect Interpreter Finalization (gh-104437)
With the move to a per-interpreter GIL, this check slipped through the cracks.
Diffstat (limited to 'Python/pylifecycle.c')
-rw-r--r--Python/pylifecycle.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c
index c5dc0f4..cb87f2c 100644
--- a/Python/pylifecycle.c
+++ b/Python/pylifecycle.c
@@ -1788,6 +1788,7 @@ Py_FinalizeEx(void)
/* Remaining daemon threads will automatically exit
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
+ _PyInterpreterState_SetFinalizing(tstate->interp, tstate);
_PyRuntimeState_SetFinalizing(runtime, tstate);
runtime->initialized = 0;
runtime->core_initialized = 0;
@@ -2142,6 +2143,10 @@ Py_EndInterpreter(PyThreadState *tstate)
Py_FatalError("not the last thread");
}
+ /* Remaining daemon threads will automatically exit
+ when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
+ _PyInterpreterState_SetFinalizing(interp, tstate);
+
// XXX Call something like _PyImport_Disable() here?
_PyImport_FiniExternal(tstate->interp);
@@ -2152,6 +2157,18 @@ Py_EndInterpreter(PyThreadState *tstate)
finalize_interp_delete(tstate->interp);
}
+int
+_Py_IsInterpreterFinalizing(PyInterpreterState *interp)
+{
+ /* We check the runtime first since, in a daemon thread,
+ interp might be dangling pointer. */
+ PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime);
+ if (finalizing == NULL) {
+ finalizing = _PyInterpreterState_GetFinalizing(interp);
+ }
+ return finalizing != NULL;
+}
+
/* Add the __main__ module */
static PyStatus