Skip to content

gh-120838: Make Sure Py_Finalize() Always Runs Against the Main Interpreter #121063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:c:func:`Py_Finalize()` and :c:func:`Py_FinalizeEx()` now always run with
the main interpreter active.
77 changes: 68 additions & 9 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1908,20 +1908,73 @@ finalize_interp_delete(PyInterpreterState *interp)
}


int
Py_FinalizeEx(void)
/* Conceptually, there isn't a good reason for Py_Finalize()
to be called in any other thread than the one where Py_Initialize()
was called. Consequently, it would make sense to fail if the thread
or thread state (or interpreter) don't match. However, such
constraints have never been enforced, and, as unlikely as it may be,
there may be users relying on the unconstrained behavior. Thus,
we do our best here to accommodate that possibility. */

static PyThreadState *
resolve_final_tstate(_PyRuntimeState *runtime)
{
PyThreadState *main_tstate = runtime->main_tstate;
assert(main_tstate != NULL);
assert(main_tstate->thread_id == runtime->main_thread);
PyInterpreterState *main_interp = _PyInterpreterState_Main();
assert(main_tstate->interp == main_interp);

PyThreadState *tstate = _PyThreadState_GET();
if (_Py_IsMainThread()) {
if (tstate != main_tstate) {
/* This implies that Py_Finalize() was called while
a non-main interpreter was active or while the main
tstate was temporarily swapped out with another.
Neither case should be allowed, but, until we get around
to fixing that (and Py_Exit()), we're letting it go. */
(void)PyThreadState_Swap(main_tstate);
}
}
else {
/* This is another unfortunate case where Py_Finalize() was
called when it shouldn't have been. We can't simply switch
over to the main thread. At the least, however, we can make
sure the main interpreter is active. */
if (!_Py_IsMainInterpreter(tstate->interp)) {
/* We don't go to the trouble of updating runtime->main_tstate
since it will be dead soon anyway. */
main_tstate =
_PyThreadState_New(main_interp, _PyThreadState_WHENCE_FINI);
if (main_tstate != NULL) {
_PyThreadState_Bind(main_tstate);
(void)PyThreadState_Swap(main_tstate);
}
else {
/* Fall back to the current tstate. It's better than nothing. */
main_tstate = tstate;
}
}
}
assert(main_tstate != NULL);

/* We might want to warn if main_tstate->current_frame != NULL. */

return main_tstate;
}

static int
_Py_Finalize(_PyRuntimeState *runtime)
{
int status = 0;

_PyRuntimeState *runtime = &_PyRuntime;
/* Bail out early if already finalized (or never initialized). */
if (!runtime->initialized) {
return status;
}

/* Get current thread state and interpreter pointer */
PyThreadState *tstate = _PyThreadState_GET();
// XXX assert(_Py_IsMainInterpreter(tstate->interp));
// XXX assert(_Py_IsMainThread());
/* Get final thread state pointer. */
PyThreadState *tstate = resolve_final_tstate(runtime);

// Block some operations.
tstate->interp->finalizing = 1;
Expand Down Expand Up @@ -2141,10 +2194,16 @@ Py_FinalizeEx(void)
return status;
}

int
Py_FinalizeEx(void)
{
return _Py_Finalize(&_PyRuntime);
}

void
Py_Finalize(void)
{
Py_FinalizeEx();
(void)_Py_Finalize(&_PyRuntime);
}


Expand Down Expand Up @@ -3217,7 +3276,7 @@ Py_Exit(int sts)
if (tstate != NULL && _PyThreadState_IsRunningMain(tstate)) {
_PyInterpreterState_SetNotRunningMain(tstate->interp);
}
if (Py_FinalizeEx() < 0) {
if (_Py_Finalize(&_PyRuntime) < 0) {
sts = 120;
}

Expand Down
Loading