racetrap
Finds race conditions in web APIs by forcing the worst order of their SQL statements.
Why it exists
async with conn.transaction():
seats = await conn.fetchval("SELECT seats_left FROM shows WHERE id = $1", show_id)
if seats < 1:
raise HTTPException(409, "sold out")
await conn.execute("UPDATE shows SET seats_left = $1 WHERE id = $2", seats - 1, show_id)
await conn.execute("INSERT INTO bookings (show_id, user_name) VALUES ($1, $2)", show_id, user)
This passes every test and runs inside a transaction. Still, when two people book the last seat at the same moment, both get it. At Postgres's default isolation level both requests read seats_left = 1 before either one writes.
Tests send one request at a time, so they never see it. In production it shows up rarely, under load, and nobody can reproduce it. racetrap makes it show up on demand, in a test, with the exact order of statements that caused it.
What it does
racetrap sits between your app and Postgres as a proxy. For every pair of requests:
- It runs them one at a time, in both orders. Those are the correct outcomes.
- It runs them again, holding the first request right before each of its SQL statements in turn while the second one runs.
- Any result that matches neither one at a time run is a race, reported with the order that caused it.
Each forced order gives the same result on every run. There is no load test and no luck involved.
$ racetrap examples/tickets/buggy.toml
racetrap 0.1.0
proxy 127.0.0.1:6499 -> localhost:5432
app http://127.0.0.1:8765
alice books vs alice books 5 orders RACE
alice books vs bob books 5 orders RACE
bob books vs alice books 5 orders RACE
bob books vs bob books 5 orders RACE
4 of 4 pairs race. 34 runs in 4.7s.
Report: racetrap-report.html
What it does not do
- It does not replace transactions, locks or tests. You keep writing normal code. racetrap shows where the transaction you have is not enough, and the fix stays a line you write:
FOR UPDATE, a conditionalUPDATE ... WHERE, a unique constraint, orSERIALIZABLEwith a retry. - It never changes your code, and it is not for production. Point it at a test database you can wipe. It only watches and delays SQL.
- It is not a load test. It does not fire thousands of requests and hope. It forces each order once.
- It does not see everything. Postgres only, two requests at a time, and locks held outside Postgres, like Redis or an in-process mutex, are invisible to it.
Benchmark
benchmarks/burst.py does what people try without racetrap: it fires alice and bob at the same instant, 1,000 times, on the ticket example. On one Windows machine it caught the double booking in 68.5%, 81.6% and 79.8% of bursts across three runs, and the rate changed every time. racetrap caught it on every run, in all 4 pairs, named the exact order of statements, and checked the fix against every order it forced.
Try it
You need Python 3.11 or newer, uv and a Postgres you can wipe.
git clone https://github.com/w-rahul/racetrap
cd racetrap
uv sync
export DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres
uv run racetrap examples/tickets/buggy.toml
uv run racetrap examples/tickets/fixed.toml
The first run finds the race. The second one has FOR UPDATE on the read and passes.
To run it on your own app, install it and start a scenario from your OpenAPI document:
pip install racetrap
racetrap init --openapi http://127.0.0.1:8000/openapi.json
init writes racetrap.toml with one request for every POST, PUT, PATCH and DELETE, filled with example bodies from your schemas. Add the setup SQL, keep the requests that touch shared data, and run racetrap racetrap.toml.
Scenario file
app = "http://127.0.0.1:8000"
database = "${DATABASE_URL}"
command = "uvicorn shop:app --port 8000"
setup = [
"TRUNCATE orders, stock RESTART IDENTITY",
"INSERT INTO stock VALUES (1, 1)",
]
[[request]]
name = "alice buys"
path = "/items/1/buy"
json = { user = "alice" }
[[request]]
name = "bob buys"
path = "/items/1/buy"
json = { user = "bob" }
| Key | Meaning |
|---|---|
app |
Base URL of the app. |
database |
The real Postgres, used directly for setup and snapshots. |
setup |
SQL run before every order. It must reset the data. |
[[request]] |
name, path, and optional method, json and headers. |
command |
Optional. Starts the app with DATABASE_URL pointing at the proxy. |
init |
Optional SQL run once before the app starts. |
invariants |
Optional SQL that must return true after every order. |
ignore |
Optional table.column entries to leave out of the comparison. |
refs |
Optional id references without a foreign key, like { "orders.user_id" = "users.id" }. |
proxy, database_env, schema, stall_ms |
Defaults 127.0.0.1:6499, DATABASE_URL, public and 1000. |
${NAME} is read from the environment. Each request is also paired with itself, which catches double submits.
Run it in CI
jobs:
races:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:18
env:
POSTGRES_PASSWORD: postgres
ports: ["5432:5432"]
options: --health-cmd pg_isready --health-interval 2s --health-retries 30
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres
steps:
- uses: actions/checkout@v7
- run: pip install -r requirements.txt
- uses: w-rahul/racetrap@v0.2.0
with:
scenario: racetrap.toml
The job fails when any pair races, and the HTML report is uploaded as an artifact. Install your app's own dependencies before the racetrap step, so command can start it.
How it decides
The check is serializability. Every outcome must match running the two requests one after the other, in some order. racetrap compares the HTTP status of each request and the rows of every table.
Values the database fills in differ between correct runs, so ids, timestamps and random values are left out, and a reference to a generated id is compared by the row it points to. A deadlock or an unretried serialization failure that turns into a 500 counts as a race. racetrap stops instead of passing if the app dies, and the exit code is 1 when any pair races, so it can fail a CI job.
Prior work
Finding these bugs from database traces comes from ACIDRain (SIGMOD 2017), which found 22 attacks in 12 popular e-commerce apps. RaceDB (IEEE S&P 2025) confirms candidates by replaying them. ChaosSQL and Deadheat also proxy the database, but check a rule you write and randomize the timing. racetrap needs no rule and gives the same answer on every run.
License
MIT
Metadata
Release files for racetrap 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| racetrap-0.2.0.tar.gz | 11.1 MB | Without provenance | Details for racetrap-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| racetrap-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for racetrap-0.2.0-py3-none-any.whl |
Total release size: 11.1 MB
Release files / racetrap-0.2.0.tar.gz
| Download URL | racetrap-0.2.0.tar.gz |
|---|---|
| Size | 11.1 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ebe7f7adea970dee2bbb43df120dc0156dd4c8116d0180d3bc4a5057721194c8
|
|
BLAKE2b-256 checksum How to use checksums |
d0509491d7f4bfb5552d57d69eaffda8c33d4cfa5c2d34fe1f9f82b18e6f82ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / racetrap-0.2.0-py3-none-any.whl
| Download URL | racetrap-0.2.0-py3-none-any.whl |
|---|---|
| Size | 30.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4fef4f43a9e323877b5aee6c13b08019f16b5270dec16a3353c31f728a36cd8e
|
|
BLAKE2b-256 checksum How to use checksums |
bc50a0e9113d959104f10a56c93511358e56fb175770d179727cbdf23dcf2807
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for racetrap 0.2.0
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.