Concurrency Bug Hunter (With Interleaving Proofs)
Finds race conditions, deadlocks and atomicity violations — and proves each one with a step-by-step interleaving trace instead of vague "this might race" warnings.
Prompt variables
3 variables detected — fill them and Copy inserts your values (0/3 filled).
Analyze this code for concurrency bugs. The rule that keeps this analysis honest: you may only report a bug if you can write out the interleaving that triggers it. No interleaving, no finding.
Check for, in order:
1. **Check-then-act races** — any read of shared state followed by a decision based on it (exists-then-create, balance-then-deduct, size-then-index).
2. **Non-atomic read-modify-write** — increments, appends, put-if-absent patterns on shared structures without atomicity.
3. **Lock ordering** — if two locks are ever held together, is the acquisition order globally consistent? Trace every path that acquires both.
4. **State escaping synchronization** — references to guarded collections returned to callers, listeners invoked while holding locks, "this" escaping constructors.
5. **Async-specific traps** — awaits inside critical sections, shared state mutated between an await and its continuation, fire-and-forget tasks whose failures vanish, missing cancellation propagation.
6. **Memory visibility** — flags read across threads without volatile/atomic/happens-before (language-dependent; apply {{language}} semantics precisely).
For every finding, output:
- **Interleaving proof**: numbered steps in the form "T1: executes line 14 (reads count=5) → T2: executes lines 14–16 (reads count=5, writes 6) → T1: writes 6 — lost update."
- **Consequence**: what corrupted state or hang results, and how often it plausibly fires under {{concurrency_context}}.
- **Fix**: the minimal correct repair (atomic type, lock scope change, queue, immutability), and one sentence on why a coarser fix (just add a big lock) is or is not acceptable here.
If the code is actually safe, explain the mechanism that makes it safe (confinement, immutability, framework single-threading) — naming the guarantee is as valuable as finding a bug.
```{{language}}
{{code}}
```
Comments (0)