Avoid underflow in exponential wait threshold#656
Merged
mergify[bot] merged 1 commit intoJul 15, 2026
Conversation
jd
approved these changes
Jul 15, 2026
Contributor
Merge Queue Status
This pull request spent 9 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
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.
Summary
wait_exponentialcan raiseValueError: math domain errorinstead of returning a wait time whenmultiplier/maxare finite and positive but their ratio underflows to0.0. It's a regression from the threshold optimization in #654. The fix computes the same threshold as a difference of logarithms so there's no underflowing division.How narrow this is
Pretty narrow, and I want to be upfront about it. The crash needs
max / multiplierto underflow a 64-bit float all the way to0.0, i.e.multiplierbigger thanmaxby more than ~308 orders of magnitude (wait_exponential(multiplier=1e300, max=1e-100)), or a non-finitemultiplier. Everyday configs —max=0,multiplier=0, any realistic seconds/milliseconds values — already go through the existing guards and are untouched.I ran into it while checking the #654 change for edge cases, not in production. Sending it because it's a genuine regression in a recently merged optimization and the fix is one line with no behavior change for valid input — not because I think it bites many people. Happy for you to close it if it's below the bar.
Root cause
The #654 optimization short-circuits to the cap when the exponent would exceed
log(max / multiplier). Formingmax / multiplierfirst can underflow to0.0for extreme finite inputs, andmath.log(0.0)raises. In that regime the uncapped wait is already pastmax, so the right answer is justmax.log(max) - log(multiplier)is the same value (log(a/b) = log(a) - log(b)) without the underflowing intermediate, so the early-return optimization still holds for every valid input.Testing
uv run pytest -q→ 172 passed, 12 subtestsuv run poe lint,uv run poe mypy,uv run reno lint→ all pass