Python traps · Chapter 16 of 16
Threads and a race on a shared counter
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
- Python
- In the all labs plan
The first 2 chapters of Python traps are free. This one comes with the all labs plan.
Why you need this
CPython's GIL lets only one thread run Python bytecode at a time, but it switches threads between bytecodes: every few milliseconds, and whenever a thread waits, for example on a network call such as the booking log. Even one line is several steps:
import dis
dis.dis("coupon.uses -= 1")
0 RESUME 0
1 LOAD_NAME 0 (coupon)
COPY 1
LOAD_ATTR 2 (uses)
LOAD_CONST 0 (1)
BINARY_OP 23 (-=)
SWAP 2
STORE_ATTR 1 (uses)
RETURN_CONST 1 (None)
A read, a subtraction and a write, with room for a switch between each. A whole method is many more. The fix is a threading.Lock held across both the check and the update (with self._lock: around them), so a second thread waits at the lock and then sees the seat is gone. A lock around the update alone is too late: both threads have already passed the check.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.py: two tablets, one seat, two bookings
Write reproduce() in repro.py. Make Slot(1, log) from slots.py with a fake log that holds the first booking inside the log call until a second booking reaches the log too: on its first call it sets one threading.Event (it has arrived) and waits for a second Event, at most 0.2 seconds; on its second call it sets that second Event and returns. Start one thread that books a name, wait until it is inside the log, start a second thread that books another name, join both with a timeout, and return [how many bookings returned True, slot.left].
The check runs your reproduce() twice, each time in a fresh python3: beside the original slots.py it must return [2, -1] (both bookings got the last seat), and beside a fixed slots.py it must return [1, 0]. So it must really race two threads on one seat, with the Events deciding the order: no sleeps. No real waiting beyond that 0.2 s and no input(): reproduce() must finish within 3 seconds.
- Task 2
slots.py: the last seat is booked once
Fix Slot.book(name) in slots.py. However the threads interleave: when a seat is left, it calls log once with "<name>: booking seat <left>", adds the name to booked, takes the seat and returns True; when no seat is left, it returns False and changes nothing. A slot never books more names than it has seats, and left never goes below 0.
Keep Slot(seats, log=print), left, booked and the log line, called once per booking. The check imports a fresh copy of your slots.py and uses its own fake log, which holds the first booking inside the log call until a second booking has also got past the check (or 0.2 seconds), so the race happens every run; it also books many seats in a row and from many threads, and joins every thread with a timeout, so a lock that is never released fails. A lock around the update alone does not help: both threads have already passed the check.
- Task 3
test_slots.py: a test that would have caught it
Write a pytest file, test_slots.py, that would have caught this race before it reached the kitchen. The check runs your file three times: beside the original slots.py, where at least one test must fail; beside the fixed slots.py, where every test must pass; and beside a wrong fix that locks only the update, where at least one test must fail.
So force the race the way your repro does, with a fake log that holds the first booking until the second reaches the log too (at most 0.2 s), and assert in the main thread that exactly one booking returned True and that no seat is overbooked. Join every thread with a timeout. Two threads started without that fake almost never collide, so such a test passes the bug. No other real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: two tablets, one seat, two bookings
- slots.py: the last seat is booked once
- test_slots.py: a test that would have caught it
More in Time
Build "Threads and a race on a shared counter" yourself
DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap 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 Python traps are free. This one comes with the all labs plan.