[3.7] bpo-32751: Wait for task cancel in asyncio.wait_for() when timeout <= 0 (GH-21895) by elprans · Pull Request #21968 · python/cpython · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Lib/asyncio/tasks.py
31 changes: 31 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,9 @@ async def inner():
nonlocal task_done
try:
await asyncio.sleep(0.2, loop=loop)
except asyncio.CancelledError:
await asyncio.sleep(0.1, loop=loop)
raise
finally:
task_done = True

Expand All @@ -848,6 +851,34 @@ async def inner():

loop.run_until_complete(foo())

def test_wait_for_waits_for_task_cancellation_w_timeout_0(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

task_done = False

async def foo():
async def inner():
nonlocal task_done
try:
await asyncio.sleep(10)
except asyncio.CancelledError:
await asyncio.sleep(0.1)
raise
finally:
task_done = True

inner_task = self.new_task(loop, inner())
await asyncio.sleep(0.1)
await asyncio.wait_for(inner_task, timeout=0)

with self.assertRaises(asyncio.TimeoutError) as cm:
loop.run_until_complete(foo())

self.assertTrue(task_done)
chained = cm.exception.__context__
self.assertEqual(type(chained), asyncio.CancelledError)

def test_wait_for_self_cancellation(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
Expand Down