@@ -1788,6 +1788,7 @@ Py_FinalizeEx(void)
1788
1788
1789
1789
/* Remaining daemon threads will automatically exit
1790
1790
when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
1791
+ _PyInterpreterState_SetFinalizing (tstate -> interp , tstate );
1791
1792
_PyRuntimeState_SetFinalizing (runtime , tstate );
1792
1793
runtime -> initialized = 0 ;
1793
1794
runtime -> core_initialized = 0 ;
@@ -2142,6 +2143,10 @@ Py_EndInterpreter(PyThreadState *tstate)
2142
2143
Py_FatalError ("not the last thread" );
2143
2144
}
2144
2145
2146
+ /* Remaining daemon threads will automatically exit
2147
+ when they attempt to take the GIL (ex: PyEval_RestoreThread()). */
2148
+ _PyInterpreterState_SetFinalizing (interp , tstate );
2149
+
2145
2150
// XXX Call something like _PyImport_Disable() here?
2146
2151
2147
2152
_PyImport_FiniExternal (tstate -> interp );
@@ -2152,6 +2157,18 @@ Py_EndInterpreter(PyThreadState *tstate)
2152
2157
finalize_interp_delete (tstate -> interp );
2153
2158
}
2154
2159
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
+
2155
2172
/* Add the __main__ module */
2156
2173
2157
2174
static PyStatus
0 commit comments