noctcore-async-safety/no-concurrent-shared-mutation
noctcore-async-safety/no-concurrent-shared-mutation
- Package
- async-safety v1.0.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- None
- Type info
- Not needed
A read-modify-write of an outer-scope binding inside a concurrent Promise.all(arr.map(async …)) callback can lose updates.
Promise.all(arr.map(async …)) starts every callback before any of them resolve, so their await points
interleave. A read-modify-write of a shared outer binding that awaits between the read and the write
(total += await amount(), total = total + await amount()) can have two iterations read the same value and
one clobber the other. The classic lost update: the final total is wrong, non-deterministically.
JavaScript runs each synchronous step to completion, so a write with no await inside it cannot interleave:
await save(item); done++; and const n = await size(item); total += n; are safe and are not reported.
What it flags
Section titled “What it flags”Inside an async callback passed to .map/.flatMap/.forEach whose result is wrapped in Promise.all /
Promise.allSettled:
- a compound assignment to an outer binding whose right-hand side awaits (
x += await f(),x *= await f()); - a self-referential assignment that reads the binding before an await in the same expression
(
x = x + await f(),x = await add(x, y)).
An update expression (x++, --x) never awaits, so it is not reported.
// lost updateslet total = 0;await Promise.all(items.map(async (item) => { total += await priceOf(item);}));// collect, then reduceconst prices = await Promise.all(items.map((item) => priceOf(item)));const total = prices.reduce((a, b) => a + b, 0);What it does not flag
Section titled “What it does not flag”Deliberately conservative to distinguish real races from safe patterns:
arr.push(…),map.set(…), and distinct-index writes (results[i] = …) are not flagged — they are order-tolerant, not lost updates;- a plain overwrite (
x = await f(), no self-reference) is not flagged — that is last-write-wins, not a read-modify-write; - a binding declared inside the callback is local per iteration, so it is never flagged;
- the callback must be
asyncand contain anawait— a synchronous callback has no interleaving point.
When not to use it
Section titled “When not to use it”If you intentionally accumulate into shared state and have externally serialized the callbacks (e.g. a mutex, or a concurrency limit of 1), this rule’s report is a false positive: disable it inline for that block.