Skip to content

The C API needs to protect state that can be deleted by finalization #134307

Closed as not planned

Description

@ZeroIntensity

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:

int
PySomething_Whatever(PyInterpreterState *interp)
{
    if (_PyInterpreterState_CriticalSection(interp) < 0) {
        // Interpreter has finalized
        return -1;
    }

    /* Do critical operation */

    _PyInterpreterState_EndCriticalSection(interp);
    return 0;
}

Does this approach seem reasonable to everyone?

cc @ericsnowcurrently

Activity

  1. ZeroIntensity commented on May 20, 2025

    @ZeroIntensity
    MemberAuthor

    cc @colesbury as well

  2. kumaraditya303 commented on Jul 23, 2026

    @kumaraditya303
    Contributor

    @ZeroIntensity should this be closed in favor of https://peps.python.org/pep-0788/?

  3. ZeroIntensity commented on Jul 23, 2026

    @ZeroIntensity
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions