Python traps · Chapter 15 of 16
A coroutine never awaited, and a blocking call in async code
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.
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: the SMS that never went out
Write reproduce() in repro.py. Make a fake SMS gateway whose send(phone, text) is an async def that raises ConnectionError the first time it is called and records every send after that. Run one confirmation with asyncio.run(confirm_order(order, sms, log, retry_after=0)), with any order (a dict with "id", "phone" and "slot") and any log function, such as print, and return how many SMS your fake recorded, as an int.
The check runs your reproduce() twice, each time in a fresh python3: beside the original confirm.py it must return 0 (the retry never went out, and Python warns that a coroutine was never awaited), and beside a fixed confirm.py it must return 1. So the fake's send must be an async def that fails once: a plain def would run even without await. No real waiting and no input(): pass retry_after=0, and reproduce() must finish within 3 seconds.
- Task 2
confirm.py: the SMS goes out, and nobody waits for it
Fix confirm_order(order, sms, log, retry_after=2) in confirm.py. When the first send works: one SMS, then the log line order <id>: confirmation sent. When the first send raises ConnectionError: log order <id>: sms failed (<error>), retrying in <retry_after> s, wait retry_after seconds without stopping other orders, send the same SMS again for real, then log the confirmation line.
Keep both log lines, the SMS text and the signature as they are. The check imports a fresh copy of your confirm.py with its own fake gateway, replaces time.sleep with a fake that fails when it is called inside the event loop and asyncio.sleep with one that records the seconds and returns at once, and runs two failing orders together to see that both retry. A retry left in a task that nothing waits for never goes out.
- Task 3
test_confirm.py: a test that would have caught it
Write a pytest file, test_confirm.py, that would have caught this bug before it reached customers. The check runs your file three times: beside the original confirm.py, where at least one test must fail; beside the fixed confirm.py, where every test must pass; and beside a wrong fix that moves the retry into a background task with asyncio.create_task and never awaits it, where at least one test must fail.
So, with a fake gateway, assert what it recorded once asyncio.run has returned: after a first send that fails, the SMS was sent once; and a send that works first time is sent once. A warning does not fail a test, so a test that only runs the code proves nothing. Use plain def test functions that call asyncio.run themselves (pytest-asyncio is not installed), and pass retry_after=0: each test must finish within 3 seconds.
What you will learn
- repro.py: the SMS that never went out
- confirm.py: the SMS goes out, and nobody waits for it
- test_confirm.py: a test that would have caught it
More in Time
Build "A coroutine never awaited, and a blocking call in async code" 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.