8000 [3.11] gh-95051: ensure that timeouts scheduled with `asyncio.Timeout` that have already expired are deliverered promptly (GH-95109) by miss-islington · Pull Request #95216 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-95051: ensure that timeouts scheduled with asyncio.Timeout that have already expired are deliverered promptly (GH-95109) #95216

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.

Alread 8000 y on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2022
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
3 changes: 3 additions & 0 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions Lib/asyncio/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 when <= loop.time():
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?"""
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_asyncio/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure that timeouts scheduled with :class:`asyncio.Timeout` that have already expired are delivered promptly.
0