Skip to main content

racetrap

Finds race conditions in web APIs by forcing the worst order of their SQL statements.

racetrap catching a double booking, then passing once the fix is in

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:

  1. It runs them one at a time, in both orders. Those are the correct outcomes.
  2. It runs them again, holding the first request right before each of its SQL statements in turn while the second one runs.
  3. 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

The report for one race: both requests as lanes, the hold, the outcome and the fix

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 conditional UPDATE ... WHERE, a unique constraint, or SERIALIZABLE with 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 the command with uv tool install git+https://github.com/w-rahul/racetrap and write a scenario file.

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.

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

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

Source distribution (sdist)

Source distribution for racetrap 0.1.0
File Size Uploaded Provenance Actions
racetrap-0.1.0.tar.gz 11.0 MB Without provenance Details for racetrap-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for racetrap 0.1.0
File Interpreter ABI Platform Actions
racetrap-0.1.0-py3-none-any.whl Python 3 none any Details for racetrap-0.1.0-py3-none-any.whl

Total release size: 11.1 MB

Release files / racetrap-0.1.0.tar.gz

Download URL racetrap-0.1.0.tar.gz
Size 11.0 MB
Tags Source
SHA-256 checksum
How to use checksums
ed313897e77495edca7eca0905d0c2c9417d8ca8ef7fe072600cafedcbf0914f
BLAKE2b-256 checksum
How to use checksums
713e4cd78892d6e7e659ceb08a1e5e5bb18ee7c2eac74407c6ceb5cffc5be7f6
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.1.0-py3-none-any.whl

Download URL racetrap-0.1.0-py3-none-any.whl
Size 26.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ac692083e2e60c64d20b3eb249a9ac95a115f37f9ce3e0355b5673a0a41353f
BLAKE2b-256 checksum
How to use checksums
5399d66f3e20f87584de0eff221d0b194820c44af7ec92a1c437fd2bc4301395
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.1.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.

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