Skip to content

ADR 0022: A generated extension module declares Py_MOD_GIL_NOT_USED

Status: Final Type: Feature Created: 2026-07-29 Amends: 0002 (the --ext-module artifact); its open ufunc-exception item was found already closed (see Consequences) Related-ADRs: 0019 (the free-threaded embed direction), 0007, 0011

Context

A CPython extension module that does not declare free-threading support re-enables the GIL for the process when imported on a free-threaded build. The GIL returns for all extensions already loaded, permanently, with one RuntimeWarning and no other sign. A project that went to the trouble of running free-threaded and imported one compiled ufunc module silently got a GIL build back: a worse failure than a slow one, because everything keeps working.

The fix is one line; the claim is the work. The declaration states that each runtime unit linked into the artifact is safe under concurrent calls with no GIL held; declaring it falsely is worse than the conservative status quo, because it removes the protection CPython still provides.

Decision

The audit precedes the declaration. Its surface is what an --ext-module build links (established from the build path, which assembles the unit set explicitly; pygo is structurally excluded because the extension builder refuses concurrency programs). Each piece of file-scope mutable state in that set falls into one of four categories. The taxonomy is the durable checklist:

category verdict
thread-local (the exception slot and save stack) safe
mutex-protected (the Python-callback cache) safe; the write is inside the critical section and readers take the lock
const, statically initialised (name/parent tables, ufunc descriptors, moduledefs) safe
written once during PyInit, read forever (the NumPy API tables, under the import lock; the module is never unloaded) safe

The paths never previously audited (the Str, array/list, and emitted ufunc loops) hold no static state at all: pure over automatic storage plus the allocator, with POST having no module-level mutable variables to begin with. Refcounting touches only owned references (the borrowed-callback-argument incref is owned-and-leaked, the safe direction; the singletons increfed are immortal on a free-threaded build). Each C-API entry point is bracketed by the generated GIL discipline of 0019. No non-reentrant libc is used anywhere.

One thing was not safe: the terminator. exit() called concurrently or re-entered is undefined behaviour. The terminator is reachable from several threads the moment anything is: the stable-ABI wrappers, and a ufunc inner loop on NumPy's own workers. It is now once-only: the winner prints and calls exit(1), each loser calls _Exit(1), which races with nothing; the process dies once and prints once. The flag this uses is the only process-wide mutable state in an extension module. A standing structural scan of the runtime units' text checks that, and names the process-wide state an executable may hold besides: the random generator and the sys.argv list of 0059, and the module instance of 0060. The generator is accessed under its own lock; the list is written once by an executable's driver before the program runs and only read after. All are linked into an executable only, since a library or an extension module that uses one is refused at build time, so this declaration never covers them. A new piece of process-wide state would falsify the declaration with nothing else failing.

The declaration is made under two compile-time conditions: the interpreter is free-threaded, and the toolchain provides real thread-local storage. The TLS macro's fallback branch defines the storage away with only a compile-time warning. That degradation was acceptable until a thread-safety claim stood on it; a claim true on two of three toolchains is the worst kind of false one. So the declaration withdraws itself wherever the storage does. The atomics guard shares the shape: a toolchain without C11 atomics has already lost thread-locals, so it has already withdrawn.

Consequences

  • Importing a compiled ufunc module on a free-threaded interpreter no longer re-enables the GIL, verified in both directions: with the declaration removed, the same test observes the GIL return and the warning print, so the test can fail.
  • Nothing changes on a GIL build; the conditional compiles the call out.
  • The claim's guards are structural: the process-wide-state scan, the two compile-time conditions, and the single definition of the TLS macro. Prose could hold none of them in place.
  • _Exit on a losing thread skips atexit handlers and stream flushes, correctly: the winner has already flushed and printed, so the loser has nothing to say.
  • The open item this ADR originally listed (a ufunc inner loop ignoring a pending exception) turned out to be already closed. The frontend has terminated in a kernel's propagate block since 0007 (a vectorized kernel cannot propagate, so it reports and exits), and tests now pin the behaviour in both build modes. The entry described a defect the compiler did not have; it lacked a test.

Alternatives considered

  • Not declaring (the conservative status quo): rejected once the audit existed; the silent process-wide GIL re-enable is a worse failure than any the declaration risks, given the audit and its standing guards.
  • Declaring without the TLS guard: rejected; on the fallback toolchain the exception slot becomes process-wide, a real data race underneath a written claim of thread safety.