mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
gh-109793: Allow Switching Interpreters During Finalization (gh-109794)
Essentially, we should check the thread ID rather than the thread state pointer.
This commit is contained in:
parent
f49958c886
commit
32466c97c0
7 changed files with 96 additions and 3 deletions
|
|
@ -2964,11 +2964,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