Skip to content

Keep Retry intervals given as a one-shot iterator - #2552

Merged
selwin merged 1 commit into
rq:masterfrom
ericdelorefice:fix/retry-interval-iterator
Oct 26, 2026
Merged

selwin merged 1 commit into
rq:masterfrom
ericdelorefice:fix/retry-interval-iterator

Conversation

@ericdelorefice

@ericdelorefice ericdelorefice commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Retry(interval=...) accepts any iterable, but Retry.__init__() checks the values for negatives by looping over interval and then builds the list with list(interval). A generator, map() or iter() is used up by the check, so Retry.intervals ends up as [] with no error.

With retry=Retry(max=3, interval=<generator>) passed to enqueue(), the first time the job raises, Job.get_retry_interval() indexes the empty list and raises IndexError inside the worker's failure handling. The worker logs "found an unhandled exception, quitting..." and exits, and the job stays started. When it is later picked up as abandoned, StartedJobRegistry.cleanup() calls job.retry() and hits the same IndexError, so the next worker quits during its startup maintenance as well.

A job that returns Retry(interval=<generator>) doesn't crash anything, but Retry.get_interval() returns 0 for an empty list, so it is requeued at once with no backoff.

Reproduction

Real Redis 8.10.2, rq master (aacfa5b) and rq 2.12.0 from PyPI, default Worker, nothing mocked:

retry = Retry(max=3, interval=(10 * 2**i for i in range(3)))  # 10, 20, 40
retry.intervals  # []
job = queue.enqueue(always_fails, retry=retry)
queue.enqueue(always_fails)  # another job behind it
Worker([queue], connection=redis).work(max_idle_time=5)
# IndexError: list index out of range
# Worker ...: found an unhandled exception, quitting...
# work() returns at once; the first job is still `started`, the second still `queued`.

After the first job's entry in the started registry expired (about 90 seconds here), a new Worker quit the same way from clean_registries(). With a list, Retry(max=3, interval=[10, 20, 40]), the job is scheduled for retry as expected.

Change

Retry.__init__() now builds the list first and checks that, which is what Repeat.__init__() already does. Lists, tuples and ranges behave as before, and negative values are still rejected.

Verification

  • The new test_retry_interval_from_iterator fails on master and passes with the fix, 10 runs each way. It also checks that a negative value in an iterator is still rejected and that a failing job is scheduled for retry.
  • A separate check of the list form through the same worker path passes before and after.
  • tests/test_retry.py: 18 passed before, 19 after. Full suite: 768 passed, 24 skipped on master; 769 passed, 24 skipped with the fix.
  • ruff check and ruff format --check on the changed files are clean.

Summary by CodeRabbit

  • Bug Fixes
    • Retry intervals supplied as one-shot iterators are now retained and applied correctly, allowing failed jobs to be scheduled for retry as configured.
    • Negative retry intervals continue to be rejected.

@coderabbitai

coderabbitai Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9bfd2c21-7beb-4f8c-9a8e-3d0fb7a2dc59
Commits

Reviewing files that changed from the base of the PR and between aacfa5b and 91b7b00.

Files selected for processing: 2
  • rq/job.py
  • tests/test_retry.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

Retry.__init__ now materializes retry intervals before validating them. Tests cover one-shot iterators, negative intervals, and scheduling a failing job with iterator-backed retry settings.

Changes: Retry interval handling

Layer / File(s) Summary
Preserve and validate retry intervals
rq/job.py, tests/test_retry.py
Retry.__init__ converts the interval iterable to a list before checking for negative values. Tests verify that iterator values are retained, negative values raise ValueError, and a failing job is scheduled by a burst worker.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: selwin

Merge Risk: Minimal · up to 91b7b

No actionable merge-blocking risk remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving Retry intervals provided through a one-shot iterator.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.22%. Comparing base (2de9491) to head (91b7b00).
⚠️ Report is 366 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2552      +/-   ##
==========================================
+ Coverage   93.61%   95.22%   +1.60%     
==========================================
  Files          28       43      +15     
  Lines        3760     5612    +1852     
==========================================
+ Hits         3520     5344    +1824     
- Misses        240      268      +28     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@selwin
selwin merged commit c64fc4f into rq:master Oct 26, 2026
31 checks passed
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.

2 participants