Concurrency and races · Chapter 13 of 15
The lock that expired mid-work
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Time
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: bring Monday's prices back on purpose
Write reproduce() in repro.js. Make a pretend clock (a variable t and a function now that returns it), a lock service with createLocks(now) and a store with createStore(), both from lock-and-store.js. Then: call store.hold() to stall the next write; start syncPrices('monday', { locks, store }) and keep its promise; wait for the held write's sent promise; move t on by more than 30,000 ms; run syncPrices('tuesday', { locks, store }) to the end; call the held write's release(); wait for the first sync to settle (it may reject, so catch it); and resolve to store.get('menu-prices').
The check runs your reproduce() twice: beside the original price-sync.js it must resolve to 'monday' (the late write landed over Tuesday's prices), and beside a fixed price-sync.js it must resolve to 'tuesday'. No real waiting: the clock is yours to move, and reproduce() must finish within 2 seconds.
- Task 2
price-sync.js: a late write cannot land over a newer one
Fix syncPrices(prices, { locks, store, ttlMs }) in price-sync.js so the store can refuse a write from a lock that has already ended. Send the lock's token with the write, as store.put(key, prices, { fence: token }). A normal sync writes, logs price sync: wrote <prices> and resolves to 'synced'. With the lock held elsewhere it writes nothing, logs the skipping line and resolves to 'busy'. When the store refuses the write (it rejects with an Error whose code is 'STALE_FENCE'), syncPrices logs price sync: write refused, the lock had expired (<the error's message>) and resolves to 'refused'; any other error from the store still rejects. The lock is released every time.
Keep the lock's name 'menu-prices', the 30-second default and the log lines as they are. The check uses its own lock service and store and its own prices, and stalls the write itself, after anything the sync asks first, so asking the lock service whether the lock is still yours just before writing does not fix it.
- Task 3
price-sync.test.js: a test that would have caught it
Write a vitest test file, price-sync.test.js, that would have caught the late write. The check runs your file three times: beside the original price-sync.js, where at least one test must fail; beside the fixed price-sync.js, where every test must pass; and beside a wrong fix that asks the lock service "still mine?" just before writing, where at least one test must fail.
So play the ticket out with the stand-ins from lock-and-store.js, as in your repro: a held write, the lock expiring, a newer sync, the held write arriving. Expect the newer prices to stay in the store and the late sync to resolve to 'refused'. Also test that a normal sync resolves to 'synced' and writes. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.js: bring Monday's prices back on purpose
- price-sync.js: a late write cannot land over a newer one
- price-sync.test.js: a test that would have caught it
More in Time
Build The lock that expired mid-work yourself
Tapri's backend grew busy, and Kabir keeps handing you incidents that only happen when two things run at once. Each is a race interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.