Skip to main content

ConcurrencyGuard

Find race conditions and deadlocks in Python.

PyPI CI MIT License

Quick start · What it catches · CI and reports · Interactive examples

ConcurrencyGuard terminal demo

ConcurrencyGuard (cguard) scans Python code for concurrency bugs that can be hard to spot in review. It reports the file, line, and rule ID, and suggests what to change. The static scan reads your source without running it.

Quick start

python -m pip install concurrency-guard
cguard scan .

Scan a single file by passing its path instead of .. To see what an available automatic fix would change, run cguard fix . --diff. Apply the changes with cguard fix . --write.

Example

Two requests can read the same wallet balance, each subtract an amount, and then overwrite each other’s update:

async def withdraw(session, wallet_id, amount):
    wallet = await session.get(Wallet, wallet_id)
    wallet.balance -= amount
    await session.commit()

cguard scan reports CG-001 at the database read. Depending on the database and transaction, a row lock or an atomic SQL update can protect the operation.

What it catches

Area Example findings
Database writes Missing row locks, incomplete locking across branches, and check-then-insert races
Deadlocks Functions in different files acquire the same locks in opposite orders
Async Python Shared state changes across an await, blocking calls in coroutines, and orphaned tasks
Redis and background jobs Non-atomic check-then-set operations, unsafe lock release, and retry or transaction hazards

The scanner includes checks for SQLAlchemy, SQLModel, Django ORM, Tortoise ORM, asyncio, Redis, and Celery.

New in 3.9.0: CG-028 finds branch paths that can mutate and commit without the lock held on another path. CG-029 traces cross-file lock-order cycles to their acquisition sites. CG-030 detects when a coroutine reads shared module or class state, awaits, and then writes to it without continuous lock protection.

cguard scan . --select CG-028,CG-029,CG-030

CI and reports

# Export findings for GitHub Code Scanning
cguard scan . --format sarif --output cguard.sarif

# Open an HTML audit report in your browser
cguard report . --format html --output cguard-report.html

The scan exits with status 1 when it reports findings. For a GitHub Actions setup, see the example Code Scanning workflow. To scan only files changed against a Git base branch, use cguard scan . --base-ref origin/main.

Version 3.9.1 improves self-scan accuracy. Deadlock checks resolve local functions and methods by their receiver and class. Redis checks distinguish Redis operations from generic file-cache calls and match the receiver and key.

When scanning this repository, examples/ and tests/ include intentional unsafe code used to demonstrate and verify findings. Run cguard scan src/ to audit the scanner implementation.

Version 3.9.2 recognizes simple repeated database status assignments in retryable tasks and keeps retry warnings when acks_late=True is the only safeguard. Arithmetic writes, inserts, state-dependent transitions, and unknown side-effect calls retain their diagnostics.

More commands

Command When to use it
cguard verify --help Run a concurrency stress test against supported targets
cguard simulate --help Explore a possible execution order behind a finding
cguard baseline --help Track existing findings so CI can focus on new ones
cguard watch --help Rescan while you edit
cguard lsp --help Set up editor diagnostics

Run cguard --help for the full command list.

Contributing

python -m pip install -e ".[dev]"
pytest
ruff check .
mypy --strict .

Browse the example projects, open an issue, or review the release history on PyPI.

Metadata

Release files for concurrency-guard 3.9.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for concurrency-guard 3.9.2
File Size Uploaded Provenance Actions
concurrency_guard-3.9.2.tar.gz 192.6 kB Without provenance Details for concurrency_guard-3.9.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for concurrency-guard 3.9.2
File Interpreter ABI Platform Actions
concurrency_guard-3.9.2-py3-none-any.whl Python 3 none any Details for concurrency_guard-3.9.2-py3-none-any.whl

Total release size: 350.0 kB

Release files / concurrency_guard-3.9.2.tar.gz

Download URL concurrency_guard-3.9.2.tar.gz
Size 192.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1eda0e54781a3b28cfaccb835e46541ed48b20bcc854e0b2943140e6cae81912
BLAKE2b-256 checksum
How to use checksums
71ff7a9180e500060bbd3689f419e05d316e51fec9e1dd71630e4543585acfb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.8

Release files / concurrency_guard-3.9.2-py3-none-any.whl

Download URL concurrency_guard-3.9.2-py3-none-any.whl
Size 157.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a3402aba3d7036e73ba804c99e299c37203985294fd7b14c5e0192c4271522c
BLAKE2b-256 checksum
How to use checksums
9d2dd1bffd93688a2ae8eee4f1080401553e28ddfd57aad87c4c38464afe2f45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.8

Security for concurrency-guard 3.9.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page