Skip to content

Forcing enabling the GIL doesn't seem to work with PEP 793 initialization #141780

Description

@da-woods

Bug report

Bug description:

#include <Python.h>

static int
examplemodule_exec(PyObject *module) {
    // Yes I know there's no error checking...
    PyObject *gil_enabled = PySys_GetObject("_is_gil_enabled");
    PyObject *result = PyObject_CallNoArgs(gil_enabled);
    printf("gil enabled: ");
    PyObject_Print(result, stdout, 0);
    printf("\n");
    Py_DECREF(result);
    return 0;
}

static PyModuleDef_Slot examplemodule_slots[] = {
    {Py_mod_name, "examplemodule"},
    {Py_mod_gil, Py_MOD_GIL_USED},
    {Py_mod_exec, (void*)examplemodule_exec},
    {0}
};

PyMODEXPORT_FUNC
PyModExport_examplemodule(void)
{
    return examplemodule_slots;
}

I believe that the code above should print "gil enabled: True". However when I run it on Python 3.15.0a2 freethreading it prints "gil enabled: False".

I don't believe this is an issue in the old style PyInit_ initialization.

CPython versions tested on:

3.15

Operating systems tested on:

Linux

Linked PRs

Activity

  1. changed the title [-]Disabling the GIL doesn't seem to work with PEP 793 initialization[/-] [+]Forcing enabling the GIL doesn't seem to work with PEP 793 initialization[/+] on Nov 20, 2025
  2. da-woods commented on Nov 20, 2025

    @da-woods
    ContributorAuthor
  3. self-assigned this
    on Nov 20, 2025
  4. added a commit that references this issue on Nov 20, 2025
  5. encukou commented on Nov 20, 2025

    @encukou
    Member

    Thanks for the catch!

  6. added a commit that references this issue on Nov 24, 2025
  7. da-woods commented on Nov 24, 2025

    @da-woods
    ContributorAuthor

    Thanks

  8. added a commit that references this issue on Dec 6, 2025
  9. added a commit that references this issue on Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.15bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions