From defc4cfda6a85aef8c115fb50366a30c4da949a3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 3 Dec 2026 10:56:45 +0100 Subject: [PATCH] Report "finalizing" in Py_FatalError() during Py_Finalize() (#163059) If Py_FatalError() is called in an early state of Py_Finalize(), report the state "finalizing" from the interpreter state. --- Python/pylifecycle.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 83d5f43f8991e3..d5ed63472ddfe8 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -3136,7 +3136,8 @@ fatal_output_debug(const char *msg) static void -fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime) +fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime, + PyInterpreterState *interp) { PUTS(fd, "Python runtime state: "); PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime); @@ -3145,6 +3146,9 @@ fatal_error_dump_runtime(int fd, _PyRuntimeState *runtime) _Py_DumpHexadecimal(fd, (uintptr_t)finalizing, sizeof(finalizing) * 2); PUTS(fd, ")"); } + else if (interp != NULL && interp->finalizing) { + PUTS(fd, "finalizing"); + } else if (runtime->initialized) { PUTS(fd, "initialized"); } @@ -3336,9 +3340,6 @@ fatal_error(int fd, int header, const char *prefix, const char *msg, PUTS(fd, "\n"); } - _PyRuntimeState *runtime = &_PyRuntime; - fatal_error_dump_runtime(fd, runtime); - /* Check if the current thread has a Python thread state and holds the GIL. @@ -3356,8 +3357,11 @@ fatal_error(int fd, int header, const char *prefix, const char *msg, else if (tss_tstate != NULL) { interp = tss_tstate->interp; } - int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate); + _PyRuntimeState *runtime = &_PyRuntime; + fatal_error_dump_runtime(fd, runtime, interp); + + int has_tstate_and_gil = (tss_tstate != NULL && tss_tstate == tstate); if (has_tstate_and_gil) { /* If an exception is set, print the exception with its traceback */ if (!_Py_FatalError_PrintExc(tss_tstate)) {