Raise a detailed error the moment an import re-enables the GIL on free-threaded Python.
You moved the service to free-threaded Python. You gave the hot path a thread pool. You deployed, pulled up the dashboard, and watched the throughput line do absolutely nothing. Not slower. Not faster. The same shape it had last week, as if the whole migration had been a dream.
An hour later you find it: one line in a startup log that scrolled past in half a second.
RuntimeWarning: The global interpreter lock (GIL) has been enabled to load module
'gilneeder._speedups', which has not declared that it can run safely without the GIL.
That line is CPython being careful. Somewhere in your import tree, an extension module never declared that it’s safe without the GIL, so the interpreter turned the lock back on for the rest of the process and carried on. Your threads still run. They just take turns again. The warning names a private module you’ve never heard of, not the package you installed or the line that imported it, and nothing can turn the GIL back off.
gilguard turns that line into an exception, raised at the import statement that caused it, with the culprit named.
uv pip install gilguardOne import, first thing
import gilguard # must be first
import fastapi
import pandas # <- raises here if anything in pandas' import tree needs the GILWhen something in that tree needs the GIL, the program stops right there and tells you who did it, where it lives, and how it got in:
gilguard.GILEnabledError: Importing 'gilneeder._speedups' re-enabled the GIL.
Module: gilneeder._speedups
File: .../site-packages/gilneeder/_speedups.cpython-314t-x86_64-linux-gnu.so
Distribution: gilneeder 1.2.0
Import chain: gilneeder -> gilneeder._speedups
Your code: /home/me/app.py:5
Imported by: .../site-packages/gilneeder/__init__.py:1
This extension module does not declare free-threading support (Py_mod_gil), so CPython
turned the GIL back on for the rest of the process. It cannot be turned off again.
Options:
- Upgrade gilneeder to a release that ships free-threaded (cp314t) wheels.
- Drop or replace gilneeder.
- If you have verified it is thread-safe, run with PYTHON_GIL=0 to keep the GIL off (at your own risk).
- Run `python -m gilguard` to list every installed package that does this.
gilneeder is one of gilguard’s test fixtures, a tiny C extension built to misbehave so the real ones don’t have to. The same report comes back as data on exc.report, so you can log it, send it to your error tracker, or fail a build on it.
What you get
The culprit, by name
The module, its file on disk, the distribution and version that shipped it, and the full import chain from your code down to the offending extension. No bisecting your imports at midnight.
An error you can’t swallow by accident
GILEnabledError derives from BaseException, the same tier as KeyboardInterrupt. The usual try: import x / except ImportError: fallback can’t catch it, so no optional import anywhere in your dependencies can quietly carry on with the GIL on.
A lineup of every offender
A process can only catch the first flip, because after that the GIL is already on. python -m gilguard gives every installed package its own fresh interpreter and lists all of them. It exits 1 when it finds one, so it drops straight into CI.
Warn mode when you’re not ready to fail
Set GILGUARD_ACTION=warn and the same report lands as a warning while the program keeps running. Set it to off to run the same code on a standard build.
Try it before you switch
Your project doesn’t need to be on a free-threaded build yet. uv can stand up a throwaway 3.14t environment with your dependencies in it and run the scan there:
uv run --isolated --python 3.14t --with gilguard python -m gilguardOne command, your real venv untouched, and a list of every dependency that would turn the GIL back on. Swap in 3.15t or 3.16t to see how the next release is shaping up.
Where to go next
- Getting started covers installing it and where the import goes.
- Reading the report explains each field and the three reasons the GIL can be on.
- Fixing what it finds is what to do once you have a name: upgrade, report, replace, or declare support in your own extension.
- Auditing an environment is the place to start if you’re moving an existing app to free-threaded Python.
- Tests and CI shows how to catch offenders before they reach production.
- Configuration covers
raise,warnandoff, and howPYTHON_GILinteracts with them. - How it works explains the import hook, the backstop, and why the error is a
BaseException. - The API reference documents every public name.
gilguard is MIT licensed and has no dependencies. It needs Python 3.13 or newer, and does its real work on a free-threaded build (3.13t, 3.14t, 3.15t).