Sub topic 11
C extensions and the GIL re-enabling rule
This chapter assumes you know what the GIL is, how to detect a free-threaded build, and how PYTHON_GIL and -X gil work. It also assumes you have a basic idea of what a compiled extension module is.
Why extensions are the hard part
Pure Python code runs on a free-threaded interpreter without changes. Compiled extension modules are different. An extension is a shared library (.so or .pyd) written in C, C++, Rust, or Cython. Many of them were written on the assumption that the GIL makes their code safe. They keep global state in plain C variables, and they mutate Python objects without locks. With the GIL gone, two threads can enter that code at once and corrupt memory.
CPython cannot inspect a compiled library to see whether it is thread-safe. So it uses an opt-in declaration.
The re-enabling rule
On a free-threaded build, the interpreter starts with the GIL off. When you import an extension module, it checks whether the module declared support for running without the GIL.
- If the module declared support, the GIL stays off.
- If it did not, the interpreter turns the GIL back on for the rest of the process and emits a
RuntimeWarning.
Once the GIL is re-enabled this way, it is not turned off again. Your threads stay correct, but you lose the parallelism you installed the free-threaded build to get.
Setting PYTHON_GIL=0 or -X gil=0 overrides the check. The interpreter then keeps the GIL off even after importing an extension that did not opt in. This is the right setting for testing, and a risky one for production.
# Default: the GIL turns on if an unready extension is imported
python3.13t app.py
# Keep the GIL off regardless. Only do this for testing.
PYTHON_GIL=0 python3.13t app.pyHow an extension opts in
Which mechanism an extension uses depends on how it is initialized.
- Modules that use multi-phase initialization declare the slot
Py_mod_gilwith the valuePy_MOD_GIL_NOT_USED. - Modules that use single-phase initialization call
PyUnstable_Module_SetGIL(module, Py_MOD_GIL_NOT_USED). - Cython 3.1 and later can generate the declaration with the directive
freethreading_compatible=True. - For Rust with PyO3, a module declares support with
#[pymodule(gil_used = false)].
The declaration is a promise by the author. It does not make the code thread-safe by itself. The author must first audit global state, add locks or use thread-local storage, and check every place where the code assumes the GIL protects an object.
Checking a module at runtime
You can see the result of the rule from Python.
import sys
print("GIL before import:", sys._is_gil_enabled())
import some_extension # replace with a real compiled module
print("GIL after import:", sys._is_gil_enabled())If the second line prints True and you saw a warning on import, that module did not opt in. To turn warnings into hard failures during testing, run with -W error::RuntimeWarning. The import then raises an exception instead of silently enabling the GIL.
Assessing a dependency
Use this order of checks before you adopt free-threading for an application.
- Check the project's release notes or documentation for free-threaded support. Look for wheels tagged
cp313t. - Install on a free-threaded build and import the package. Watch for the
RuntimeWarning. - Check
sys._is_gil_enabled()after the import. - Run the package's own tests under multiple threads.
- If the package is not ready, keep it out of the threaded process, or run it in a separate process.
Exercise
You run a program on a free-threaded build. It imports a compiled package that has not declared support for running without the GIL. What happens to the GIL, and what two ways can you use to keep it off while testing?
Show answer
The interpreter re-enables the GIL for the rest of the process and emits a RuntimeWarning, so threads no longer run in parallel. To keep it off for testing, set the environment variable PYTHON_GIL=0 or pass -X gil=0. This is risky because the extension may not be thread-safe.