Awesome Reviewers

Reviewer · Concurrency

Guard check-then-act races

Many bugs in this batch came from checking a condition and then acting on it after an `await`, without accounting for state that can change in the gap: deadlines read before a request instead of after, abort signals checked only before a poll and not after it resolves, counts or statuses computed from a snapshot that goes stale before the write lands, and...

Instruction

Many bugs in this batch came from checking a condition and then acting on it after an await, without accounting for state that can change in the gap: deadlines read before a request instead of after, abort signals checked only before a poll and not after it resolves, counts or statuses computed from a snapshot that goes stale before the write lands, and queued work that gets resubmitted because it wasn’t removed from the pending set before its async write started.

When a condition and the action it gates are separated by an await, re-validate the condition immediately before acting, not only before starting the async call:

status = await getStatus();
signal?.throwIfAborted(); // re-check after the await, not just before it
failedPolls = 0;

For writes that race with concurrent updates to the same record, use optimistic concurrency (version/ETag) rather than blind overwrites or in-place updates: read the current version, write conditioned on it, and treat a version mismatch as a real conflict to retry or surface rather than silently clobbering or skipping. When a conflict occurs because another writer got there first, verify whether the other writer’s result is already correct (e.g. hash matches) before deciding to retry.

For in-memory queues that are drained asynchronously (bulk upserts, IO flush batches), move the batch out of the “pending” collection into an “in-flight” marker before starting the async write, so a concurrent flush can’t resubmit the same items, and any items that arrive during the write are preserved for the next flush.

When multiple fields/documents must appear consistent to a reader (e.g. a terminal status plus its last step record), order the writes so dependent data lands before the status that signals it’s available, not in parallel with it.

Apply this standard whenever code does read → await → write/branch: ask “can the read be stale by the time I act on it, and if so, what’s the safe fallback (reject, retry, re-check, or dedupe)?”