/tiran-skills:port-to-free-threaded-pythonfrom tiran-skills
Makes Python packages work correctly on free-threaded (no-GIL) CPython builds (PEP 703). Covers pure-Python thread-safety, C/C++/Cython/pybind11/nanobind/Rust-PyO3 extension support, wheel building, and testing with ThreadSanitizer.
Ships in tiran-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Free-threaded CPython (PEP 703; experimental in 3.13, officially supported from 3.14
per PEP 779) runs without the GIL, so threads execute Python in parallel. Target the
3.14t build. The catch: the GIL used to serialize every operation, so
code that was "accidentally thread-safe" no longer is. This skill takes a package to
correct behavior under the free-threaded build and to declaring support, which
are two separate jobs — an extension that imports fine can still silently re-enable
the GIL, and one that declares support can still race.
Read ../GUARDRAILS.md first. It governs installs, builds, and
not committing/pushing without approval.
Scope. Pure-Python packages need only steps 1–3 and 8. Packages that ship a
compiled extension need all steps. This skill is language-agnostic at the binding
level (C/C++, Cython, pybind11, nanobind, Rust/PyO3); it does not teach the ABI
mechanics of a single-wheel abi3t build — for that, run
port-to-python-limited-api (PEP 803 + PEP
793) after this one. Thread-safety and ABI stability are orthogonal.
cp313t/cp314t
wheels (the norm today), or — only once 3.15 is your floor — a single
abi3.abi3t wheel via the limited-API skill.Install a free-threaded interpreter and confirm the GIL is actually off:
uv python install 3.14t # 3.14t is the baseline; 3.13t was experimental
uv run --python 3.14t python -c "import sys; print(sys._is_gil_enabled())" # -> False
Then import your package under it and watch for the warning
"The global interpreter lock (GIL) has been enabled to load module ...". If it
prints, the extension has not declared support and every user silently loses
parallelism (step 4 fixes it). sysconfig.get_config_var("Py_GIL_DISABLED") == 1 is
the build-time check; sys._is_gil_enabled() is the runtime one.
Applies to every package. The GIL used to make these safe; now they race. See
reference/thread-safety.md.
contextvars.ContextVar for per-logical-thread state (asyncio-aware);
threading.local only for genuine per-OS-thread state.threading.Lock/RLock as context managers around read-modify-write
on shared objects.This is the single highest-value change for an extension: it stops the GIL being
re-enabled. It is a promise, so only make it after (or alongside) steps 3 and 5.
See reference/declaring-support.md.
{Py_mod_gil, Py_MOD_GIL_NOT_USED} slot
(version-guarded, so it still compiles on 3.12).PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED) under #ifdef Py_GIL_DISABLED.# cython: freethreading_compatible=True (3.1+); pybind11
py::mod_gil_not_used(); nanobind FREE_THREADED CMake flag; PyO3 free-threaded
by default since 0.28 (write gil_used = true only to opt out; 0.23–0.27 need
gil_used = false). All declare support only — none makes code safe.Programming Language :: Python :: Free Threading :: 3 - Stable (or 2 - Beta /
1 - Unstable) so users can see the status.For a compiled extension, detection is the primary deliverable. Inventory every
piece of state reachable from more than one thread and classify each finding, then act
within the scope boundary below. Detection heuristics and the fix patterns are in
reference/thread-safety.md.
Safe to fix in place (local, self-contained; patterns and caveats in the reference):
PyList_GetItemRef,
PyDict_GetItemRef, PyWeakref_GetRef; backport via pythoncapi-compat). Don't
blanket-convert — arguments and new references stay safe.<stdatomic.h> (C++/Rust std) with the
weakest correct memory order, #ifdef Py_GIL_DISABLED-guarded. Anything wider than
one word — a CAS loop or a hand-rolled lock-free structure — is a report, not a fix.Py_BEGIN_CRITICAL_SECTION) or a static PyMutex. Mind the two rules: never nest to
lock two objects (use the _2 form); a section is suspended across blocking calls.check_c_api_usage.py) so these
fixes can't silently regress.Detect and report to the user — do NOT attempt (out of scope; these need design judgement and can introduce subtle deadlocks or performance cliffs):
For each such finding, tell the user where (file:line), what state is shared, why it races without the GIL, and the candidate fix — then stop and let them decide. Legitimate outcomes for this category, not just "add a lock" (scipy does all three): detect-and-refuse concurrent use with a clear error, or document a subsystem as not thread-safe and quarantine its tests. Report these clearly rather than declaring the port done.
The wheel tag follows the interpreter you build with — build with a t interpreter
and you get a cp3Xt wheel. See
reference/build-and-test.md for meson-python,
scikit-build-core, maturin, and setuptools specifics.
CIBW_ENABLE=cpython-freethreading (or enable = ["cpython-freethreading"]).Py_GIL_DISABLED-guarded support declaration from step 4 is compiled in.abi3.abi3t-tagged wheel
(a *.abi3t.so) loads on both the standard (GIL) and free-threaded interpreter
from 3.15 up — no more separate cp3Xt build. That ABI work (opaque PyObject +
PEP 793 PyModExport) lives in
port-to-python-limited-api; pair it with
this skill. Until your floor is 3.15, ship version-specific cp314t wheels.Concurrency bugs don't show single-threaded. See
reference/build-and-test.md.
pytest-run-parallel (--parallel-threads=auto) runs the existing suite from
many threads at once — the cheapest way to shake out races.-fsanitize=thread and run the suite;
it finds data races the tests don't assert on. Use a TSan-instrumented CPython
(and instrument native deps like OpenBLAS too) — running TSan against a stock
free-threaded interpreter floods false positives. Test on ARM too — some races
only manifest on weakly-ordered CPUs.sys._is_gil_enabled() stays False.pytest-run-parallel and TSan is clean on x86 and ARM.cp313t/cp314t tag (or abi3.abi3t), and the classifier
advertises the support level you actually verified — on every version you claim.3 - Stable) while known races are unresolved — use 1 - Unstable/2 - Beta or
leave the declaration off until the maintainer addresses them.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
free-threaded-python, no-gil, pep-703, cpython, thread-safety, c-extension
Other plugins' skills for the same job
12 skills in this plugin
/bootstrap-python-projectScaffold a new Python project from zero: src/ layout, PEP 621 metadata, a chosen build backend (hatchling for pure Python; meson-python / scikit-build-core / maturin for compiled) with dynamic VCS versioning, py.typed, ruff, a type checker, pytest + coverage, tox or nox via uv, and hardened GitHub Actions CI (SHA-pinned, Trusted Publisher, CodeQL/Scorecard/Dependabot). Always writes a README, LICENSE, and .gitignore. Orchestrates the choose-python-build-backend, modernize-python-metadata, ship-type-information, and secure-python-release-pipeline skills. Use when asked to start a new package, scaffold/bootstrap a Python project, set up a repo from zero, turn a loose script into a proper package, or bring an existing project up to this standard (adopt the layout, metadata, tooling, or CI it lacks)./choose-python-build-backendRecommend the right PEP 517 build backend for a new or existing Python package, and give enough context to migrate. Chooses by three axes: purelib (pure Python) vs platlib (compiled C/C++/Rust/Torch), static vs dynamic/VCS metadata, and native language/build system. Covers uv-build, flit-core, hatchling (incl. compiled via scikit-build-core[hatchling] / a custom build hook), meson-python, scikit-build-core, maturin, and setuptools (legacy). Weighs compiled-project concerns: Cython, PyTorch extensions, pybind11/nanobind bindings, the stable Python ABI (abi3 / limited API), and fast parallel builds (CMake+Ninja, Meson). Use when asked "which build backend should I use", to pick a backend for a new project, or to decide whether to move an existing project off setuptools.npx claudepluginhub tiran/agent-skills --plugin tiran-skills