In this run, asyncio.wait_for raised TimeoutError for pending with timeout=0. In this run, asyncio.wait_for with timeout=0 returned the value set by ready.set_result, as shown by the output “ready: 17”. Use a fresh pending Future to exercise the timeout path. Check its cancellation separately in the exception handler.
The problem
In this fixture, ready returned 17 and pending raised TimeoutError. The script calls ready.set_result before passing ready to asyncio.wait_for. It creates pending separately and passes pending to asyncio.wait_for without calling set_result on it.
Reproduce the result
The fixture creates ready with loop.create_future and calls ready.set_result with values[0] before awaiting asyncio.wait_for with timeout=0. The code asserts that value equals values[0].
The fixture passes a fresh, unresolved Future named pending directly to asyncio.wait_for with timeout=0. The TimeoutError handler contains an assertion on pending.cancelled(). The next statement prints the literal “pending: TimeoutError cancelled=True”.
The following example ran on Python 3.11.9 with synthetic data.
values = [17] import asyncio async def main(): loop = asyncio.get_running_loop() ready = loop.create_future() ready.set_result(values[0]) value = await asyncio.wait_for(ready, timeout=0) assert value == values[0] print("ready:", value) pending = loop.create_future() try: await asyncio.wait_for(pending, timeout=0) except TimeoutError: assert pending.cancelled() print("pending: TimeoutError cancelled=True") else: raise AssertionError("pending operation unexpectedly completed") asyncio.run(main()) ready: 17 pending: TimeoutError cancelled=True What the output says
The output “pending: TimeoutError cancelled=True” shows that execution reached the fixture’s TimeoutError handler. The fixture places the assertion on pending.cancelled() before that print. The printed cancelled=True marker is fixed text from the handler.
The documentation specifies that asyncio.wait_for cancels the awaitable when the timeout expires. It specifies TimeoutError as the exception raised on that path.
Limits
The fixture passes an already completed Future and a fresh unresolved Future directly to asyncio.wait_for with timeout=0. Its assertions check value against values[0] and check pending.cancelled().
The documentation says the total wait can exceed the timeout because asyncio.wait_for waits for cancellation to finish.
Apply the check
Create pending with loop.create_future and leave it unresolved before passing it to asyncio.wait_for with timeout=0. Check that the call raises TimeoutError. Check pending.cancelled() after catching the exception.
Create ready with loop.create_future and call ready.set_result with values[0] before passing it to asyncio.wait_for with timeout=0. Check that the return value equals values[0]. For a caller that passes an agent coroutine, test that coroutine separately with the caller’s positive timeout.
Primary reference: Python documentation
AI disclosure: This article was generated by an automated Codex editorial workflow.
