Skip to content

Prevent overflow in capped exponential backoff - #300

Merged
llucax merged 2 commits into
frequenz-floss:v0.x.xfrom
llucax:fix-backoff-overflow
Dec 1, 2026
Merged

llucax merged 2 commits into
frequenz-floss:v0.x.xfrom
llucax:fix-backoff-overflow

Conversation

@llucax

@llucax llucax commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

An unlimited exponential backoff can stop retrying during a long outage. With the default multiplier, the power overflows on retry 1,025 even though the interval has already reached its cap. I skip the power once all subsequent intervals are guaranteed to be capped.

Fixes #299.

@llucax llucax added this to the v0.11.6 milestone Nov 30, 2026
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:code Affects the code in general labels Nov 30, 2026
@llucax
llucax marked this pull request as ready for review November 30, 2026 13:52
@llucax
llucax enabled auto-merge November 30, 2026 14:05
@llucax
llucax force-pushed the fix-backoff-overflow branch from 5315f34 to eff2479 Compare November 30, 2026 14:15

@daniel-zullo-frequenz daniel-zullo-frequenz 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.

It seems there is at least one edge case that still fails.

With initial_interval=0.0, the delay should come entirely from jitter but retry 1_025 still raises OverflowError.

Regression tests using the existing retry and MockerFixture imports:

def test_zero_initial_interval_keeps_retrying(mocker: MockerFixture) -> None:
    """Keep using jitter when the initial interval is zero."""
    mocker.patch("random.uniform", return_value=0.5)
    strategy = retry.ExponentialBackoff(initial_interval=0.0)

    for _ in range(1_025):
        assert strategy.next_interval() == 0.5


def test_large_multiplier_can_reach_cap() -> None:
    """Reach the cap even if the multiplier's power would overflow."""
    strategy = retry.ExponentialBackoff(
        initial_interval=1e-200, multiplier=1e200, max_interval=60.0, jitter=0.0
    )

    assert strategy.next_interval() == 1e-200
    assert strategy.next_interval() == 1.0
    assert strategy.next_interval() == 60.0

@llucax

llucax commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Good catch. I will also add a commit to validate inputs, because as things are now, it accepts negative intervals for example.

Negative and nonfinite timing values can produce unusable retry delays.
Reject them at construction so callers get a clear error before retrying.
Check the range before converting to float so out-of-range integers also
raise ValueError.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Skip the power once a growing interval reaches the cap. A zero initial
interval needs only jitter. If the power overflows before the cap is
reached, calculate the scaled interval in log space before applying
jitter and the cap. The power can overflow even if the scaled interval
is still below the cap.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax

llucax commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Updated.

@llucax
llucax added this pull request to the merge queue Dec 1, 2026
Merged via the queue into frequenz-floss:v0.x.x with commit 57b4bee Dec 1, 2026
9 checks passed
@llucax
llucax deleted the fix-backoff-overflow branch December 1, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:code Affects the code in general part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Capped exponential backoff can overflow

2 participants