Reject NaN deadlines on asyncio to match Trio - #1317
Open
dylanpulver wants to merge 2 commits into
Open
Conversation
CancelScope on Trio raises ValueError for a NaN deadline; on asyncio the NaN was stored as-is. Every deadline comparison against NaN is false, so the scope simply never fired: fail_after(math.nan) returned instead of raising TimeoutError, and move_on_after(math.nan) never cancelled. This is the same backend inconsistency agronholm#1307 fixed for sleep(), on the five remaining public surfaces that take a deadline or delay: CancelScope(deadline=...), the deadline setter, move_on_at, fail_at, move_on_after and fail_after. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo
agronholm
reviewed
Sep 9, 2026
Comment on lines
+2545
to
+2548
| pytest.param(lambda: move_on_at(math.nan), id="move_on_at"), | ||
| pytest.param(lambda: fail_at(math.nan), id="fail_at"), | ||
| pytest.param(lambda: move_on_after(math.nan), id="move_on_after"), | ||
| pytest.param(lambda: fail_after(math.nan), id="fail_after"), |
Owner
There was a problem hiding this comment.
Why are we explicitly testing these too? They all come down to CancelScope anyway.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NOTE Erasing or replacing the contents of this template will result in your pull
request being summarily closed without consideration!
Changes
Fixes #.
CancelScopeon Trio raisesValueErrorfor a NaN deadline; on asyncio the NaN was stored as-is. Every deadline comparison against NaN is false, so the scope never fired —fail_after(math.nan)returned instead of raisingTimeoutError, andmove_on_after(math.nan)never cancelled. A NaN delay reaching a timeout silently removes the timeout.This is the same backend inconsistency #1307 fixed for
sleep(), on the surfaces that were left. Measured before the change:sleep(nan)ValueErrorValueErrorCancelScope(deadline=nan)ValueError: deadline must not be NaNscope.deadline = nanValueError: deadline must not be NaNmove_on_at(nan)/fail_at(nan)ValueError: deadline must not be NaNmove_on_after(nan)/fail_after(nan)ValueError: deadline must not be NaNNegative and
-infdelays already agree across backends; only NaN diverged. The asyncioCancelScopenow rejects it in the constructor and thedeadlinesetter, with Trio's wording, which covers all six entry points since they all funnel through the scope.Checklist
If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):
tests/) which would fail without your patchdocs/), in case of behavior changes or newfeatures
docs/versionhistory.rst).Reverting
_backends/_asyncio.pyfails 18 of the 24 new test cases — every asyncio variant (asyncio,asyncio+uvloop,asyncio+eager) — while the 6 Trio ones still pass, which is the inconsistency itself. Full suite: 3972 passed, 299 skipped, 5 xfailed.pre-commit runclean on both changed files.No documentation change:
fail_after/move_on_afterdo not currently document delay validation, andsleep()'s:raises ValueError:note from #1307 is the only such mention. Happy to add matching notes if you'd like them.🤖 Generated with Claude Code
https://claude.ai/code/session_011M5uTyCU4WcNTsPvGrErDo