@@ -1788,6 +1788,7 @@ Py_FinalizeEx(void)
17881788
17891789 /* Remaining daemon threads will automatically exit
17901790 when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
1791+ _PyInterpreterState_SetFinalizing (tstate -> interp , tstate );
17911792 _PyRuntimeState_SetFinalizing (runtime , tstate );
17921793 runtime -> initialized = 0 ;
17931794 runtime -> core_initialized = 0 ;
@@ -2142,6 +2143,10 @@ Py_EndInterpreter(PyThreadState *tstate)
21422143 Py_FatalError ("not the last thread" );
21432144 }
21442145
2146+ /* Remaining daemon threads will automatically exit
2147+ when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
2148+ _PyInterpreterState_SetFinalizing (interp , tstate );
2149+
21452150 // XXX Call something like _PyImport_Disable() here?
21462151
21472152 _PyImport_FiniExternal (tstate -> interp );
@@ -2152,6 +2157,18 @@ Py_EndInterpreter(PyThreadState *tstate)
21522157 finalize_interp_delete (tstate -> interp );
21532158}
21542159
2160+ int
2161+ _Py_IsInterpreterFinalizing (PyInterpreterState * interp )
2162+ {
2163+ /* We check the runtime first since, in a daemon thread,
2164+ interp might be dangling pointer. */
2165+ PyThreadState * finalizing = _PyRuntimeState_GetFinalizing (& _PyRuntime );
2166+ if (finalizing == NULL ) {
2167+ finalizing = _PyInterpreterState_GetFinalizing (interp );
2168+ }
2169+ return finalizing != NULL ;
2170+ }
2171+
21552172/* Add the __main__ module */
21562173
21572174static PyStatus
0 commit comments