Spot the bug in the shared counter update

from Processes and threads
Node 24 intermediate 6 min 1 issue to find

This function receives a shared Int32Array and can run in several workers. Find the synchronization bug.

JavaScript
function claimSlot(state) {
  const claimed = state[0];
  const capacity = state[1];
  if (claimed >= capacity) return false;
  state[0] = claimed + 1;
  return true;
}
Open in playground
Report an error