8000 bpo-37658: Actually return result in race condition by Dreamsorcerer · Pull Request #29202 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-37658: Actually return result in race condition #29202

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

Merged
merged 6 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments. 8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ async def wait_for(fut, timeout):

await _cancel_and_wait(fut, loop=loop)
try:
fut.result()
return fut.result()
except exceptions.CancelledError as exc:
raise exceptions.TimeoutError() from exc
else:
Expand Down Expand Up @@ -455,7 +455,7 @@ async def wait_for(fut, timeout):
# exception, we should re-raise it
# See https://bugs.python.org/issue40607
try:
fut.result()
return fut.result()
except exceptions.CancelledError as exc:
raise exceptions.TimeoutError() from exc
else:
Expand Down
37 changes: 7 additions & 30 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,20 +1009,15 @@ 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
async def inner():
await asyncio.wait_for(asyncio.sleep(1), timeout=2)
return 1

loop = self.new_test_loop(gen)
async def main():
result = await asyncio.wait_for(inner(), timeout=1)
assert result == 1

fut = self.new_future(loop)
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")
asyncio.run(main())

def test_wait_for_waits_for_task_cancellation(self):
loop = asyncio.new_event_loop()
Expand Down Expand Up @@ -1101,24 +1096,6 @@ async def inner():
with self.assertRaises(FooException):
loop.run_until_complete(foo())

def test_wait_for_raises_timeout_error_if_returned_during_cancellation(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test literally tests that we don't return a value when it has completed successfully...

I can't see any reason for this test, the issue linked to the PR does not mention anything that warrants this test existing: https://bugs.python.org/issue40607

So, as far as I can tell, this test should be binned and the behaviour should be as defined in the above changes.

loop = asyncio.new_event_loop()
self.addCleanup(loop.close)

async def foo():
async def inner():
try:
await asyncio.sleep(0.2)
except asyncio.CancelledError:
return 42

inner_task = self.new_task(loop, inner())

await asyncio.wait_for(inner_task, timeout=_EPSILON)

with self.assertRaises(asyncio.TimeoutError):
loop.run_until_complete(foo())

def test_wait_for_self_cancellation(self):
loop = asyncio.new_event_loop()
self.addCleanup(loop.close)
Expand Down
0