Skip to content

Commit

Permalink
More tweaking resolve_final_tstate().
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed Jun 27, 2024
1 parent d78f655 commit fe12f40
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1966,8 +1966,14 @@ resolve_final_tstate(_PyRuntimeState *runtime, struct pyfinalize_args *args)
/* Then we decide the thread state we should use for finalization. */

PyThreadState *final_tstate = tstate;
if (_Py_IsMainThread() && main_tstate != NULL) {
if (tstate != main_tstate) {
if (_Py_IsMainThread()) {
if (main_tstate == NULL) {
if (tstate->interp != main_interp) {
/* We will swap in a tstate for the main interpreter. */
final_tstate = NULL;
}
}
else 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.
Expand All @@ -1982,18 +1988,22 @@ resolve_final_tstate(_PyRuntimeState *runtime, struct pyfinalize_args *args)
over to the main thread. At the least, however, we can make
sure the main interpreter is active. */
if (tstate->interp != main_interp) {
/* We don't go to the trouble of updating runtime->main_tstate
since it will be dead soon anyway. */
final_tstate =
_PyThreadState_New(main_interp, _PyThreadState_WHENCE_FINI);
if (final_tstate != NULL) {
_PyThreadState_Bind(final_tstate);
}
/* Otherwise we fall back to the current tstate.
It's better than nothing. */
final_tstate = NULL;
}
}
if (final_tstate == NULL) {
/* We don't go to the trouble of updating runtime->main_tstate
since it will be dead soon anyway. */
final_tstate =
_PyThreadState_New(main_interp, _PyThreadState_WHENCE_FINI);
if (final_tstate == NULL) {
/* Fall back to the current tstate. It's better than nothing. */
final_tstate = tstate;
}
else {
_PyThreadState_Bind(final_tstate);
}
}
assert(final_tstate != NULL);

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

Expand Down

0 comments on commit fe12f40

Please sign in to comment.