fix: wait_exponential with zero multiplier no longer overflows into max wait - #712
TrueFurina wants to merge 5 commits into
Conversation
|
Lint fix: the |
|
Correction: my earlier lint fix used an incomplete copy of the project ruff config (the isort subsection was dropped), so my local run passed while CI correctly flagged I001 at |
|
Root cause found via the re-triggered run: two real violations my earlier local check missed because I had reconstructed the ruff config by hand instead of using the project file - I001 (import block order, fixed via the project ruff config) and B904 on the intentional implicit-chaining |
Fixes #710.
Problem
With
wait_exponential(multiplier=0)(or any zero multiplier), the result ofmultiplier * exp_base**exponentis always 0. Butexp_base**exponentis computed first, and for large attempt numbers (2**1024at attempt 1025 with the defaultexp_base=2) it raisesOverflowError— which is caught by theexcept OverflowError: return self.maxbranch, so the strategy wrongly returnsmaxinstead of 0.This is easy to hit with a long-lived
@retry(wait=wait_exponential(multiplier=0)): the process retried hourly for a few days, then every "failure" wait silently became the maximum wait.Fix
Short-circuit the zero-multiplier case before computing the power:
The
minclamp still applies afterwards, sowait_exponential(multiplier=0, min=5)correctly returns 5.Testing
pytest tests/test_tenacity.py -k exponential— 18 passed (16 existing + 2 new regression tests:test_exponential_zero_multiplier_returns_zero_for_huge_attemptscovering attempts 2/10/1025 andexp_base=2.0, andtest_exponential_zero_multiplier_with_min_wait)multiplier=1still returns 1/2/4/8... and caps atmax)