8000 gh-95051: ensure that timeouts scheduled with `asyncio.Timeout` that … · python/cpython@0c6f898 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c6f898

Browse files
gh-95051: ensure that timeouts scheduled with asyncio.Timeout that have already expired are deliverered promptly (#95109)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
1 parent eb9c8a8 commit 0c6f898

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

Doc/library/asyncio-task.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ Timeouts
625625

626626
If *when* is a float, it is set as the new deadline.
627627

628+
if *when* is in the past, the timeout will trigger on the next
629+
iteration of the event loop.
630+
628631
.. method:: expired() -> bool
629632

630633
Return whether the context manager has exceeded its deadline

Lib/asyncio/timeouts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def reschedule(self, when: Optional[float]) -> None:
5252
self._timeout_handler = None
5353
else:
5454
loop = events.get_running_loop()
55-
self._timeout_handler = loop.call_at(
56-
when,
57-
self._on_timeout,
58-
)
55+
if when <= loop.time():
56+
self._timeout_handler = loop.call_soon(self._on_timeout)
57+
else:
58+
self._timeout_handler = loop.call_at(when, self._on_timeout)
5959

6060
def expired(self) -> bool:
6161
"""Is timeout expired during execution?"""

Lib/test/test_asyncio/test_timeouts.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ async def test_timeout_zero(self):
105105
self.assertLess(t1-t0, 2)
106106
self.assertTrue(t0 <= cm.when() <= t1)
107107

108+
async def test_timeout_zero_sleep_zero(self):
109+
loop = asyncio.get_running_loop()
110+
t0 = loop.time()
111+
with self.assertRaises(TimeoutError):
112+
async with asyncio.timeout(0) as cm:
113+
await asyncio.sleep(0)
114+
t1 = loop.time()
115+
self.assertTrue(cm.expired())
116+
# 2 sec for slow CI boxes
117+
self.assertLess(t1-t0, 2)
118+
self.assertTrue(t0 <= cm.when() <= t1)
119+
120+
async def test_timeout_in_the_past_sleep_zero(self):
121+
loop = asyncio.get_running_loop()
122+
t0 = loop.time()
123+
with self.assertRaises(TimeoutError):
124+
async with asyncio.timeout(-11) a A59F s cm:
125+
await asyncio.sleep(0)
126+
t1 = loop.time()
127+
self.assertTrue(cm.expired())
128+
# 2 sec for slow CI boxes
129+
self.assertLess(t1-t0, 2)
130+
self.assertTrue(t0 >= cm.when() <= t1)
131+
108132
async def test_foreign_exception_passed(self):
109133
with self.assertRaises(KeyError):
110134
async with asyncio.timeout(0.01) as cm:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure that timeouts scheduled with :class:`asyncio.Timeout` that have already expired are delivered promptly.

0 commit comments

Comments
 (0)
0