mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
bpo-32751: Wait for task cancellation in asyncio.wait_for() (GH-7216)
Currently, asyncio.wait_for(fut), upon reaching the timeout deadline, cancels the future and returns immediately. This is problematic for when *fut* is a Task, because it will be left running for an arbitrary amount of time. This behavior is iself surprising and may lead to related bugs such as the one described in bpo-33638: condition = asyncio.Condition() async with condition: await asyncio.wait_for(condition.wait(), timeout=0.5) Currently, instead of raising a TimeoutError, the above code will fail with `RuntimeError: cannot wait on un-acquired lock`, because `__aexit__` is reached _before_ `condition.wait()` finishes its cancellation and re-acquires the condition lock. To resolve this, make `wait_for` await for the task cancellation. The tradeoff here is that the `timeout` promise may be broken if the task decides to handle its cancellation in a slow way. This represents a behavior change and should probably not be back-patched to 3.6 and earlier.
This commit is contained in:
parent
863b674909
commit
e2b340ab41
5 changed files with 100 additions and 3 deletions
|
@ -789,6 +789,62 @@ class BaseTaskTests:
|
|||
res = loop.run_until_complete(task)
|
||||
self.assertEqual(res, "ok")
|
||||
|
||||
def test_wait_for_waits_for_task_cancellation(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(0.2, loop=loop)
|
||||
finally:
|
||||
task_done = True
|
||||
|
||||
inner_task = self.new_task(loop, inner())
|
||||
|
||||
with self.assertRaises(asyncio.TimeoutError):
|
||||
await asyncio.wait_for(inner_task, timeout=0.1, loop=loop)
|
||||
|
||||
self.assertTrue(task_done)
|
||||
|
||||
loop.run_until_complete(foo())
|
||||
|
||||
def test_wait_for_self_cancellation(self):
|
||||
loop = asyncio.new_event_loop()
|
||||
self.addCleanup(loop.close)
|
||||
|
||||
async def foo():
|
||||
async def inner():
|
||||
try:
|
||||
await asyncio.sleep(0.3, loop=loop)
|
||||
except asyncio.CancelledError:
|
||||
try:
|
||||
await asyncio.sleep(0.3, loop=loop)
|
||||
except asyncio.CancelledError:
|
||||
await asyncio.sleep(0.3, loop=loop)
|
||||
|
||||
return 42
|
||||
|
||||
inner_task = self.new_task(loop, inner())
|
||||
|
||||
wait = asyncio.wait_for(inner_task, timeout=0.1, loop=loop)
|
||||
|
||||
# Test that wait_for itself is properly cancellable
|
||||
# even when the initial task holds up the initial cancellation.
|
||||
task = self.new_task(loop, wait)
|
||||
await asyncio.sleep(0.2, loop=loop)
|
||||
task.cancel()
|
||||
|
||||
with self.assertRaises(asyncio.CancelledError):
|
||||
await task
|
||||
|
||||
self.assertEqual(await inner_task, 42)
|
||||
|
||||
loop.run_until_complete(foo())
|
||||
|
||||
def test_wait(self):
|
||||
|
||||
def gen():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue