8000 gh-120838: Make Sure Py_Finalize() Always Runs Against the Main Interpreter by ericsnowcurrently · Pull Request #121063 · python/cpython · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Next Next commit
Factor out resolve_final_tstate() and _Py_Finalize().
  • Loading branch information
ericsnowcurrently committed Jun 26, 2024
commit df63d15cf5ca3af06ef87bba488cf6c998afd125
32 changes: 23 additions & 9 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1908,20 +1908,28 @@ finalize_interp_delete(PyInterpreterState *interp)
}


int
Py_FinalizeEx(void)
static PyThreadState *
resolve_final_tstate(_PyRuntimeState *runtime)
{
PyThreadState *tstate = _PyThreadState_GET();
// XXX assert(_Py_IsMainInterpreter(tstate->interp));
// XXX assert(_Py_IsMainThread());

return 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 +2149,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 +3231,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
0