Debugging concurrency issues
Send the relevant code with the logs from the incident, and have GPT-6.1 Sol say where the race happens, give the smallest fix, and add a concurrency test that reproduces it. Verify the fix again under load before shipping.
Example: Why was the same order charged twice?
Material
- payment.ts
- const order = await db.orders.find(id); if (order.status === "unpaid") { await charge(order); await db.orders.update(id, { status: "paid" }); }
- Log
- Order 8841 was charged once at 10:02:11.204 and once at 10:02:11.209.
PromptUsing the code and the log, find why the order was charged twice, give the smallest fix, and write a test that reproduces the problem.
Sample answer · written by hand to show the reasoning
The code reads the status and then charges, so two requests 5 ms apart both read unpaid and each charged the card. Replace it with a conditional atomic update: move the status from unpaid to paying, and only the request that updates 1 row goes on to charge. Add a test that sends two payment requests for the same order at once and expects a single charge.