You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The C API needs to protect state that can be deleted by finalization #134307
In the C API, we have some functions that need to access state that is deleted by finalization. For example, PyGILState_Ensure accesses the gilstate, and that can be concurrently deallocated. In that specific case, I proposed #132510 as a fix, but really, we should have a better way to protect state against a finalizing interpreter. I'm not sure whether to categorize this as a bug or a feature.
I think the best way to do this is through a finalizing counter similar to how PEP-788 proposes preventing interpreter shutdown. So, for functions that don't require an attached thread state, we need something like:
intPySomething_Whatever(PyInterpreterState*interp)
{
if (_PyInterpreterState_CriticalSection(interp) <0) {
// Interpreter has finalizedreturn-1;
}
/* Do critical operation */_PyInterpreterState_EndCriticalSection(interp);
return0;
}
Yes. The original hope was that we could backport some of the PEP 788 APIs as internal (so we could fix bugs on 3.14), but I think they're too complicated.
Activity
ZeroIntensity commentedon May 20, 2025
cc @colesbury as well
kumaraditya303 commentedon Jul 23, 2026
@ZeroIntensity should this be closed in favor of https://peps.python.org/pep-0788/?
ZeroIntensity commentedon Jul 23, 2026
Yes. The original hope was that we could backport some of the PEP 788 APIs as internal (so we could fix bugs on 3.14), but I think they're too complicated.