Skip to content
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.

Already on GitHub? Sign in to your account

gh-105331: Fix asyncio.sleep() bug #105641

Merged
merged 14 commits into from
Jun 12, 2023
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.
Jump to
Jump to file
Failed to load files.
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 @@ -426,6 +426,9 @@ Sleeping
.. versionchanged:: 3.10
Removed the *loop* parameter.

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


Running Tasks Concurrently
==========================
Expand Down
4 changes: 4 additions & 0 deletions Lib/asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import functools
import inspect
import itertools
import math
import types
import warnings
import weakref
Expand Down Expand Up @@ -646,6 +647,9 @@ async def sleep(delay, result=None):
await __sleep0()
return result

if math.isnan(delay):
raise ValueError("Invalid delay: NaN (not a number)")

loop = events.get_running_loop()
future = loop.create_future()
h = loop.call_later(delay,
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_asyncio/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,21 @@ async def sleeper(dt, arg):
self.assertEqual(t.result(), 'yeah')
self.assertAlmostEqual(0.1, loop.time())

def test_sleep_when_delay_is_nan(self):

def gen():
yield

loop = self.new_test_loop(gen)

async def sleeper():
await asyncio.sleep(float("nan"))

t = self.new_task(loop, sleeper())

with self.assertRaises(ValueError):
loop.run_until_complete(t)

def test_sleep_cancel(self):

def gen():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Raise :exc:`ValueError` if the ``delay`` argument to :func:`asyncio.sleep` is a NaN (matching :func:`time.sleep`).