Skip to content

Commit c517fc7

Browse files
authored
bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (#21895)
When I was fixing bpo-32751 back in GH-7216 I missed the case when *timeout* is zero or negative. This takes care of that. Props to @aaliddell for noticing the inconsistency.
1 parent 8e19c8b commit c517fc7

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎Lib/asyncio/tasks.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,13 @@ async def wait_for(fut, timeout, *, loop=None):
445445
iffut.done():
446446
returnfut.result()
447447

448-
fut.cancel()
449-
raiseexceptions.TimeoutError()
448+
await_cancel_and_wait(fut, loop=loop)
449+
try:
450+
fut.result()
451+
exceptexceptions.CancelledErrorasexc:
452+
raiseexceptions.TimeoutError() fromexc
453+
else:
454+
raiseexceptions.TimeoutError()
450455

451456
waiter=loop.create_future()
452457
timeout_handle=loop.call_later(timeout, _release_waiter, waiter)

‎Lib/test/test_asyncio/test_tasks.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,9 @@ async def inner():
11311131
nonlocaltask_done
11321132
try:
11331133
awaitasyncio.sleep(0.2)
1134+
exceptasyncio.CancelledError:
1135+
awaitasyncio.sleep(_EPSILON)
1136+
raise
11341137
finally:
11351138
task_done=True
11361139

@@ -1145,6 +1148,34 @@ async def inner():
11451148
chained=cm.exception.__context__
11461149
self.assertEqual(type(chained), asyncio.CancelledError)
11471150

1151+
deftest_wait_for_waits_for_task_cancellation_w_timeout_0(self):
1152+
loop=asyncio.new_event_loop()
1153+
self.addCleanup(loop.close)
1154+
1155+
task_done=False
1156+
1157+
asyncdeffoo():
1158+
asyncdefinner():
1159+
nonlocaltask_done
1160+
try:
1161+
awaitasyncio.sleep(10)
1162+
exceptasyncio.CancelledError:
1163+
awaitasyncio.sleep(_EPSILON)
1164+
raise
1165+
finally:
1166+
task_done=True
1167+
1168+
inner_task=self.new_task(loop, inner())
1169+
awaitasyncio.sleep(_EPSILON)
1170+
awaitasyncio.wait_for(inner_task, timeout=0)
1171+
1172+
withself.assertRaises(asyncio.TimeoutError) ascm:
1173+
loop.run_until_complete(foo())
1174+
1175+
self.assertTrue(task_done)
1176+
chained=cm.exception.__context__
1177+
self.assertEqual(type(chained), asyncio.CancelledError)
1178+
11481179
deftest_wait_for_reraises_exception_during_cancellation(self):
11491180
loop=asyncio.new_event_loop()
11501181
self.addCleanup(loop.close)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
When cancelling the task due to a timeout, :meth:`asyncio.wait_for` will now
2+
wait until the cancellation is complete also in the case when *timeout* is
3+
<= 0, like it does with positive timeouts.

0 commit comments

Comments
 (0)