mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
[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.
This commit is contained in:
parent
daf9ff99f9
commit
82ae5a609d
7 changed files with 118 additions and 34 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue