Find and fix a race condition or concurrency bug
Analyzes code for race conditions, deadlocks, and lost updates, explains the exact interleaving that fails, and proposes a safe fix.
The prompt
Help me find and fix a concurrency bug.
Symptoms: [e.g. "duplicate orders", "counter occasionally wrong", "requests hang under load"]
Code involved: [PASTE THE RELEVANT FUNCTIONS, INCLUDING DATABASE QUERIES AND LOCKING]
Concurrency model: [THREADS, ASYNC/EVENT LOOP, MULTIPLE PROCESSES OR INSTANCES, QUEUE WORKERS]
Shared resources: [DATABASE ROWS, CACHE KEYS, FILES, IN-MEMORY STATE, EXTERNAL APIs]
Database and isolation level: [e.g. "PostgreSQL, read committed" - or "default/unknown"]
Steps:
1. Identify every shared resource and every read-modify-write sequence on it.
2. For each suspected race, write the exact interleaving step by step (Request A reads..., Request B reads..., A writes..., B writes...) and what goes wrong.
3. Check for: check-then-act bugs, lost updates, non-atomic counters, deadlocks from inconsistent lock order, async code that assumes no interleaving across `await`, and in-memory locks that don't work across multiple instances.
4. Propose fixes, comparing options such as atomic database operations, unique constraints, row locks (`SELECT ... FOR UPDATE`), optimistic locking with version columns, distributed locks, or serializing through a queue. Explain the trade-offs (throughput, deadlock risk, complexity) and recommend one.
5. Show how to reproduce it in a test (parallel requests, barriers, or injected delays) so the fix can be verified.
Return: the ranked list of races with interleavings, the recommended fix as code, the test, and any data cleanup needed for records already corrupted.
Highlighted [brackets] are blanks to replace with your own details.
Fill in the blanks and use this promptMore Software Development prompts
What to fill in
- e.g. "PostgreSQL, read committed" - or "default/unknown"
How this prompt is built
This prompt follows the Few-Shot structure (Few-shot prompting). A few-shot prompt includes one or more worked examples of the input and the output you want, so the model can copy the pattern instead of guessing it.
Related prompts
- Break my first coding project into small buildable stepsA milestone plan where every step leaves you with a working program and tells you what to learn next.
- Get unstuck on a coding error with hints instead of the answerA plain-English explanation of your error and step-by-step hints, so you learn to debug instead of copying a fix.
- Write precise TypeScript types for my data and APIsSafer, readable TypeScript types that rule out impossible states, replace unsafe casts, and come with compile-time tests.
- Write a style guide that documents my app's existing design codeReverse-engineers colors, typography, spacing, and component patterns from your codebase into a STYLE_GUIDE.md developers and AI agents can follow.