Race conditions in AI-built apps, and how to close them

Direct answer

Race conditions appear when two requests touch the same data at the same moment and the code assumed they wouldn’t. AI-generated code usually checks first and writes second, which works for one user and fails under real traffic. Close the gap in the database, where it can be enforced: unique constraints for “only one of these,” atomic updates or row locks for read-modify-write, a consistent lock order, keys scoped to the request or account, and tests that fire parallel requests on purpose.

Why AI-generated code races

A coding assistant writes the logic a person would describe: look up whether the slot is free, and if it is, book it. Read aloud, that is correct. Run twice at the same instant, both requests see a free slot and both book it. One person clicking through a demo never produces two requests in the same few milliseconds.

The same blind spot covers hidden shared state: a module-level variable, a storage key with no owner in it, a helper that steps outside the transaction.

In the production issues we reviewed across about 25 projects in October, concurrency and race conditions appeared in four. The best-known version, a single “latest result” slot that every user shared, is covered in our article on silent failures in AI-built apps. Three others are below.

Three races we fixed

  • A booking marketplace let two clients book the same creator for the same date. It checked for a conflict and then inserted, so simultaneous requests both passed the check. The fix moved the rule into the database as a partial unique index, and the booking function now catches the constraint violation and returns a clean 409. Its payment version of the same race is in our article on why AI-built apps charge customers twice.
  • An e-signature flow in an insurance platform hung at random. Creating a signing request opened a transaction and updated a document row, then called a helper that updated the same row outside the transaction. The helper waited for a lock the transaction held, and the transaction waited for the helper. Every write now uses the same transaction, entities are reloaded inside it, and storage and email calls stay outside it.
  • A fitness coaching app let one account delete another account’s drafts. It saved drafts on the device under one shared key, with the owner’s ID only inside the payload. When a different account opened the same screen, it saw “no draft for me,” and its cleanup logic deleted the owner’s draft. Keys now embed the account ID, and key changes migrate by writing the new key and confirming it before removing the old one.

Let the database enforce the rule

Application checks cannot close a race. A constraint can. PostgreSQL’s documentation on partial indexes shows a unique index with a WHERE clause, which enforces uniqueness only among rows that match it, for example one active booking per creator and date while cancelled ones pile up freely. When the second insert hits the index, PostgreSQL raises error code 23505, listed as unique_violation in its error codes appendix. Catch that code and turn it into a “this slot was just taken” message.

For read-modify-write, such as decrementing stock or credits, do the check inside the write: one UPDATE whose WHERE clause includes the condition. PostgreSQL’s page on transaction isolation explains that under Read Committed, its default level, a second updater waiting on a changed row re-evaluates the WHERE clause against the new version. When the logic is too complex for one statement, lock the row first. PostgreSQL’s row-level locking docs say SELECT … FOR UPDATE blocks other transactions from updating, deleting or locking those rows until the current transaction ends.

Order locks and keep transactions short

The same chapter, in its section on deadlocks, says the best defense against deadlocks is to acquire locks on multiple objects in a consistent order, and to retry transactions that are aborted because of one. It also warns that a transaction waiting for a lock will wait indefinitely unless a deadlock is detected, so holding transactions open for long periods is a bad idea. The e-signature hang was that second case: the database could not see the helper waiting in application code, so nothing was aborted and the request just stalled.

In practice: pass one transaction object through every call that belongs to it, sort IDs before locking several rows, and never put a network call to storage, email or an AI model inside a transaction.

Test with parallel requests

  1. Fire the same request twice at once. Send two or more identical bookings, payments or updates in parallel and assert exactly one succeeds.
  2. Use two accounts on one device or browser. Shared storage keys only fail when a second identity loads the same screen.
  3. Search for shared state. Look for module-level variables, caches and storage keys that do not include a user, account or request ID.
  4. Watch for lock waits. Log slow queries and deadlock errors in staging while the parallel tests run.

Retries and duplicate webhooks are the same problem from another angle; see our guide to idempotency for AI agents.

Related Zenveus services: AI Prototype Hardening and the 48-Hour Verdict

Scroll to Top

Optional tracking stays off until you enable it. You can change your choice at any time.

Essential — always active
Security, requested features, and remembering these choices.

Read our cookie policy