summaryrefslogtreecommitdiff
path: root/Lib/test/test_asyncio/test_locks.py
diff options
context:
space:
mode:
authorElvis Pranskevichus <elvis@magic.io>2018-05-29 17:31:01 -0400
committerYury Selivanov <yury@magic.io>2018-05-29 17:31:01 -0400
commite2b340ab4196e1beb902327f503574b5d7369185 (patch)
treed47d0236c55372324d406d99ae67b0cc14399c86 /Lib/test/test_asyncio/test_locks.py
parent863b6749093a86810c4077112a857363410cc221 (diff)
downloadcpython-git-e2b340ab4196e1beb902327f503574b5d7369185.tar.gz
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.
Diffstat (limited to 'Lib/test/test_asyncio/test_locks.py')
-rw-r--r--Lib/test/test_asyncio/test_locks.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py
index 8642aa86b9..b8d155e1d0 100644
--- a/Lib/test/test_asyncio/test_locks.py
+++ b/Lib/test/test_asyncio/test_locks.py
@@ -807,6 +807,19 @@ class ConditionTests(test_utils.TestCase):
with self.assertRaises(ValueError):
asyncio.Condition(lock, loop=loop)
+ def test_timeout_in_block(self):
+ loop = asyncio.new_event_loop()
+ self.addCleanup(loop.close)
+
+ async def task_timeout():
+ condition = asyncio.Condition(loop=loop)
+ async with condition:
+ with self.assertRaises(asyncio.TimeoutError):
+ await asyncio.wait_for(condition.wait(), timeout=0.5,
+ loop=loop)
+
+ loop.run_until_complete(task_timeout())
+
class SemaphoreTests(test_utils.TestCase):