Skip to content

Add --reruns-delay-backoff-factor for exponential rerun backoff#336

Open
ovsds wants to merge 2 commits into
pytest-dev:masterfrom
ovsds:reruns-delay-backoff-factor
Open

Add --reruns-delay-backoff-factor for exponential rerun backoff#336
ovsds wants to merge 2 commits into
pytest-dev:masterfrom
ovsds:reruns-delay-backoff-factor

Conversation

@ovsds

@ovsds ovsds commented Jun 20, 2026

Copy link
Copy Markdown

Summary

Adds a --reruns-delay-backoff-factor option (with a matching reruns_delay_backoff_factor marker kwarg / ini setting) for exponential backoff between reruns. Today --reruns-delay is a fixed delay; this lets the delay grow after each attempt.

The delay before the n-th re-run becomes:

reruns_delay * reruns_delay_backoff_factor ** (n - 1)

The default factor is 1.0, so existing behaviour (a constant delay) is unchanged. A single factor covers both cases — 1.0 = constant, >1 = exponential — so no separate boolean flag is needed.

Usage

pytest --reruns 3 --reruns-delay 1 --reruns-delay-backoff-factor 2   # waits 1, 2, 4s
@pytest.mark.flaky(reruns=3, reruns_delay=1, reruns_delay_backoff_factor=2)
def test_example(): ...

Implementation notes

  • New CLI option + addini mirror the existing --reruns-delay machinery.
  • New get_reruns_delay_backoff_factor() getter mirrors get_reruns_delay() (marker kwarg → CLI → ini → default 1.0), with a < 0 guard that warns and falls back to 1.0.
  • The single time.sleep(delay) site in the rerun loop now uses delay * factor ** (item.execution_count - 1)execution_count is already the per-attempt counter.

Tests / docs

  • tests/: CLI sequence, marker sequence, and negative-factor warning (full suite: 131 passed, 1 skipped; ruff check/ruff format clean).
  • README.rst + CHANGES.rst updated.

Context: motivated by replacing a custom pytest_runtest_setup backoff hook in our repo with native library support.

@ovsds ovsds changed the title Add --reruns-delay-backoff-factor for exponential rerun backoff Add --reruns-delay-backoff-factor for exponential rerun backoff Jun 20, 2026

@icemac icemac 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.

LGTM.

Copilot AI 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.

Pull request overview

This PR adds an exponential backoff capability to pytest-rerunfailures by introducing a new rerun-delay backoff factor that scales the existing --reruns-delay between attempts, with corresponding marker/ini support and documentation/tests.

Changes:

  • Add --reruns-delay-backoff-factor CLI option plus matching reruns_delay_backoff_factor ini setting and marker kwarg.
  • Apply exponential delay scaling in the rerun loop (time.sleep) based on attempt count.
  • Add tests and update docs/changelog to cover the new behavior and negative-factor warning.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/pytest_rerunfailures.py Adds the new option/ini + getter and applies backoff factor in the rerun sleep calculation.
tests/test_pytest_rerunfailures.py Adds coverage for CLI, marker, and negative-factor warning behavior.
README.rst Documents the new option and provides usage examples for exponential backoff.
CHANGES.rst Adds changelog entry describing the new feature and its default behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/pytest_rerunfailures.py
Comment thread README.rst
@icemac

icemac commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

@ovsds Could you please look into the Copilot comments and resolve them?

ovsds added 2 commits June 26, 2026 10:34
Add a reruns-delay-backoff-factor option (with matching
reruns_delay_backoff_factor marker kwarg / ini setting) that multiplies
the rerun delay after each attempt, so the delay before the n-th re-run
is reruns_delay * factor ** (n - 1). Default factor is 1.0, keeping the
current constant-delay behaviour unchanged.

Includes tests (CLI, marker, negative-factor warning), README docs and a
changelog entry.
- Support positional third flaky() arg for the backoff factor and default
  to 1.0 when a marker is present without the factor (mirrors get_reruns_delay)
- Add positional-arg marker test
- Use underscore identifiers in README backoff formula
@ovsds ovsds force-pushed the reruns-delay-backoff-factor branch from a716562 to d769995 Compare June 26, 2026 08:34

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment on lines +246 to +251
if factor < 0:
factor = 1.0
warnings.warn(
"Rerun delay backoff factor cannot be < 0. Using default value: 1.0"
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

same behaviour as in delay, not introducing new layer of complexity

@ovsds

ovsds commented Jun 26, 2026

Copy link
Copy Markdown
Author

@icemac done. One new issue copilot found after adressing fixes, but i marked it as unnecessary.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants