Eye Training · Shared State and Races
Every await is a door. While your code waits, someone else walks through it and changes what you read.
Part 2 · Surface 18. Bug counts are hidden and some snippets are clean. See Start Here for how to use these drills.
J · The basics
Bug hunt
J · Cart endpoint
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Like a post
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Place order button
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
J · Register
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
M · Three years in, these should jump out
Bug hunt
M · Search as you type
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Reserve stock
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Redeem a coupon
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · Nightly report
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
M · API client
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
M · Show details
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
S · Where seniors stand out
Bug hunt
S · Refresh the token
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Refresh, done right
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 1 min
Bug hunt
S · Payment webhook
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
Bug hunt
S · Withdraw
Bug count hidden, and some snippets are clean. Click every line you think is wrong (or none), then check.
0:00 / 2 min
What your eye checks
Whenever code reads something, then awaits, then acts on what it read, ask:
- Who else could change it during the
await? Another request, another tab, another instance, a retry. - Is the check in the same atomic step as the write? A conditional
UPDATE … WHERE, a unique constraint,SELECT … FOR UPDATE. A separate read is a gap. - Is the arithmetic done by the database?
likes = likes + 1, notpost.likes + 1written back. - Where does this state live? A module variable is shared by every request; an in-process
Setor lock exists once per instance. - Is this a copy or a shared object?
{ ...obj }copies one level. - Can it run twice? Double clicks, retries, webhook redelivery. Make it idempotent with keys and unique inserts.
- Which response wins? Out-of-order responses overwrite newer state unless you abort or check that the result is still wanted.
- Do concurrent callers each start the same work? Share one in-flight promise.
Tells
| You see | Ask |
|---|---|
let / const state at module level in a server | Shared by whom? |
find … await … update with a computed value | Lost update? |
if (exists) throw before an insert | Is there a unique constraint? |
A check, then a separate UPDATE | Can two requests pass the check? |
An async submit handler | What about a double click? |
await api… then render | Is the result still wanted? |
A Set or flag used as a lock | How many processes? |
{ ...defaults } then options.nested.x = | Shared nested object? |
if (expired) token = await refresh() | How many callers refresh at once? |
| A webhook handler with side effects | What if it's delivered twice? |
BEGIN, SELECT, then UPDATE … SET x = $1 | Is the row locked? |
Go deeper
- Race Conditions: interleavings, lost updates and stale responses, with a simulation
- L4 · Concurrency and Time: the same races in bigger codebases
- Previous surface: Caching and memoization · Next: Values, equality and numbers