[BUG] Duplicate dispute webhook from connector returns 500 (DuplicateValue on dispute insert)
Bug Description
When a connector delivers the same dispute webhook twice at nearly the same instant, one of the two requests fails with a 500.
Both requests independently check whether the dispute already exists, both see "not present", and both attempt an insert. One insert wins; the other violates the unique constraint on the dispute record and surfaces as DuplicateValue → 500 Internal Server Error back to the connector.
There is no data corruption — the dispute is persisted correctly, exactly once. The problem is purely that the losing request returns a 5xx instead of a successful/idempotent response, which shows up in our 5xx metrics and can trigger connector-side webhook retries.
Root Cause
The dispute webhook flow has:
- No distributed lock — unlike the payments flow, which locks on
payment_idto serialize concurrent deliveries, the dispute flow does not lock onconnector_dispute_id. - No duplicate-insert handling — a
DuplicateValuefrom the DB is propagated as an internal error instead of being treated as "already processed".
This makes the check-then-insert sequence a classic race: the gap between the existence check and the insert is unguarded.
Expected Behavior
Concurrent duplicate deliveries of the same dispute webhook should be handled idempotently — both requests should return 200, with the dispute created once and subsequent deliveries treated as a no-op/update.
Actual Behavior
- Request A: creates the dispute →
200 - Request B (same instant, same
connector_dispute_id): existence check passes → insert → unique constraint violation →DuplicateValue→500
Impact
- 5xx on the webhook endpoint (alerting noise)
- Possible connector-side webhook retries for a dispute that was already recorded successfully
- No functional/data impact on the dispute record itself
References
Source: juspay/hyperswitch