Data Race Detector for Free-Threading Python
Project description
Threadcheck
Python data race detector for the free-threading (no-GIL) era. Detects concurrent access to shared mutable state in multi-threaded Python programs through static analysis and runtime instrumentation.
Problem
Python 3.14 (2026) introduces free-threading, removing the Global Interpreter Lock (GIL). This enables true parallel execution of multi-threaded code, but the ecosystem lacks debugging tools for concurrency bugs. Go has -race, C++ has ThreadSanitizer, Java has SpotBugs. Python has nothing comparable without recompiling the interpreter with Clang and TSan.
threadcheck is a pure-Python race detector that installs with pip and works out of the box across Linux, Windows, and macOS.
Features
- Static analysis -- scans AST for shared mutable state (
global,nonlocal, class attributes, module-level lists/dicts) and missing lock protection - Runtime detection -- instruments code via AST transformation at import time; tracks memory accesses with vector clocks and detects happens-before violations (read-write and write-write races)
- Lock-aware suppression -- understands
threading.Lock/RLockandwith-based synchronization; supports nested locks (with lock1, lock2:) with automatic per-lock vector clock tracking - Cross-module analysis -- two-pass scan collects
Thread(target=...)andexecutor.submit/maptargets across all files in a directory - Confidence scoring -- each warning tagged HIGH / MEDIUM / LOW based on thread context and lock coverage
- Configuration --
.threadcheckignorefile (gitignore-style patterns +file:linesuppression) and[tool.threadcheck]section inpyproject.toml - Multiple output formats -- terminal (grouped by file, colorized), JSON, SARIF v2.1.0, and self-contained HTML report
- pytest plugin -- automatic race detection during test execution via
--threadcheckflag - Free-threading compatibility checker --
threadcheck compatscans installed packages for C extensions and checks FT ABI tags
Installation
pip install threadcheck
Requires Python 3.12+. Python 3.14+ is recommended for free-threading features.
Quick Start
Static Analysis
Scan a file or directory for potential race conditions without running any code:
threadcheck scan my_project/
Output (grouped by file, with severity icons and per-file summary):
[1/2] my_project/counter.py
--------------------------------
[!] HIGH [unsafe_global] line 8:8
Global variable `counter` modified without lock
suggestion: Use `threading.Lock()` to protect `counter`
[i] LOW [thread_usage] line 10:11
Thread creation detected (target=increment)
[2/2] my_project/worker.py
--------------------------------
[!] HIGH [shared_mutable] line 15:8
Module-level mutable object `results.append()` called from multiple threads
Total: 2 issue(s) in 2 file(s) (0 error(s), 2 warning(s), 0 info(s))
Runtime Detection
Execute a script with instrumentation to detect actual data races:
threadcheck run my_script.py
Output for a racing script:
Data races detected:
[!] `counter`
|-- Thread-28928 (write) at my_script.py:8
|-- Thread-9888 (write) at my_script.py:8
\-- No happens-before relationship between accesses
(10000 overlapping accesses)
A script protected with locks reports:
No data races detected
Free-threading Compatibility Check
Check whether your project's dependencies support free-threading:
threadcheck compat
Output:
threadcheck compat - Free-threading compatibility check
Python 3.13.10
[OK] numpy C extension has free-threading tag (cp313t-)
[??] torch C extension without free-threading tag
[OK] pytest pure Python, no C extensions
Total: 3 package(s) - 2 compatible, 1 need verification, 0 not installed
HTML Report
threadcheck scan my_project/ -o report.html
Generates a self-contained HTML report with dark/light theme, sortable table, and summary cards.
Quiet / Verbose Modes
threadcheck scan my_project/ -q # one-line summary only
threadcheck scan my_project/ -v # include source code snippets
threadcheck scan my_project/ # default grouped output
Configuration
.threadcheckignore
Create a .threadcheckignore file in your project root (gitignore-style patterns):
# Ignore generated files
generated/*.py
build/*.py
# Ignore specific lines in a specific file
src/legacy.py:42 # suppress line 42
src/legacy.py:50-60 # suppress lines 50-60
# Negation (do not ignore)
*.py
!important.py
pyproject.toml
[tool.threadcheck]
ignore = [
"build/*",
"generated/*.py",
]
Both sources are merged automatically.
Output Formats
| Flag | Format | Use Case |
|---|---|---|
| (default) | Terminal (colorized, grouped) | Interactive use |
-q |
One-line summary | Quick status |
-v |
Terminal + source snippets | Debugging |
--json |
JSON | CI pipelines |
--sarif |
SARIF v2.1.0 | GitHub CodeQL integration |
-o report.html |
Self-contained HTML | Team reports |
Commands
| Command | Description |
|---|---|
scan <path> |
Static race analysis of file or directory |
run <script> |
Execute script with runtime race detection |
compat [path] |
Check free-threading compatibility of dependencies |
Library Usage
from threadcheck import analyze_file, analyze_path
warnings = analyze_file("my_module.py")
warnings = analyze_path("src/")
for w in warnings:
print(f"{w.file}:{w.line} [{w.confidence.value}] {w.message}")
from threadcheck.dynamic.tracker import ThreadCheckTracker
from threadcheck.dynamic.transform import transform_and_compile
code = transform_and_compile(source, "script.py")
ThreadCheckTracker.start()
exec(code, {"_threadcheck_tracker": ThreadCheckTracker})
ThreadCheckTracker.stop()
print(ThreadCheckTracker.format_races())
ThreadCheckTracker.reset()
pytest Integration
python -m pytest tests/ --threadcheck
The plugin hooks into pytest_runtest_call and reports race warnings as test failures.
Architecture
Static Analysis Pipeline
- Parse source into AST
- Identify shared mutable state: globals, nonlocals, class attributes (
self.x), module-level mutable objects (lists, dicts, sets) - Detect thread creation sites (
threading.Thread,executor.submit/map) - Cross-reference with lock usage (
with lock:, nestedwith lock1, lock2:) - Assign confidence: HIGH (thread target, no lock), MEDIUM (thread present, no lock), LOW (suspicious pattern, no thread context)
- Report findings with repair suggestions
Runtime Detection Pipeline
- Parse source into AST
- Identify shared variables per function scope (
global,nonlocaldeclarations) - Transform AST: inject
read_before()for reads andwrite_before()for writes of shared variables; injectlock_acquire()/lock_release()aroundwithblocks - Compile and execute transformed code under a tracker that maintains per-thread vector clocks
- On lock acquire, merge the lock's clock into the thread's clock (happens-before)
- On lock release, save the thread's clock to the lock
- After execution, scan access log for conflicting operations (concurrent writes OR write-read pairs with no happens-before relationship)
- Report detected races with thread IDs, source locations, and overlap counts
Free-threading Compatibility Check
- Parse project dependencies from
pyproject.tomlorrequirements.txt - For each installed package, scan for C extension files (
.pyd/.so) - If no C extensions found -> COMPATIBLE
- If C extension filename contains free-threading ABI tag (
cp313t-/cpython-313t-) -> COMPATIBLE - Otherwise -> NEEDS_VERIFICATION
Project Structure
threadcheck/
|-- pyproject.toml
|-- src/threadcheck/
| |-- __init__.py
| |-- __main__.py
| |-- _version.py # single version source
| |-- _tid.py # platform thread ID (swapped per-platform in CI)
| |-- cli.py # argument parsing + dispatch
| |-- config.py # .threadcheckignore + pyproject.toml loader
| |-- pytest_plugin.py # --threadcheck flag for pytest
| |-- static/
| | |-- analyzer.py # static analysis entry point
| | |-- visitors.py # 5 AST visitors
| | |-- lock_tracker.py # lock usage analysis
| | \-- models.py # RaceWarning, Severity, Confidence
| |-- dynamic/
| | |-- __main__.py # run_script entry point
| | |-- transform.py # AST transformation engine
| | |-- tracker.py # runtime tracker with vector clocks
| | |-- clock.py # vector clock implementation
| | \-- hook.py # sys.meta_path import hook
| |-- compat/
| | |-- checker.py # C extension FT tag scanner
| | \-- models.py # FTCompatResult, CompatStatus
| \-- reporting/
| |-- formatter.py # terminal / JSON output
| |-- sarif.py # SARIF v2.1.0 output
| \-- html.py # HTML report output
|-- tests/
| |-- fixtures/ # 11 fixture files with known races
| |-- test_static_analyzer.py
| |-- test_dynamic_detector.py
| |-- test_formatter.py
| |-- test_sarif.py
| |-- test_compat.py
| |-- test_config.py
| \-- test_pytest_plugin.py
|-- demo/
| |-- race_example.py # sample with intentional races
| \-- run_demo.py # demo runner for all output formats
\-- README.md
Platform Support
| Platform | Python | CI |
|---|---|---|
| Linux (x86_64) | 3.12, 3.13, 3.14 | ubuntu-latest |
| Windows (amd64) | 3.12, 3.13, 3.14 | windows-latest |
| macOS (ARM64) | 3.12, 3.13, 3.14 | macos-latest |
Thread IDs: uses native_id (gettid) on Linux/macOS, threading.get_ident() on Windows.
Roadmap
- v0.0.1.2a1 (current): Round A (core gap fill) + Round B (DX) -- static and dynamic analysis, lock tracking, cross-module analysis, pytest plugin, FT compat checker, HTML reports, configuration, enhanced output
- v0.2.0 (next): Round C --
Thread.join()happens-before,threading.Atomicsupport, function call chain tracking, deadlock detection - v1.0.0 (future): Round D -- GitHub Action, pre-commit hook, VS Code integration, stable API
Limitations
- Static analysis may produce false positives (reports a race that cannot occur at runtime) and false negatives (misses races involving indirect sharing through aliases or containers)
- Runtime detection modifies the AST before execution; code that introspects its own source or frame objects may behave differently
- Runtime instrumentation incurs overhead (approximately 2-5x slowdown for typical code)
- Lock tracking supports
threading.Lock,threading.RLock, and standardwith-based patterns; other synchronization primitives (threading.Event,threading.Condition, third-party libraries) are not tracked - Cross-module analysis handles
Thread(target=...)andexecutor.submit/mapbut does not perform full inter-procedural data-flow analysis
License
MIT
Contributing
Contributions are welcome. Please open an issue or submit a pull request on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file threadcheck-0.0.1.2a5.tar.gz.
File metadata
- Download URL: threadcheck-0.0.1.2a5.tar.gz
- Upload date:
- Size: 44.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43e883f5a67ce3457bb9cdc7bf6cbb4afdea408e6d6d2a2a4360bc3bf06b5168
|
|
| MD5 |
8369530d774969967f7bbe2eefd66c63
|
|
| BLAKE2b-256 |
c1f091ed84827219b11742e2fddf29dd0c81d81663a57ec727d45e7086acac85
|
Provenance
The following attestation bundles were made for threadcheck-0.0.1.2a5.tar.gz:
Publisher:
release.yml on ChidcGithub/Threadcheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
threadcheck-0.0.1.2a5.tar.gz -
Subject digest:
43e883f5a67ce3457bb9cdc7bf6cbb4afdea408e6d6d2a2a4360bc3bf06b5168 - Sigstore transparency entry: 1761882368
- Sigstore integration time:
-
Permalink:
ChidcGithub/Threadcheck@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Branch / Tag:
refs/tags/v0.0.1.2a5 - Owner: https://github.com/ChidcGithub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Trigger Event:
push
-
Statement type:
File details
Details for the file threadcheck-0.0.1.2a5-py3-none-win_amd64.whl.
File metadata
- Download URL: threadcheck-0.0.1.2a5-py3-none-win_amd64.whl
- Upload date:
- Size: 33.6 kB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fefd173ec9818980e70d7b3af02b446eed681ba8ae4e66fff5c4aea85275d2e7
|
|
| MD5 |
3793a5c7828565ab6244b6752566136e
|
|
| BLAKE2b-256 |
c81d7962c23ef1be09ca40e337ffb5637afe71f61d4fd63d59cecf31774c793c
|
Provenance
The following attestation bundles were made for threadcheck-0.0.1.2a5-py3-none-win_amd64.whl:
Publisher:
release.yml on ChidcGithub/Threadcheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
threadcheck-0.0.1.2a5-py3-none-win_amd64.whl -
Subject digest:
fefd173ec9818980e70d7b3af02b446eed681ba8ae4e66fff5c4aea85275d2e7 - Sigstore transparency entry: 1761882547
- Sigstore integration time:
-
Permalink:
ChidcGithub/Threadcheck@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Branch / Tag:
refs/tags/v0.0.1.2a5 - Owner: https://github.com/ChidcGithub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Trigger Event:
push
-
Statement type:
File details
Details for the file threadcheck-0.0.1.2a5-py3-none-manylinux_2_28_x86_64.whl.
File metadata
- Download URL: threadcheck-0.0.1.2a5-py3-none-manylinux_2_28_x86_64.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3, manylinux: glibc 2.28+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
daefd793c41a98fc33f1f2498f133ffce898c5a3dd1f82ed416fd856d57b764a
|
|
| MD5 |
f61289322046cdfdae99ec0f22c17436
|
|
| BLAKE2b-256 |
9e2f3e06fdf8af121aff95cb30f366e24bca64caa769c3298c4754a4c74317d8
|
Provenance
The following attestation bundles were made for threadcheck-0.0.1.2a5-py3-none-manylinux_2_28_x86_64.whl:
Publisher:
release.yml on ChidcGithub/Threadcheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
threadcheck-0.0.1.2a5-py3-none-manylinux_2_28_x86_64.whl -
Subject digest:
daefd793c41a98fc33f1f2498f133ffce898c5a3dd1f82ed416fd856d57b764a - Sigstore transparency entry: 1761882616
- Sigstore integration time:
-
Permalink:
ChidcGithub/Threadcheck@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Branch / Tag:
refs/tags/v0.0.1.2a5 - Owner: https://github.com/ChidcGithub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Trigger Event:
push
-
Statement type:
File details
Details for the file threadcheck-0.0.1.2a5-py3-none-macosx_11_0_arm64.whl.
File metadata
- Download URL: threadcheck-0.0.1.2a5-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 33.4 kB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c408a64eab9c51017fbf09a37bc141887379498f71c7b087e9979607105e8799
|
|
| MD5 |
b1d64cbedc3a398dace5ac4259057ca0
|
|
| BLAKE2b-256 |
30a6ad4cc31b1fc7af5e3ae0e5081d69d350040bd707303ccba38ee49c46bfe9
|
Provenance
The following attestation bundles were made for threadcheck-0.0.1.2a5-py3-none-macosx_11_0_arm64.whl:
Publisher:
release.yml on ChidcGithub/Threadcheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
threadcheck-0.0.1.2a5-py3-none-macosx_11_0_arm64.whl -
Subject digest:
c408a64eab9c51017fbf09a37bc141887379498f71c7b087e9979607105e8799 - Sigstore transparency entry: 1761862531
- Sigstore integration time:
-
Permalink:
ChidcGithub/Threadcheck@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Branch / Tag:
refs/tags/v0.0.1.2a5 - Owner: https://github.com/ChidcGithub
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4fe4d2054793c4bac9fcdbcb7deb47737490961c -
Trigger Event:
push
-
Statement type: