diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index bc79927b985627..d5598dbc522bc0 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -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 @@ -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: diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py index e425884a453e27..64290981f21382 100644 --- a/Lib/asyncio/timeouts.py +++ b/Lib/asyncio/timeouts.py @@ -1,4 +1,5 @@ import enum +import math from types import TracebackType @@ -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 diff --git a/Lib/test/test_asyncio/test_timeouts.py b/Lib/test/test_asyncio/test_timeouts.py index 43eebd3b56f150..39f9bb613a838c 100644 --- a/Lib/test/test_asyncio/test_timeouts.py +++ b/Lib/test/test_asyncio/test_timeouts.py @@ -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() @@ -244,6 +254,9 @@ 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() @@ -251,6 +264,18 @@ async def f(): 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"") diff --git a/Misc/NEWS.d/next/Library/2026-12-03-20-01-14.gh-issue-163101.TAtm8A.rst b/Misc/NEWS.d/next/Library/2026-12-03-20-01-14.gh-issue-163101.TAtm8A.rst new file mode 100644 index 00000000000000..50a823fa4db595 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-03-20-01-14.gh-issue-163101.TAtm8A.rst @@ -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`.