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.

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.1

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.1
File Size Uploaded Provenance Actions
concurrency_guard-3.9.1.tar.gz 191.1 kB Without provenance Details for concurrency_guard-3.9.1.tar.gz

Built distribution (wheel)

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

Total release size: 347.8 kB

Release files / concurrency_guard-3.9.1.tar.gz

Download URL concurrency_guard-3.9.1.tar.gz
Size 191.1 kB
Tags Source
SHA-256 checksum
How to use checksums
90dcc2566378fd03df8a9a988b540987efe43694ce47549538eff614ef700269
BLAKE2b-256 checksum
How to use checksums
bd1d19ceef6f8a145a55459eb254de533a3fe4363972697700ca15d2b4d4db86
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.1-py3-none-any.whl

Download URL concurrency_guard-3.9.1-py3-none-any.whl
Size 156.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5e30a077f185998c8ecefdeb8f1b30b2b0f685d0a9a2222711d904990b6f528
BLAKE2b-256 checksum
How to use checksums
51097f7d79a7e607aaad346360e86078e95434c3936bf214f7d84349eebde925
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.1

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