Skip to content

[3.10] bpo-37658: Actually return result in race condition (GH-29202) #29831

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 1 commit into from
Nov 29, 2021
Merged
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
8 changes: 2 additions & 6 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,9 @@ 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:
raise exceptions.TimeoutError()

waiter = loop.create_future()
timeout_handle = loop.call_later(timeout, _release_waiter, waiter)
Expand Down Expand Up @@ -455,11 +453,9 @@ 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:
raise exceptions.TimeoutError()
finally:
timeout_handle.cancel()

Expand Down
38 changes: 8 additions & 30 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,20 +1148,16 @@ 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():
with contextlib.suppress(asyncio.CancelledError):
await asyncio.sleep(1)
return 1

loop = self.new_test_loop(gen)
async def main():
result = await asyncio.wait_for(inner(), timeout=.01)
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 @@ -1240,24 +1236,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):
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix issue when on certain conditions ``asyncio.wait_for()`` may allow a
coroutine to complete successfully, but fail to return the result,
potentially causing memory leaks or other issues.