Skip to content

Commit 57b6988

Browse files
authored
[3.8] bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (pythonGH-21895) (python#21967)
When I was fixing bpo-32751 back in pythonGH-7216 I missed the case when *timeout* is zero or negative. This takes care of that. Props to @aaliddell for noticing the inconsistency.. (cherry picked from commit c517fc7)
1 parent 6e1954c commit 57b6988

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

Lib/asyncio/tasks.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,13 @@ async def wait_for(fut, timeout, *, loop=None):
460460
if fut.done():
461461
return fut.result()
462462

463-
fut.cancel()
464-
raise exceptions.TimeoutError()
463+
await _cancel_and_wait(fut, loop=loop)
464+
try:
465+
fut.result()
466+
except exceptions.CancelledError as exc:
467+
raise exceptions.TimeoutError() from exc
468+
else:
469+
raise exceptions.TimeoutError()
465470

466471
waiter = loop.create_future()
467472
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)

Lib/test/test_asyncio/test_tasks.py

+31
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,9 @@ async def inner():
888888
nonlocal task_done
889889
try:
890890
await asyncio.sleep(0.2)
891+
except asyncio.CancelledError:
892+
await asyncio.sleep(0.1)
893+
raise
891894
finally:
892895
task_done = True
893896

@@ -900,6 +903,34 @@ async def inner():
900903

901904
loop.run_until_complete(foo())
902905

906+
def test_wait_for_waits_for_task_cancellation_w_timeout_0(self):
907+
loop = asyncio.new_event_loop()
908+
self.addCleanup(loop.close)
909+
910+
task_done = False
911+
912+
async def foo():
913+
async def inner():
914+
nonlocal task_done
915+
try:
916+
await asyncio.sleep(10)
917+
except asyncio.CancelledError:
918+
await asyncio.sleep(0.1)
919+
raise
920+
finally:
921+
task_done = True
922+
923+
inner_task = self.new_task(loop, inner())
924+
await asyncio.sleep(0.1)
925+
await asyncio.wait_for(inner_task, timeout=0)
926+
927+
with self.assertRaises(asyncio.TimeoutError) as cm:
928+
loop.run_until_complete(foo())
929+
930+
self.assertTrue(task_done)
931+
chained = cm.exception.__context__
932+
self.assertEqual(type(chained), asyncio.CancelledError)
933+
903934
def test_wait_for_self_cancellation(self):
904935
loop = asyncio.new_event_loop()
905936
self.addCleanup(loop.close)
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)