From dce6d745fe4516648e2293d518ef911193b30966 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Wed, 20 Jul 2022 13:10:05 +0100 Subject: [PATCH 1/8] gh-95051: handle timeouts.timeout(0) and timeouts.timeout(-1) --- Lib/asyncio/timeouts.py | 16 +++++++++++----- Lib/test/test_asyncio/test_timeouts.py | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py index a89205348ff24c..b76a55cc0cc07f 100644 --- a/Lib/asyncio/timeouts.py +++ b/Lib/asyncio/timeouts.py @@ -52,10 +52,10 @@ def reschedule(self, when: Optional[float]) -> None: self._timeout_handler = None else: loop = events.get_running_loop() - self._timeout_handler = loop.call_at( - when, - self._on_timeout, - ) + if loop.time() >= when: + self._timeout_handler = loop.call_soon(self._on_timeout) + else: + self._timeout_handler = loop.call_at(when, self._on_timeout) def expired(self) -> bool: """Is timeout expired during execution?""" @@ -126,7 +126,13 @@ def timeout(delay: Optional[float]) -> Timeout: into TimeoutError. """ loop = events.get_running_loop() - return Timeout(loop.time() + delay if delay is not None else None) + if delay is None: + return Timeout(None) + + if delay <= 0: + return Timeout(0) + + return Timeout(loop.time() + delay) def timeout_at(when: Optional[float]) -> Timeout: diff --git a/Lib/test/test_asyncio/test_timeouts.py b/Lib/test/test_asyncio/test_timeouts.py index 9801541e55b7f8..edef0ffdf30b34 100644 --- a/Lib/test/test_asyncio/test_timeouts.py +++ b/Lib/test/test_asyncio/test_timeouts.py @@ -105,6 +105,30 @@ async def test_timeout_zero(self): self.assertLess(t1-t0, 2) self.assertTrue(t0 <= cm.when() <= t1) + async def test_timeout_zero_sleep_zero(self): + loop = asyncio.get_running_loop() + t0 = loop.time() + with self.assertRaises(TimeoutError): + async with asyncio.timeout(0) as cm: + await asyncio.sleep(0) + t1 = loop.time() + self.assertTrue(cm.expired()) + # 2 sec for slow CI boxes + self.assertLess(t1-t0, 2) + self.assertTrue(t0 <= cm.when() <= t1) + + async def test_timeout_in_the_past_sleep_zero(self): + loop = asyncio.get_running_loop() + t0 = loop.time() + with self.assertRaises(TimeoutError): + async with asyncio.timeout(-11) as cm: + await asyncio.sleep(0) + t1 = loop.time() + self.assertTrue(cm.expired()) + # 2 sec for slow CI boxes + self.assertLess(t1-t0, 2) + self.assertTrue(t0 <= cm.when() <= t1) + async def test_foreign_exception_passed(self): with self.assertRaises(KeyError): async with asyncio.timeout(0.01) as cm: From 51686d702bd327473cad13ea0f83ef0cb4c1f541 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 21 Jul 2022 22:59:24 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst diff --git a/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst b/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst new file mode 100644 index 00000000000000..26f86e8a38344c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst @@ -0,0 +1 @@ +special case asyncio.timeout(0) and timeouts in the past From 418a81f2cc6d13ac5b9a306f91016c9cd7276691 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 22 Jul 2022 00:09:47 +0100 Subject: [PATCH 3/8] Update Lib/asyncio/timeouts.py --- Lib/asyncio/timeouts.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py index b76a55cc0cc07f..960a39a91501c9 100644 --- a/Lib/asyncio/timeouts.py +++ b/Lib/asyncio/timeouts.py @@ -126,13 +126,7 @@ def timeout(delay: Optional[float]) -> Timeout: into TimeoutError. """ loop = events.get_running_loop() - if delay is None: - return Timeout(None) - - if delay <= 0: - return Timeout(0) - - return Timeout(loop.time() + delay) + return Timeout(loop.time() + delay if delay is not None else None) def timeout_at(when: Optional[float]) -> Timeout: From 174a3ff0e0188f81c1fa1800458fe1fefa78f0a8 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 22 Jul 2022 00:11:34 +0100 Subject: [PATCH 4/8] Update Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst --- .../next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst b/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst index 26f86e8a38344c..0c5809cdc9ec62 100644 --- a/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst +++ b/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst @@ -1 +1 @@ -special case asyncio.timeout(0) and timeouts in the past +ensure that timeouts scheduled with :class:`asyncio.Timeout` that have already expired are delivered promptly From a42e1e190cbe175c3db5a05435bd11ce663d2e90 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 22 Jul 2022 00:40:31 +0100 Subject: [PATCH 5/8] Update Lib/test/test_asyncio/test_timeouts.py --- Lib/test/test_asyncio/test_timeouts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_timeouts.py b/Lib/test/test_asyncio/test_timeouts.py index edef0ffdf30b34..b9bac6f783776b 100644 --- a/Lib/test/test_asyncio/test_timeouts.py +++ b/Lib/test/test_asyncio/test_timeouts.py @@ -127,7 +127,7 @@ async def test_timeout_in_the_past_sleep_zero(self): self.assertTrue(cm.expired()) # 2 sec for slow CI boxes self.assertLess(t1-t0, 2) - self.assertTrue(t0 <= cm.when() <= t1) + self.assertTrue(t0 >= cm.when() <= t1) async def test_foreign_exception_passed(self): with self.assertRaises(KeyError): From 495cd2b7c94270c27ccb5a0ea0c79228cd8d06f7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 22 Jul 2022 18:33:28 +0100 Subject: [PATCH 6/8] Update Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> --- .../next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst b/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst index 0c5809cdc9ec62..40196dd214a283 100644 --- a/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst +++ b/Misc/NEWS.d/next/Library/2022-07-21-22-59-22.gh-issue-95109.usxA9r.rst @@ -1 +1 @@ -ensure that timeouts scheduled with :class:`asyncio.Timeout` that have already expired are delivered promptly +Ensure that timeouts scheduled with :class:`asyncio.Timeout` that have already expired are delivered promptly. From 6b01baac29fc546ff48d2a1bbabc62966917ddf7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 23 Jul 2022 00:59:32 +0100 Subject: [PATCH 7/8] Update Lib/asyncio/timeouts.py Co-authored-by: Guido van Rossum --- Lib/asyncio/timeouts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py index 960a39a91501c9..94d25535fbc059 100644 --- a/Lib/asyncio/timeouts.py +++ b/Lib/asyncio/timeouts.py @@ -52,7 +52,7 @@ def reschedule(self, when: Optional[float]) -> None: self._timeout_handler = None else: loop = events.get_running_loop() - if loop.time() >= when: + if when <= loop.time(): self._timeout_handler = loop.call_soon(self._on_timeout) else: self._timeout_handler = loop.call_at(when, self._on_timeout) From b7fe485425b83ebe987b8418b55ef9aa6d6e9132 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sat, 23 Jul 2022 01:08:15 +0100 Subject: [PATCH 8/8] document asyncio.timeout(-1) --- Doc/library/asyncio-task.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index a307d22e456ebf..a6b638c1124094 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -625,6 +625,9 @@ Timeouts If *when* is a float, it is set as the new deadline. + if *when* is in the past, the timeout will trigger on the next + iteration of the event loop. + .. method:: expired() -> bool Return whether the context manager has exceeded its deadline