diff options
author | Elvis Pranskevichus <elvis@magic.io> | 2018-05-29 21:31:01 (GMT) |
---|---|---|
committer | Yury Selivanov <yury@magic.io> | 2018-05-29 21:31:01 (GMT) |
commit | e2b340ab4196e1beb902327f503574b5d7369185 (patch) | |
tree | d47d0236c55372324d406d99ae67b0cc14399c86 /Doc/library/asyncio-task.rst | |
parent | 863b6749093a86810c4077112a857363410cc221 (diff) | |
download | cpython-e2b340ab4196e1beb902327f503574b5d7369185.zip cpython-e2b340ab4196e1beb902327f503574b5d7369185.tar.gz cpython-e2b340ab4196e1beb902327f503574b5d7369185.tar.bz2 |
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 'Doc/library/asyncio-task.rst')
-rw-r--r-- | Doc/library/asyncio-task.rst | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index dc450c3..3121b47 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -790,7 +790,9 @@ Task functions Returns result of the Future or coroutine. When a timeout occurs, it cancels the task and raises :exc:`asyncio.TimeoutError`. To avoid the task - cancellation, wrap it in :func:`shield`. + cancellation, wrap it in :func:`shield`. The function will wait until + the future is actually cancelled, so the total wait time may exceed + the *timeout*. If the wait is cancelled, the future *fut* is also cancelled. @@ -800,3 +802,8 @@ Task functions .. versionchanged:: 3.4.3 If the wait is cancelled, the future *fut* is now also cancelled. + + .. versionchanged:: 3.7 + When *fut* is cancelled due to a timeout, ``wait_for`` now waits + for *fut* to be cancelled. Previously, + it raised :exc:`~asyncio.TimeoutError` immediately. |