-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-42130: Fix for explicit suppressing of cancellations in wait_for() #28149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9c2942a
7ff2c04
cdaa9c8
342222f
01d9884
af96f10
1e5233d
743177e
d7c0965
80f027b
4eb5de2
a8f5c1f
5de6b79
5a79cf2
11e1214
3a35126
df15ac1
7374366
c694173
cb59377
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1009,16 +1009,63 @@ def gen(): | |
self.assertEqual(res, "ok") | ||
|
||
def test_wait_for_cancellation_race_condition(self): | ||
def gen(): | ||
yield 0.1 | ||
yield 0.1 | ||
yield 0.1 | ||
yield 0.1 | ||
|
||
loop = self.new_test_loop(gen) | ||
|
||
fut = self.new_future(loop) | ||
# Test that if task is cancelled at the same time the future | ||
# completes, the result is still returned. | ||
loop.call_later(0.1, fut.set_result, "ok") | ||
task = loop.create_task(asyncio.wait_for(fut, timeout=1)) | ||
loop.call_later(0.1, task.cancel) | ||
res = loop.run_until_complete(task) | ||
self.assertEqual(res, "ok") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Putting this test back in, with a comment. It does manage to trigger the race condition aaliddel mentioned. |
||
|
||
def test_wait_for_suppress_cancellation(self): | ||
def gen(): | ||
yield | ||
yield 0.1 | ||
yield 0.1 | ||
yield 0.1 | ||
|
||
async def inner(): | ||
with contextlib.suppress(asyncio.CancelledError): | ||
try: | ||
await asyncio.sleep(1) | ||
return 1 | ||
except asyncio.CancelledError: | ||
return "ok" | ||
|
||
async def main(): | ||
result = await asyncio.wait_for(inner(), timeout=.01) | ||
assert result == 1 | ||
loop = self.new_test_loop(gen) | ||
|
||
fut = self.new_future(loop) | ||
task = loop.create_task(asyncio.wait_for(inner(), timeout=1)) | ||
loop.call_later(0.1, task.cancel) | ||
# Cancellation is suppressed in inner(), so should still return here. | ||
res = loop.run_until_complete(task) | ||
self.assertEqual(res, "ok") | ||
|
||
def test_wait_for_cancellation_inner_race_condition(self): | ||
def gen(): | ||
yield | ||
yield 0.1 | ||
yield 0 | ||
yield 0 | ||
|
||
async def inner(): | ||
# Test that if fut completes at same time as timeout, the result | ||
# is still returned. | ||
return await asyncio.wait_for(fut, timeout=1) | ||
|
||
asyncio.run(main()) | ||
loop = self.new_test_loop(gen) | ||
|
||
fut = self.new_future(loop) | ||
loop.call_later(0.1, fut.set_result, "ok") | ||
res = loop.run_until_complete(asyncio.wait_for(inner(), timeout=0.1)) | ||
self.assertEqual(res, "ok") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Converted my last test to use |
||
|
||
def test_wait_for_waits_for_task_cancellation(self): | ||
loop = asyncio.new_event_loop() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix issue where ``asyncio.wait_for()`` is cancelled when the coro | ||
actually suppresses the cancellation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ensures that a cancellation suppressed in the inner future, also suppresses it in
wait_for()
as one would expect (see test_wait_for_suppress_cancellation).