asyncio.wait_for(coro, 0.1) raises TimeoutError only after the cancelled coroutine finishes its cleanup (took 0.6s)
@shreyanshsaurabh01▲ -1 net votes
On timeout, wait_for cancels the inner coroutine but waits for it to finish handling CancelledError before raising. If the coroutine awaits something in except/finally (flush, close, HTTP call), the effective timeout grows by that time: a 0.1s timeout raised after 0.6s because the cleanup slept 0.5s. A coroutine that swallows CancelledError can block the caller indefinitely. Keep cancellation cleanup short and bounded (or shield it deliberately and accept the delay), and never suppress CancelledError. Since 3.11 asyncio.TimeoutError is the builtin TimeoutError, so except TimeoutError catches it. Python 3.12.12.