#47 · Races

Find the bug · #47 · Races Hard JavaScript

One checkout, two orders

A function meant to place each order once sometimes places it twice.

On this page

The report

Bug report

The Pay button and the page’s automatic retry both call placeOrderOnce, which is supposed to place each checkout’s order only once. Every few hundred orders, a customer gets two orders and is charged twice.

The code

Read it first: what should it do? Then click the line where you think the bug is.

JavaScriptClick the line you think is wrong
// Places the order for a checkout session. The Pay button and the page's
// auto-retry both call this, so each session's order must be placed only once.
const placed = new Map();
async function placeOrderOnce(sessionId, createOrder) {
  if (placed.has(sessionId)) return placed.get(sessionId);
  const order = await createOrder(sessionId);
  placed.set(sessionId, order);
  return order;
}

Read it, then pick a line.

What it prints

Running it on a small example prints the output on the left. It should print the output on the right.

The example
let created = 0;
const createOrder = async (id) => {
  await new Promise((r) => setTimeout(r, 20));
  created++;
  return { id: 'order-' + created, session: id };
};
const [a, b] = await Promise.all([placeOrderOnce('s1', createOrder), placeOrderOnce('s1', createOrder)]);
console.log('orders created: ' + created);
console.log(a.id === b.id ? 'same order' : 'two different orders');

It prints

orders created: 2
two different orders

It should print

orders created: 1
same order
Show the answer: the bug, the fix, and how you’d find it

The bug (lines 5, 6, 7)

Line 7 records the order only after createOrder finishes. A second call that arrives while the first is still waiting finds nothing in placed (line 5) and creates the order again. Store the promise straight away, so every later call waits on the same one.

The fix

2// auto-retry both call this, so each session's order must be placed only once.3const placed = new Map();4async function placeOrderOnce(sessionId, createOrder) {5  if (placed.has(sessionId)) return placed.get(sessionId);6  const order = await createOrder(sessionId);7  placed.set(sessionId, order);8  return order;5  if (!placed.has(sessionId)) placed.set(sessionId, createOrder(sessionId));6  return placed.get(sessionId);9}

How you’d find it

  1. Reproduce with two calls at once for the same session: two orders. One after the other, it works, which is why tests passed.
  2. Trace it: call A checks the map (empty) and starts creating; call B checks the map, still empty because A hasn’t finished, and creates too.
  3. The window is the time createOrder takes. Close it by putting something in the map before awaiting: the in-flight promise.

The test that catches it

It fails on the code above and passes once it’s fixed. Keep it, so the bug can’t come back.

let created = 0;
const createOrder = async () => {
  await new Promise((r) => setTimeout(r, 10));
  created++;
  return { n: created };
};
await Promise.all([1, 2, 3].map(() => placeOrderOnce('x', createOrder)));
assert.equal(created, 1);

Where else this bites

  • A cache that lets ten requests for the same missing key all hit the database at once (a “cache stampede”).
  • Refreshing an auth token from several requests at once.
Your progress stays on this device.