Text of discuss.python.org › t › concerns-about-pyunstable-module-setgil › 89374 as Styx crawled it on Nov 20, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Concerns About PyUnstable_Module_SetGIL() - Core Development - Discussions on Python.org

Concerns About PyUnstable_Module_SetGIL() Core Development eric.snow (Eric Snow) April 23, 2025, 6:32pm 1 (This is a response to a comment in a separate thread discussing a soft deprecation of support for single-phase init extension modules, which I don’t want to get distracted by free-threading stuff. ) PyUnstable_Module_SetGIL() was added unofficially . The only discussion about it was my objection to it [1] a year ago in the PR that added it (with an ensuing temporary compromise).

It was added strictly only so folks can try out the free-threading build sooner (and originally named PyModule_ExperimentalSetGIL() to reflect this). From an official standpoint, PEP 703 only proposes support for multi-phase init modules (via Py_mod_gil ). Thus, PyUnstable_Module_SetGIL() (or PyModule_SetGIL() ) should not be treated as official until it is added to PEP 703 and gets appropriate attention and discussion.

It should not be a recommended tool until then, outside of experimentation. (That was kind of the point of the original PyModule_ExperimentalSetGIL() name.) My original concerns remain. Furthermore, it feels like they were mostly ignored when I first brought them up and that they are still considered inconvenient. --------------------- To be clear, my main concern is that Py*Module_SetGIL() is both unnecessary and reinforces use of an outdated, legacy feature (single-phase init) which we would like to eliminate.

My comments in the original PR thread cover things fairly well. (See gh-116322: Add Py_mod_gil module slot by swtaarrs · Pull Request #116882 · python/cpython · GitHub ) IMHO, Py*Module_SetGIL() should be treated as strictly temporary and eventually removed. It should not be advertised as part of the long-term solution. Clearly users are under the impression that it is. That’s exactly what I expected would happen and why I pushed back on the original addition.

At the time it was added, the argument for Py*Module_SetGIL() was that the multi-phase init requirement was getting in the way of people trying out the free-threading build and thus providing feedback. I definitely understand and relate to such barriers to adoption all too well. Furthermore, I was on board with adding PyModule_ExperimentalSetGIL() as a temporary workaround. However, it’s getting entrenched. [2] Renaming it to PyUnstable_Module_SetGIL() made it worse.

Just because we’re worried about barriers to adoption, however minor, it doesn’t mean we have to cut corners. That’s not how we operate. [3] The free-threading project should not be given special treatment. The zen of Python is quite appropriate here. So what do I suggest is the long-term solution? It’s the already proposed (and implemented) multi-phase init module slot, Py_mod_gil . Extension maintainers can use it right now.

I’m not recommending some new invention. You might say “but then users have to implement multi-phase init!” For one thing, that’s a good thing. For another, that concern represents a misunderstanding (which we’re discussing in another thread ), where people have conflated multi-phase init with isolation (which involves switching to module state and heap types). [4] Just implementing multi-phase init is fairly trivial, as I noted a year ago .

I’ll post an elaborating comment in a moment. With all that in mind, I don’t thing Py*Module_SetGIL() is needed. I do think it should be removed. https://github.com/python/cpython/pull/116882#discussion_r1575006053 ↩︎ With the status quo, eventually we’ll end up keeping Py*Module_SetGIL() , without a deliberate public decision to do so, because “people are used to it” and we don’t want to inconvenience them.

That’s a mistake we can avoid. ↩︎ Otherwise I’d have done a number of things quite differently with my own projects. ↩︎ Implementing extension module isolation is a very good thing, but if a module uses static types or (C) global variables then the effort to isolate becomes non-trivial. That said, switching to multi-phase init remains trivial. ↩︎ 8 Likes Concerns About Process With the Free-Threading Build and PEP 703 Proposal: Officially Deprecate Support for Legacy (Single-Phase Init) Extension Modules Proposal: Officially Deprecate Support for Legacy (Single-Phase Init) Extension Modules eric.snow (Eric Snow) April 23, 2025, 6:34pm 2 As I noted, implementing multi-phase init is fairly trivial [1] : move the content of the module’s init function to a corresponding “module exec” function (dropping the call to create the module object) set the module def’s m_slots field to an array with: a Py_mod_exec slot, set to the new module exec function a Py_mod_multiple_interpreters slot [2] , set to Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED [3] to try free-threading, a Py_mod_gil slot [4] , set to Py_MOD_GIL_NOT_USED set def.m_size to 0 (if negative) update the module init function to only return PyModuleDef_Init(def); for the corresponding module def (Also see Module Objects — Python 3.13.3 documentation and https://docs.python.org/3/howto/isolating-extensions.html. ) There’s no need to do any of the isolation stuff (heap types, module state, etc.) immediately.

At that point, the only extra step to run on a free-threading build would be to add the Py_mod_gil slot, set to Py_MOD_GIL_USED , etc. IMHO, the free-threading build should always treat single-phase init modules as though they had Py_MOD_GIL_USED set. my original outline of the steps: https://github.com/python/cpython/pull/116882#discussion_r1586449047 ↩︎ the default is Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED ↩︎ think of Py_mod_multiple_interpreters as a hypothetical Py_mod_isolated , where Py_MOD_PER_INTERPRETER_GIL_SUPPORTED means “isolated” and Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED means “not isolated” ↩︎ the default is Py_MOD_GIL_USED ↩︎ 6 Likes Proposal: Officially Deprecate Support for Legacy (Single-Phase Init) Extension Modules da-woods (Da Woods) April 23, 2025, 7:56pm 3 From a Cython point of view we don’t really need this function, so I’m pretty relaxed if it gets removed.

We do use it if people declare purposefully make the module single-phase init and declare free-threading compatibility. But I don’t see why anyone would. However - I’m not really sure I understand the problem. There doesn’t seem anything intrinsic about free-threading which really needs multi-phase init modules. So this seems more like using people’s desire to support free-threading to push them towards multi-phase initialization rather than something that’s really needed.

It’d probably be useful to find out why things like PyO3 and PyBind11 have been slow to adopt multi-phase initialization? (My feeling is that no-one’s really pushed it for PyBind11, and the Rust culture of “everything should be safe” maybe discourages experimental WIP implementations. But this is complete, reading-between-the-lines guesswork from me). But either way, both seem to have prioritised free-threading rather than multi-phase initialization, and that decision seems reasonable to me (as an outsider to both projects).

1 Like ngoldbaum (Nathan Goldbaum) April 23, 2025, 8:25pm 4 I think it’s more that it’s a big change to very complicated procedural macros that are generating unsafe FFI code, and someone needs to carry the change over the finish line. See RFC: Implement Multi-Phase Module Initialization as per PEP 489 by Aequitosh · Pull Request #4162 · PyO3/pyo3 · GitHub . 2 Likes davidhewitt (David Hewitt) April 23, 2025, 8:31pm 5 Exactly this.

I am keen to move PyO3 to multi-phase init, just have had to balance this with all other ongoing work. If it’s a priority for CPython to get that done, that helps me focus where I need to be putting efforts. 3 Likes da-woods (Da Woods) April 23, 2025, 8:34pm 6 Fair enough - I stand corrected. That does seem to contradict of course (at least in some cases). 1 Like eric.snow (Eric Snow) April

The rest of this page is on the live site.