Record the real scheduling order of an asyncio app and replay its race conditions deterministically.
pip install deja-replay Copy PIP instructions
Replay the asyncio race condition you couldn't reproduce.
Déjà records the real scheduling order of a running asyncio app (which task ran when, what each database and HTTP call returned, what the clock and random number generator said) and replays it on your laptop with a deterministic event loop. The bug that only happened when two requests overlapped in production happens again, every time, with no database or network needed.
Status: working end to end for ASGI apps (FastAPI, Starlette, Litestar, Quart, Django), aiohttp servers and queue workers, on CPython 3.12 to 3.15 and PyPy 3.12, tested on Linux, Windows and macOS. Not production-hardened yet; see what's not covered. Documentation: morning0star.github.io/Deja (source in docs/) · Design and prior art: docs/DESIGN.md · Architecture and decisions: docs/ARCHITECTURE.md · Feasibility spikes: spikes/RESULTS.md · Benchmarks: benchmarks/RESULTS.md · Race gallery: gallery/README.md
pip install deja-replay Python 3.12 or newer. The import name is deja and the command is deja. Adapters switch on for the libraries you have installed; nothing else is needed.
From a clone of this repository, with the race gallery (small FastAPI apps with real races):
pip install -e ".[dev,gallery]" # record: run the race gallery on Déjà's recording loop; every trial that breaks its invariant is saved python -m gallery.stress wallet --trials 50 --record recordings/ # see it: the requests and the exact interleaving that caused the double charge deja show recordings/wallet-fail-trial0003.deja deja report recordings/wallet-fail-trial0003.deja --app gallery.wallet:create_app # HTML, with a replay verdict # replay it 100 times: same interleaving, same double charge, every time deja replay recordings/wallet-fail-trial0003.deja --app gallery.wallet:create_app --times 100 In your own app:
import deja app.add_middleware(deja.DejaMiddleware, directory="deja-recordings") # saves on 5xx, exception, or deja.flag() # uvicorn app:app --loop deja.loop:recording_loop (uvicorn 0.36 or newer) Other kinds of app:
# aiohttp web server from deja.aiohttp_web import deja_middleware app = web.Application(middlewares=[deja_middleware(directory="deja-recordings")]) # a queue consumer / scheduled job: each block is one "request" async with deja.scope("orders", message) as job: job.result = await handle_order(message) # replay: deja replay rec.deja --app myworker:handlers # several worker processes (uvicorn --workers N): one recording of a race across them app.add_middleware(deja.DejaMiddleware, group="/shared/deja-group") # then: deja merge /shared/deja-group/ / Then, from a recording:
Replay with the app configured the same way it was recorded (same store, same feature flags). If the code changed, replay lists the modules that differ; if it takes a different path, Déjà reports where the replay diverged instead of pretending it matched.
Record. A subclass of the standard asyncio event loop logs which task each loop pass ran (one integer per step, so recording is cheap), with stable task ids derived from where each task was created. A middleware marks each HTTP request, WebSocket connection, job and the app's background tasks as request scope, and adapters record what each call returned:
Clocks, random numbers and dates read inside a request are recorded too. Everything sits in a fixed-size ring buffer; a 5xx, an exception, close code 1011 or deja.flag() saves the window around the failing request to a small .deja file.
Explain. The report names the race (lost update, check-then-act, stale read, write skew, torn read, deadlock), the requests and the order of their reads and writes. It finds them in SQL (rows, ranges evaluated against the recorded data, subqueries, guarded inserts, transactions counted at commit, isolation levels), Redis, MongoDB documents, S3 objects, outside calls, and the app's in-memory state, which Déjà tracks on its own: containers (and the values inside them), objects and module variables the routes' code can reach.
Replay. A replay loop with a virtual clock re-sends the recorded requests (and WebSocket messages) to a fresh copy of the app and only lets request tasks run in the recorded order. Database, HTTP and blocking calls get their recorded answers; worker threads run again and wait at each access to shared state until the recording says it's their turn.
Check. If the code no longer behaves as recorded (a task can't take its recorded step, or a query differs), replay stops and says where. Replaying a recorded race against a fixed version shows the fix blocks it.
Acceptance run (python -m gallery.acceptance --recordings 5 --replays 100), CPython 3.12 and 3.14, November 2026:
The same holds with the apps on Postgres, including with the database container stopped before replaying.
Overhead, measured in November 2026 (FastAPI + Postgres + an HTTP call per order, k6 at 100 to 400 req/s, GitHub Actions Linux, 2 vCPU): throughput unchanged, no failed requests, about +12% server CPU, +1 to 3 ms at p99, about 0.1 KB of recording per request. Details and method: benchmarks/RESULTS.md.
asyncio code races at every await. Two requests read the same row, both pass the check, both write. In tests the timing never lines up, so the bug "can't be reproduced". Existing tools record a single request sequentially, simulate random schedules in tests, or profile where time goes. None of them capture the interleaving that actually happened and play it back. (prior art)
What Déjà can't decide, it says so ("possible conflict") rather than guess. The limits:
The full list, with what to do about each: Limits.
CPython 3.12, 3.13, 3.14 or 3.15, or PyPy 3.12. sqlglot is the only dependency; adapters load for the libraries you have. The import name is deja; the PyPI distribution will be deja-replay (deja is taken on PyPI).
Recordings hold what the app saw: request bodies, SQL parameters, rows, API responses. Treat them like database dumps and read SECURITY.md before sharing one.
Bug reports with a recording (private data removed) are the most useful; see CONTRIBUTING.md for setup and tests, and CHANGELOG.md for what changed.
Both files in this release have provenance metadata.
bench dev docs drivers frameworks gallery sql
asgi asyncio debugging deterministic replay fastapi race condition record replay
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
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.
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.
Both files in this release have been uploaded with provenance metadata.
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.




