fix(idempotency): in-flight reservation closes concurrent double-create race (bug-bash #21)#232
Merged
mastermanas805 merged 5 commits intoJun 3, 2026
Conversation
…te race (bug-bash #21) The middleware did GET(miss) → run handler → SET with no atomic reservation between the GET and the SET. Two requests carrying the same Idempotency-Key (or the same body-fingerprint) racing in that window both saw redis.Nil and both ran the handler — double-creating real backend resources on the authenticated /db|/cache|/nosql provision paths, which have no other per-burst dedup gate (the per-fingerprint INCR cap lives only on the anon branch; CreateDeployment already has its own FOR UPDATE cap). Fix: on a cache miss, write an in-flight reservation marker via SETNX (idemEntry.InFlight, 60s TTL) the instant the handler starts. A concurrent same-key request reads the marker in the top GET hit-block and returns 409 idempotency_key_in_progress (retryable — same envelope as the existing conflict 409) instead of re-running the handler. The real response overwrites the marker on completion (plain SET); non-cacheable paths (5xx, handler error, mutable 4xx) DELETE the marker so an immediate legitimate retry isn't blocked. Applied symmetrically to both the explicit-key and fingerprint branches. SETNX is best-effort/fail-open — a Redis hiccup never blocks provisioning (documented dedup posture, never a correctness gate). Tests: TestIdempotency_{ExplicitKey,Fingerprint}_ConcurrentInFlight_Returns409 hold request A in the handler (blocked on a channel) so B observes A's live reservation → 409, handler runs exactly once. Pass under -race; new code 100% covered. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
c5e7663 to
5c73b49
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The idempotency middleware did
GET(miss)→ run handler →SET, with no atomic reservation between the GET and the SET. Two requests carrying the sameIdempotency-Key(or the same body-fingerprint) racing in that window both observedredis.Niland both ran the handler — double-creating real backend resources on the authenticated/db,/cache,/nosqlprovision paths, which have no other per-burst dedup gate.(The deploy path is already race-safe via
CreateDeploymentWithCap'sSELECT … FOR UPDATE; this race is the db/cache/mongo resource-waste case.)Fix
On a cache miss, write an in-flight reservation marker via
SETNX(idemEntry.InFlight, 60s TTL) the instant the handler starts. A concurrent same-key request reads the marker in the top GET hit-block and returns 409idempotency_key_in_progress(retryable — same envelope as the existing conflict 409) instead of re-running the handler.SET).SETNXis best-effort / fail-open — a Redis hiccup never blocks provisioning (documented dedup posture: best-effort, never a correctness gate).Coverage
Both tests hold request A inside the handler (blocked on a channel) so B is guaranteed to observe A's live reservation → 409, handler runs exactly once.
🤖 Generated with Claude Code