Python traps 2 · Chapter 14 of 16
Two locks taken in opposite orders
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Libraries
- 3 tasks
- Python
- In the all labs plan
The first 2 chapters of Python traps 2 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.py: two riders pay each other, and both freeze
Write reproduce() in repro.py. Make Wallet("Santosh Kamble", 500) and Wallet("Imran Shaikh", 500) from wallets.py, and a fake audit(line) that waits on one threading.Barrier(2, timeout=0.2) and ignores the BrokenBarrierError it raises when nobody else comes. Start two daemon threads at once, one running transfer(santosh, imran, 100, audit) and the other transfer(imran, santosh, 40, audit), join each with a timeout of 0.5 seconds, and return how many of the two threads finished, as an int.
The check runs your reproduce() twice, each time in a fresh python3: beside the original wallets.py it must return 0 (both transfers still waiting for each other), and beside a fixed wallets.py it must return 2. So the Barrier must make both transfers meet inside the audit, and give up after 0.2 s: no sleeps. No real waiting beyond the Barrier and the joins, and no input(): reproduce() must finish within 3 seconds, so the threads must be daemons.
- Task 2
wallets.py: transfers that never wait for each other
Fix transfer(src, dst, amount, audit=print) in wallets.py so that, however many transfers run at once on their own threads, in any directions, every one of them finishes: when src has the amount it moves from src to dst; when src is short it raises ValueError and nothing changes.
Keep Wallet(owner, balance) with its lock, keep a lock on each wallet you change, and keep the audit line "<payer> pays <payee> ₹<amount>", called once per transfer after the first wallet's lock is taken and before the second. The check imports a fresh copy of your wallets.py and gives transfer its own fake audit, which waits until every transfer running is inside the audit (a Barrier, at most 0.2 s), so the transfers meet there every run: two opposite transfers, a ring of three, and many transfers in every direction. Its threads are joined with a timeout: a transfer still waiting after a second fails the check.
- Task 3
test_wallets.py: a test that would have caught it
Write a pytest file, test_wallets.py, that would have caught this freeze before the settlement run. The check runs your file three times: beside the original wallets.py, where at least one test must fail; beside the fixed wallets.py, where every test must pass; and beside a wrong fix that locks the receiver first instead of the payer, where at least one test must fail.
So run two transfers in opposite directions on two daemon threads, with a fake audit that waits on a Barrier(2, timeout=0.2) as your repro does, join each thread with a timeout, and assert that both finished and the balances add up. Two threads without the Barrier almost never freeze, 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 riders pay each other, and both freeze
- wallets.py: transfers that never wait for each other
- test_wallets.py: a test that would have caught it
More in Libraries
Build "Two locks taken in opposite orders" yourself
Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. 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 2 are free. This one comes with the all labs plan.