Skip to content

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ async def wait_for(fut, timeout):
# after wait_for() returns.
# See https://bugs.python.org/issue32751
await _cancel_and_wait(fut, loop=loop)
raise
return fut.result()
Copy link
Contributor Author

@Dreamsorcerer Dreamsorcerer Dec 15, 2021

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).


if fut.done():
return fut.result()
Expand Down
59 changes: 53 additions & 6 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted my last test to use new_test_loop(), which allows to reproduce the issue without suppressing cancellation.


def test_wait_for_waits_for_task_cancellation(self):
loop = asyncio.new_event_loop()
Expand Down
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.