Skip to content

Latest commit

 

History

History
90 lines (65 loc) · 4.83 KB

debugging-report.md

File metadata and controls

90 lines (65 loc) · 4.83 KB

Bug Investigation Report: Duplicate Orders on Checkout

Generated with DevPilot
Date: 2026-11-11
Incident Reference: INC-8921
Target Repository: example-org/nexus-commerce


1. Restatement of the Symptom

When customers click the "Place Order" button on the web checkout page, intermittent duplicate order records are created in the database (typically 2 identical orders created within 100-300ms of each other), resulting in duplicate payment authorizations.


2. Relevant Execution Code Path

POST /api/v1/orders/checkout (src/routes/order.routes.ts:84)
  └─► OrderController.checkout (src/controllers/order-controller.ts:45)
        └─► OrderService.createOrder (src/domain/order/order-service.ts:112)
              ├─► PaymentGateway.authorizeCharge (src/infra/payments/stripe.ts:35)
              └─► OrderRepository.saveOrder (src/infra/db/order-repository.ts:50)
  1. Route handler receives HTTP request and invokes OrderController.checkout.
  2. Controller calls OrderService.createOrder with cartId and paymentMethodId.
  3. Payment is authorized with the payment processor via HTTP API call.
  4. Database order record is created in PostgreSQL.

3. Candidate Hypotheses

Hypothesis 1: Lack of Request Idempotency Key in Controller / Gateway (Rank 1 - Confirmed)

  • Description: Double-clicking the submission button on unstable mobile connections generates two concurrent HTTP POST requests with identical payload. Because no idempotency key is required or passed, both requests execute the full authorization and creation pipeline independently.
  • Evidence For:
    • src/controllers/order-controller.ts:48 extracts req.body, but does not inspect or validate an Idempotency-Key HTTP header.
    • src/infra/payments/stripe.ts:38 calls stripe.paymentIntents.create() without passing { idempotencyKey } in the request options.
    • Timestamps in production incident database show two orders created 140ms apart from the same client IP with the same cartId.
  • Evidence Against: None.
  • Status: Confirmed Root Cause.

Hypothesis 2: Client Cart Re-use After Payment (Rank 2 - Secondary Contributor)

  • Description: Cart status is not marked as LOCKED before initiating payment authorization, allowing a second concurrent request to read the cart before the first request finishes marking it as ORDERED.
  • Evidence For:
    • src/domain/cart/cart-service.ts:80 updates cart status to ORDERED after payment authorization completes rather than in an initial optimistic lock.
  • Evidence Against: Cart status alone would not explain duplicate payment intents if Stripe idempotency was present.
  • Status: Secondary contributing factor.

4. Recommended Diagnostic Verification

To confirm the race condition locally without production traffic:

  1. Run parallel curl requests with the same cart payload:
    curl -X POST http://localhost:3000/api/v1/orders/checkout -H "Content-Type: application/json" -d '{"cartId": "cart_123"}' & \
    curl -X POST http://localhost:3000/api/v1/orders/checkout -H "Content-Type: application/json" -d '{"cartId": "cart_123"}'
  2. Observe whether both respond with 201 Created and separate order IDs.

5. Proposed Fix Plan (Awaiting User Authorization)

DevPilot will not modify any files until you explicitly authorize this change.

Proposed Changes:

  1. Require Idempotency Key:
    • In src/routes/order.routes.ts: Add idempotency-key header to schema validation.
    • In src/controllers/order-controller.ts: Check Redis for active/completed idempotency keys before processing.
  2. Pass Key to Payment Gateway:
    • In src/infra/payments/stripe.ts: Pass the key to Stripe's idempotencyKey parameter.
  3. Optimistic Cart Locking:
    • In src/domain/cart/cart-service.ts: Transition cart to PROCESSING atomically using UPDATE cart SET status='PROCESSING' WHERE id=:id AND status='ACTIVE'.

6. Verification Plan

  1. Unit Test: Add test in tests/unit/order-service.test.ts verifying that passing an identical idempotency key returns the cached order response without re-invoking payment gateway.
  2. Concurrency Integration Test: Add test in tests/integration/checkout.test.ts firing 5 concurrent requests with the same key and asserting exactly 1 order is created and 4 return the idempotent cached receipt.

7. Scope & Limitations Statement

  • Inspected Files: src/routes/order.routes.ts, src/controllers/order-controller.ts, src/domain/order/order-service.ts, src/domain/cart/cart-service.ts, src/infra/payments/stripe.ts, and src/infra/db/order-repository.ts.
  • Runtime Limitations: Diagnostic conclusions are based on static code analysis and incident log telemetry. Live Stripe webhook re-delivery and downstream warehouse logistics consumers were not inspected.