Skip to content
Closed
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
6 changes: 6 additions & 0 deletions Doc/library/asyncio-task.rst
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@ Timeouts

.. versionadded:: 3.11

.. versionchanged:: next
Raises :exc:`ValueError` if *delay* is :data:`~math.nan`.

.. function:: timeout_at(when)

Similar to :func:`asyncio.timeout`, except *when* is the absolute time
Expand All @@ -860,6 +863,9 @@ Timeouts

.. versionadded:: 3.11

.. versionchanged:: next
Raises :exc:`ValueError` if *when* is :data:`~math.nan`.

.. function:: wait_for(fut, timeout)
:async:

Expand Down
3 changes: 3 additions & 0 deletions Lib/asyncio/timeouts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
import math

from types import TracebackType

Expand Down Expand Up @@ -54,6 +55,8 @@ def reschedule(self, when: float | None) -> None:
raise RuntimeError(
f"Cannot change state of {self._state.value} Timeout",
)
if when is not None and math.isnan(when):
raise ValueError("Invalid deadline: NaN (not a number)")

self._when = when

Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_asyncio/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ async def test_timeout_at_disabled(self):
self.assertFalse(cm.expired())
self.assertIsNone(cm.when())

async def test_timeout_nan(self):
with self.assertRaisesRegex(ValueError, "NaN"):
async with asyncio.timeout(float("nan")):
pass

async def test_timeout_at_nan(self):
with self.assertRaisesRegex(ValueError, "NaN"):
async with asyncio.timeout_at(float("nan")):
pass

async def test_timeout_zero(self):
loop = asyncio.get_running_loop()
t0 = loop.time()
Expand Down Expand Up @@ -244,13 +254,28 @@ async def f():
self.assertEqual(cm.when(), deadline2)
cm.reschedule(None)
self.assertIsNone(cm.when())
with self.assertRaisesRegex(ValueError, "NaN"):
cm.reschedule(float("nan"))
self.assertIsNone(cm.when())

task.cancel()

with self.assertRaises(asyncio.CancelledError):
await task
self.assertFalse(cm.expired())

async def test_reschedule_nan_keeps_armed_timeout(self):
with self.assertRaises(TimeoutError):
async with asyncio.timeout(0) as cm:
when = cm.when()
handle = cm._timeout_handler
with self.assertRaisesRegex(ValueError, "NaN"):
cm.reschedule(float("nan"))
self.assertEqual(cm.when(), when)
self.assertIs(cm._timeout_handler, handle)
self.assertFalse(handle.cancelled())
await asyncio.sleep(0)

async def test_repr_active(self):
async with asyncio.timeout(10) as cm:
self.assertRegex(repr(cm), r"<Timeout \[active\] when=\d+\.\d*>")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raise :exc:`ValueError` if the deadline of :func:`asyncio.timeout`,
:func:`asyncio.timeout_at` or :meth:`asyncio.Timeout.reschedule` is a NaN,
matching :func:`asyncio.sleep`.