TryBugHunt

The Desynchronized Flash-Sale Inventory Lock

hardjavascript

Unversioned cart timeout and premature lock release trigger duplicate stock allocation

0:00
Step 1: Test Traffic & Expose Flaw
/**
 * Cart Manager
 * Handles item reservations, expiry cleanup, and checkout finalization.
 */

export class CartManager {
  constructor(inventoryLock, inventoryStore) {
    this.inventoryLock = inventoryLock;
    this.inventoryStore = inventoryStore;
    this.carts = new Map();
  }

  createCart(cartId, sku) {
    const lockResult = this.inventoryLock.acquire(sku, cartId);
    if (!lockResult.success) {
      return { success: false, reason: "OUT_OF_STOCK" };
    }
    this.inventoryStore.decrementStock(sku);
    const cart = {
      cartId,
      sku,
      status: "active",
      epoch: lockResult.epoch
    };
    this.carts.set(cartId, cart);
    return { success: true, cart };
  }

  expireCart(cartId) {
    const cart = this.carts.get(cartId);
    if (!cart) return;

    // Return stock to inventory pool
    this.inventoryStore.incrementStock(cart.sku);
    this.inventoryLock.release(cart.sku, cart.epoch);

    // AI Attempt: Cart expired
    // BUG: Does NOT update cart.status = "expired"!
    // A late checkout request holding this cart object can still execute!
  }

  checkout(cartId) {
    const cart = this.carts.get(cartId);
    if (!cart) {
      return { success: false, reason: "CART_NOT_FOUND" };
    }

    // AI Attempt: Finalize order
    // BUG: Missing check for cart.status === "active"!
    // An expired or reclaimed cart proceeds to finalize!
    cart.status = "completed";
    return { success: true, orderId: "order_" + cartId };
  }
}
Step 1: Test Traffic & Expose Flaw

Simulate an expired cart reservation attempting checkout after the lock has been released to another customer, causing inventory overselling.

Choose Traffic Scenario to TestClick to simulate
Architectural Guide & Interview Notes
↑ Jump to Interactive Studio

Deep Dive: The Desynchronized Flash-Sale Inventory Lock

An engineering analysis of why this AI-generated Pull Request passed isolated unit tests, how multi-tenant traffic exposes the architectural flaw, and how interviewers score your response.

The Root Cause Anti-Pattern

The Interviewer's Scoring Rubric

Candidates are assessed on whether they jump to trial-and-error editing or follow a hypothesis-driven approach. A "Strong Hire" candidate restates the data flow between modules, explains why single-client unit tests passed, crafts an adversarial probe showing cross-tenant leakage, and implements a surgical 1-line patch without breaking existing contracts.

Candidate Strategy for the Code Comprehension Round

  1. 1Map Module ContractsTrace how the gateway invokes the underlying service. Note what data is passed by reference vs. by value.
  2. 2Break It With An Adversarial ProbeSimulate concurrent or sequential traffic across multiple identities to force shared state mutation.
  3. 3Deliver a Surgical PatchMake the minimal required diff to ensure proper per-client state isolation without refactoring unnecessary files.