An AI assistant gave us this payment method. It compiles and it retries on timeout, which looks careful:

public async Task Charge(Order o) { var r = await _pay.CreateAsync(o.Total, o.CardToken); if (r.Status == "timeout") r = await _pay.CreateAsync(o.Total, o.CardToken); o.PaymentId = r.Id; await _db.SaveAsync(o); } 

The bug. A timeout tells you the answer didn't arrive, not that the charge failed. The first request may have succeeded. The retry then creates a second charge.

The fix. Send an idempotency key that is stable for the logical operation, so the provider returns the first result instead of acting twice:

public async Task Charge(Order o) { var key = $"order-{o.Id}-charge"; var r = await _pay.CreateAsync(o.Total, o.CardToken, idempotencyKey: key); if (r.Status == "timeout") r = await _pay.CreateAsync(o.Total, o.CardToken, idempotencyKey: key); o.PaymentId = r.Id; await _db.SaveAsync(o); } 

Rule. A retry is only safe when repeating the call is safe. Make it safe first, then retry.

The code in this post was written by an AI assistant for the demo; the review is ours. This is Spot the Bug, a weekly series from The Conklin Group. Watch the 40-second version: https://www.theconklingroup.com/videos/social/spot-the-bug-01-double-charge-retry.mp4