🤖 The Prompt That Reads Code the Way a Second Thread Would
A race condition rarely announces itself in the source code the way a syntax error or an obvious null check does – the code reads as correct on its own, line by line, and only breaks when a second thread, request, or task happens to interleave at exactly the wrong moment, which is precisely the kind of scenario a normal code review, run by one person reading sequentially, is poorly suited to simulate in their head. A prompt that deliberately asks an AI to consider every shared piece of state in a function and imagine a second execution interleaving at each line is a mechanical, exhaustive way to surface exactly the class of bug that usually only turns up after it has already caused an intermittent production failure.
🔎 The Problem
You are reviewing code specifically for race conditions - bugs that only occur when two or more threads, requests, or async tasks run this code concurrently. For the code I paste, go through it and: 1. List every piece of state that is read or written by this code and is NOT local to a single call - a static field, a shared dictionary, a cached value, a file, or anything else more than one concurrent execution could touch at once. 2. For each one, describe a specific interleaving of two concurrent calls that would produce an incorrect result - name which line each call is at when the problem happens, not just that a race is theoretically possible. 3. Flag any check-then-act pattern (checking a condition, then acting on it in a separate step) involving shared state, since this pattern is race-prone even when each individual line looks correct in isolation. 4. For each finding, suggest the minimal synchronization or restructuring that would close it - a lock, an atomic operation, or a redesign to avoid sharing the state in the first place. Code to review: [paste your code here]
✅ Why This Prompt Works
- Asking for a specific two-call interleaving, naming exact lines, forces a concrete scenario instead of a vague “this could theoretically race” comment that is easy to read past without actually picturing how the failure would happen.
- Explicitly calling out shared state as the first step means the review has to enumerate every candidate before judging any of them, which catches a shared cache or a static field that a normal line-by-line read might pass over without specifically asking whether it is shared.
- Flagging the check-then-act pattern by name targets the single most common shape a race condition actually takes in real code, which is far more useful than a generic instruction to “look for concurrency bugs” with no concrete pattern to anchor the search.
⚠️ Why This Is Easy to Miss
- Paste the full method or class, including anywhere it reads configuration, a cache, or a field initialized elsewhere, since a race condition often depends on exactly where a piece of state comes from, which a narrow code snippet can hide entirely.
- Treat the findings as scenarios worth deliberately testing under real concurrent load, not as a guarantee of a bug – some flagged patterns are genuinely safe once you know the broader context, such as a value that is actually only ever written once at startup.
A race condition is invisible to a reader who reads one line at a time – asking what a second thread would see at each of those lines is the whole difference.




