Skip to content

gh-163101: Reject NaN deadlines in asyncio.timeout - #163113

Closed
josh-clairox wants to merge 2 commits into
python:mainfrom
josh-clairox:gh-163101-timeout-nan
Closed

josh-clairox wants to merge 2 commits into
python:mainfrom
josh-clairox:gh-163101-timeout-nan

Conversation

@josh-clairox

@josh-clairox josh-clairox commented Dec 3, 2026 •

Copy link
Copy Markdown

asyncio.timeout(float("nan")) puts a NaN deadline on the loop's timer heap. NaN fails every comparison, so it skips the "already expired" check and breaks the heap's ordering, which delays or cancels unrelated work.

This rejects a NaN deadline in Timeout.reschedule() with ValueError, matching asyncio.sleep(). timeout(), timeout_at(), reschedule() and wait_for() all go through that method. Tests, docs and a NEWS entry are included.

@python-cla-bot

python-cla-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community

read-the-docs-community Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

📖 2 files modified


Preview · build overview · cpython-previews · built Dec. 4, 2026, 6:41 a.m. UTC

@efanntyo

efanntyo commented Dec 4, 2026

Copy link
Copy Markdown

One additional regression boundary: rejecting NaN while a timeout is already armed should preserve the old deadline and its cancellation callback. The new reschedule check currently follows reschedule(None), so it only covers an unarmed timeout.

A deterministic variant can enter timeout(0), save cm.when() and cm._timeout_handler, call cm.reschedule(float('nan')) under assertRaises(ValueError), assert the deadline/handle are unchanged and the handle is not cancelled, then await asyncio.sleep(0) under an outer assertRaises(TimeoutError). That also proves the original timeout still fires, without a wall-clock delay.

I checked this with an AI-assisted original probe on Python 3.12.16/Linux. The installed method accepts NaN and cancels the original handle; substituting only the exact reschedule method from da32179 (blob 64290981f21382423e495e262ca91f1a9daf72a2) into the host Timeout preserves the handle and raises the original TimeoutError on the next yield. This is an isolated method-level check, not a full PR build or upstream-suite result.

@asvetlov asvetlov left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add isnan() check to Timeout.__init__ for failing fast at the moment of the timeout creation, not at a moment of __aenter__() call.

Also, protect loop.call_at() (and indirectly loop.call_later()) by check for NaN.

@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@picnixz

picnixz commented Dec 4, 2026

Copy link
Copy Markdown
Member

The OP was not even given a chance to submit their own PR and they seem to want to.

@picnixz picnixz added the AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. label Dec 4, 2026
@josh-clairox

Copy link
Copy Markdown
Author

Fair enough, sorry for jumping ahead. Happy for @hu-jeff to take it, and thanks @asvetlov for the review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. awaiting changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants