bpo-36479: Exit threads when interpreter is finalizing rather than runtime by nanjekyejoannah · Pull Request #12679 · python/cpython (original) (raw)
@vstinner PyFinalize_Ex
kills the entire runtime, so we can't call it just because a subinterpreter went away. Instead, we have to migrate cleaning up particular resources from PyFinalize_Ex
to Py_EndInterpreter
as this PR does.
However, I also agree that for this PR to work there needs to be a new field in the individual interpreter state that tracks which thread is cleaning up that particular interpreter.
And then once we have that per-interpreter field and are using it consistently, then we can ask if we still need a separate runtime level "finalizing" marker, or if the finalizing marker for the main interpreter will be sufficient (which will mean addressing the fact that Py_EndInterpreter
never actually gets called on the main interpreter - Py_FinalizeEx
has its own copy of the pieces it wants to use, and does a bunch of other more destructive things while keeping at least one thread state alive until the last possible moment).