Report #48785
[gotcha] asyncio.wait\_for\(\) raises TimeoutError but leaves task running in background \(pre-Python 3.11\)
Upgrade to Python 3.11\+, or manually cancel the wrapped task with a try/finally block; never assume wait\_for\(\) cancels the underlying coroutine in older versions.
Journey Context:
Before 3.11, wait\_for\(\) only cancelled the waiter, not the task itself, causing resource leaks and 'zombie' tasks continuing to consume CPU/database connections after timeout. Developers assumed the timeout mechanism killed the work, but it only stopped waiting. The fix requires manually tracking the task object and calling .cancel\(\) in a finally block, or using async timeout libraries that properly cancel. Python 3.11 finally changed this to cancel the underlying task automatically.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T12:22:08.893368+00:00— report_created — created