From c8a7b00d7f713054fb6a1ebef18fc1aeecaae2ab Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Wed, 30 Sep 2020 12:29:22 +0200 Subject: [PATCH 1/3] bpo-41891: ensure asyncio.wait_for waits for task completion --- Lib/asyncio/tasks.py | 5 ++++- .../next/Library/2020-09-30-13-35-29.bpo-41891.pNAeYI.rst | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2020-09-30-13-35-29.bpo-41891.pNAeYI.rst diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index 8b05434f273b52..45308bc5b74551 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -469,7 +469,10 @@ async def wait_for(fut, timeout, *, loop=None): return fut.result() else: fut.remove_done_callback(cb) - fut.cancel() + # We must ensure that the task is not running + # after wait_for() returns. + # See https://bugs.python.org/issue32751 + await _cancel_and_wait(fut, loop=loop) raise if fut.done(): diff --git a/Misc/NEWS.d/next/Library/2020-09-30-13-35-29.bpo-41891.pNAeYI.rst b/Misc/NEWS.d/next/Library/2020-09-30-13-35-29.bpo-41891.pNAeYI.rst new file mode 100644 index 00000000000000..75c25127803153 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-09-30-13-35-29.bpo-41891.pNAeYI.rst @@ -0,0 +1 @@ +Ensure asyncio.wait_for waits for task completion From 96564c65a1196e992b0a67f1c893a78b4afe8a38 Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Thu, 1 Oct 2020 09:39:18 +0200 Subject: [PATCH 2/3] bpo-41891: test cases for wait_for to behave consistently. asyncio.wait_for should wait for the future to terminate in all cases. --- Lib/test/test_asyncio/test_asyncio_waitfor.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Lib/test/test_asyncio/test_asyncio_waitfor.py diff --git a/Lib/test/test_asyncio/test_asyncio_waitfor.py b/Lib/test/test_asyncio/test_asyncio_waitfor.py new file mode 100644 index 00000000000000..eeb966f818891b --- /dev/null +++ b/Lib/test/test_asyncio/test_asyncio_waitfor.py @@ -0,0 +1,61 @@ +import asyncio +import unittest +import time + +def tearDownModule(): + asyncio.set_event_loop_policy(None) + + +class SlowTask: + """ Task will run for this defined time, ignoring cancel requests """ + TASK_TIMEOUT = 0.2 + + def __init__(self): + self.exited = False + + async def run(self): + exitat = time.time() + self.TASK_TIMEOUT + + while True: + tosleep = exitat - time.time() + if tosleep <= 0: + break + + try: + await asyncio.sleep(tosleep) + except asyncio.CancelledError: + pass + + self.exited = True + +class AsyncioWaitForTest(unittest.TestCase): + + async def atest_asyncio_wait_for_cancelled(self): + t = SlowTask() + + waitfortask = asyncio.create_task(asyncio.wait_for(t.run(), t.TASK_TIMEOUT * 2)) + await asyncio.sleep(0) + waitfortask.cancel() + await asyncio.wait({waitfortask}) + + self.assertTrue(t.exited) + + def test_asyncio_wait_for_cancelled(self): + asyncio.run(self.atest_asyncio_wait_for_cancelled()) + + async def atest_asyncio_wait_for_timeout(self): + t = SlowTask() + + try: + await asyncio.wait_for(t.run(), t.TASK_TIMEOUT / 2) + except asyncio.TimeoutError: + pass + + self.assertTrue(t.exited) + + def test_asyncio_wait_for_timeout(self): + asyncio.run(self.atest_asyncio_wait_for_timeout()) + + +if __name__ == '__main__': + unittest.main() From ffa0956e3a03494aca87ed2f077af77193bfd91a Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Fri, 2 Oct 2020 09:25:36 +0200 Subject: [PATCH 3/3] test_asyncio_waitfor: use monotonic time source --- Lib/test/test_asyncio/test_asyncio_waitfor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_asyncio/test_asyncio_waitfor.py b/Lib/test/test_asyncio/test_asyncio_waitfor.py index eeb966f818891b..2ca64abbeb527c 100644 --- a/Lib/test/test_asyncio/test_asyncio_waitfor.py +++ b/Lib/test/test_asyncio/test_asyncio_waitfor.py @@ -14,10 +14,10 @@ def __init__(self): self.exited = False async def run(self): - exitat = time.time() + self.TASK_TIMEOUT + exitat = time.monotonic() + self.TASK_TIMEOUT while True: - tosleep = exitat - time.time() + tosleep = exitat - time.monotonic() if tosleep <= 0: break