Audit Native Extensions for Free Threading
Core idea
CPython 3.13 provides an experimental free-threaded build. Build capability and current GIL state are different: sysconfig.get_config_var("Py_GIL_DISABLED") describes build configuration and sys._is_gil_enabled() reports current state in CPython 3.13. Extensions must explicitly declare support after auditing shared state, borrowed references and locking. Unsupported imports may enable the GIL in a free-threaded build.
import sys,sysconfigprint(bool(sysconfig.get_config_var('Py_GIL_DISABLED')))print(sys._is_gil_enabled())The local CPython 3.13.5 build is ordinary and GIL-enabled; no free-threaded runtime was executed.
Expected output:
FalseTrueProtect a compound operation
from threading import Lock,Threadlock=Lock(); values=[]def worker(): with lock: values.append(len(values))threads=[Thread(target=worker) for _ in range(3)]for t in threads: t.start()for t in threads: t.join()print(values)The lock protects a compound read/update. This ordinary-build example illustrates the contract, not a free-threaded stress test.
Expected output:
[0, 1, 2]Common mistake
Do not declare an extension free-thread-safe merely because its tests pass under the GIL. In 3.13 audit strong-reference APIs, critical sections, allocator domains and module initialization; test with an actual compatible free-threaded build.
Practice
Write a review checklist for shared caches, borrowed container items, module state, locking and dependency wheels. Separate documentation evidence from actual free-threaded test results.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Free threading and native compatibility
Check your understanding of free threading and native compatibility.
What is printed?
import sysprint(sys._is_gil_enabled())What is printed?
from threading import Lockl=Lock()with l: print(l.locked())Does a GIL-enabled test prove free-thread safety?
Which describes build capability rather than live state?