Concurrency and races · Chapter 12 of 15
A job two workers took
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: pay one refund twice on purpose
Write reproduce() in repro.js. Make a jobs table with createQueue from job-queue.js holding one refund job, say { id: 'job-1', kind: 'refund', orderId: 4410 }, and a handle(job) that counts the refunds it pays. Start two workers at the same time, workOnce('worker-a', { queue, handle, now }) and workOnce('worker-b', { queue, handle, now }) from worker.js, with a now() that always answers 0, wait for both, and resolve to how many refunds were paid.
The check runs your reproduce() twice: beside the original worker.js it must resolve to 2 (one refund, paid twice), and beside a fixed worker.js it must resolve to 1. Workers started one after another never race, so start both before waiting for either. No real waiting: the stand-in answers on the next turn of the event loop, and reproduce() must finish within 2 seconds.
- Task 2
worker.js: one worker per job, and no job held forever
Fix workOnce(name, { queue, handle, now, leaseMs }) in worker.js. Take a job in one step with queue.claim(name, now(), leaseMs), so that two workers polling at the same moment can never get the same job: five workers at once with two jobs handle each job exactly once, and the other three resolve to null. A worker that took a job and crashed (its handle never finishes) holds the job only until its lease ends: before now() reaches the time it was taken plus leaseMs, another worker gets null; from then on, another worker takes the job and handles it.
Otherwise it works as today: log <name>: took <id> (<kind>), handle the job, complete it, log <name>: finished <id>, and resolve to the job's id; with nothing to do, resolve to null. Keep the log lines, the default lease of 30 seconds and the export as they are. The check uses its own jobs table with the same methods as job-queue.js, its own now(), a lease of 5000 ms and the default.
- Task 3
worker.test.js: a test that would have caught it
Write a vitest test file, worker.test.js, that would have caught the double refunds. The check runs your file three times: beside the original worker.js, where at least one test must fail; beside the fixed worker.js, where every test must pass; and beside a wrong fix that takes the job in one step but holds it for ever, where at least one test must fail.
So test both sides with createQueue: two workers at once on one job handle it once; and when a worker takes the job and crashes (a handle that never settles), another worker gets null before the lease ends (30 seconds by default) and gets the job once it has ended, with a now() you move by hand. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.js: pay one refund twice on purpose
- worker.js: one worker per job, and no job held forever
- worker.test.js: a test that would have caught it
More in Time
Build "A job two workers took" 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.