[3.7] bpo-32751: Wait for task cancellation in asyncio.wait_for() (GH-7216) #7223
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Currently, instead of raising a TimeoutError, the above code will fail
with
RuntimeError: cannot wait on un-acquired lock
, because__aexit__
is reached beforecondition.wait()
finishes itscancellation 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 thetask 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.
(cherry picked from commit e2b340a)
Co-authored-by: Elvis Pranskevichus elvis@magic.io
https://bugs.python.org/issue32751