diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-11-28 00:58:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-28 00:58:02 (GMT) |
commit | 82ae5a609d9a2c0ff2384527a18ff1caf7410052 (patch) | |
tree | cff9bfd9647110dca841fdac8668b6f8a0ebf503 /Python/pystate.c | |
parent | daf9ff99f9909c72e002746e51cbb9051ee0a1b6 (diff) | |
download | cpython-82ae5a609d9a2c0ff2384527a18ff1caf7410052.zip cpython-82ae5a609d9a2c0ff2384527a18ff1caf7410052.tar.gz cpython-82ae5a609d9a2c0ff2384527a18ff1caf7410052.tar.bz2 |
[3.12] gh-109793: Allow Switching Interpreters During Finalization (gh-109794) (gh-110705)
Essentially, we should check the thread ID rather than the thread state pointer.
Diffstat (limited to 'Python/pystate.c')
-rw-r--r-- | Python/pystate.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/Python/pystate.c b/Python/pystate.c index e789eb9..0430454 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -2916,11 +2916,26 @@ _PyThreadState_MustExit(PyThreadState *tstate) tstate->interp->runtime to support calls from Python daemon threads. After Py_Finalize() has been called, tstate can be a dangling pointer: point to PyThreadState freed memory. */ + unsigned long finalizing_id = _PyRuntimeState_GetFinalizingID(&_PyRuntime); PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(&_PyRuntime); if (finalizing == NULL) { + // XXX This isn't completely safe from daemon thraeds, + // since tstate might be a dangling pointer. finalizing = _PyInterpreterState_GetFinalizing(tstate->interp); + finalizing_id = _PyInterpreterState_GetFinalizingID(tstate->interp); } - return (finalizing != NULL && finalizing != tstate); + // XXX else check &_PyRuntime._main_interpreter._initial_thread + if (finalizing == NULL) { + return 0; + } + else if (finalizing == tstate) { + return 0; + } + else if (finalizing_id == PyThread_get_thread_ident()) { + /* gh-109793: we must have switched interpreters. */ + return 0; + } + return 1; } |