Skip to content

Keep subclasses of Retry and the other behavioral dependencies - #619

Merged
chrisguidry merged 4 commits into
mainfrom
retry-subclasses
Nov 20, 2026
Merged

chrisguidry merged 4 commits into
mainfrom
retry-subclasses

Conversation

@chrisguidry

@chrisguidry chrisguidry commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Retry and ExponentialRetry each constructed their own class by name in __aenter__, so a task that declared a subclass received a plain Retry or ExponentialRetry, and the subclass's handle_failure override and constructor state were dropped. Perpetual, Cron, Timeout, and ConcurrencyLimit did the same on entry, and ConcurrencyLimit, Cooldown, RateLimit, and Debounce did it when bound to a parameter through Annotated. Each now makes a shallow copy of the declared instance, which keeps the subclass and its state without calling a constructor whose arguments may differ from the base class's. Cron builds a new iterator for each copy. These methods and forever() now return Self.

The docs now say that with Perpetual or Cron a retry applies within one run and each run starts again at attempt 1, that exhausted retries do not stop the cycle, and how a task stops it by calling perpetual.cancel() on its last attempt. They also show how to subclass Retry for a custom policy. docket-rs has no subclassing and already behaves the same way with Perpetual; it gains a test of that and the same sentence in the documentation of Retry and ExponentialRetry.

Three things change for existing subclasses. A subclass that did not override __aenter__ was replaced by its base class and now reaches the task and the worker as itself, so its overrides of handle_failure, on_complete, or run start to run. A subclass that overrides __aenter__ and annotates its return as the base class no longer matches the base signature under mypy; annotate it as the subclass or Self. A subclass of ExponentialRetry that defines delay as a read-only property now fails on its second attempt, because the per-attempt delay is assigned on the subclass's copy.

Neither FastMCP nor Prefect subclasses these classes, and their test suites that use docket pass against this branch: 525 tests in FastMCP and 377 in Prefect.

Closes #441

🤖 Generated with Claude Code

https://claude.ai/code/session_01P7t1QdnTYnAA3UXK25adCM

Retry.__aenter__ and ExponentialRetry.__aenter__ each constructed their
own class by name, so a task that declared a subclass received a plain
Retry or ExponentialRetry, and the subclass's handle_failure override
and constructor state were dropped. Both now copy the declared instance,
which keeps the subclass and its state without calling a constructor
whose arguments may differ from the base class's.

The docs now say that with Perpetual or Cron a retry applies within one
run and each run starts again at attempt 1, and they show how to
subclass Retry for a custom policy. docket-rs has no subclassing and
already behaves the same way with Perpetual; it gains a test of that and
the same sentence in Retry's documentation.

Closes #441

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P7t1QdnTYnAA3UXK25adCM
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T15:01:49.786927Z 9695c2b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@read-the-docs-community

read-the-docs-community Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 docket | 🛠️ Build #36047004 | 📁 Comparing 6aa93fd against latest (887a179)

  🔍 Preview build  

3 files changed
± api-reference/index.html
± dependency-injection/index.html
± task-behaviors/index.html

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9695c2ba45

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

chrisguidry and others added 2 commits November 20, 2026 10:09
A review of the previous commit found that Retry.forever() and
ExponentialRetry.forever() were still typed as the base class, so a
subclass's forever() did not type-check as that subclass. Both now
return Self.

The docs said a task stops its cycle by calling perpetual.cancel(), but
each attempt has its own Perpetual, so a cancel on an attempt that is
retried is discarded; they now show the cancel on the last attempt. They
also said a failing hourly task makes its attempts "each hour", when the
next run is scheduled from the last attempt's start, and they now say
the per-attempt copy is shallow and what that means for subclass state.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P7t1QdnTYnAA3UXK25adCM
Perpetual, Cron, Timeout, and ConcurrencyLimit constructed their own
class by name on entry, and ConcurrencyLimit, Cooldown, RateLimit, and
Debounce did the same when bound to a parameter through Annotated, so a
subclass's overrides and constructor state were dropped the same way
Retry's were. Each now makes a shallow copy of the declared instance.
Cron builds a new iterator for each copy, because the declared
instance's iterator counts from when the task was declared.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P7t1QdnTYnAA3UXK25adCM
@chrisguidry chrisguidry changed the title Inject Retry subclasses into tasks and document retries with Perpetual Keep subclasses of Retry and the other behavioral dependencies Nov 20, 2026
The fundamentals suite shows each feature's happy path, and its
AGENTS.md sends edge cases to the other test files. The tests of retry
subclasses now sit with the other dependencies' subclass tests, and the
tests of each attempt's copy have their own module.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01P7t1QdnTYnAA3UXK25adCM
@chrisguidry
chrisguidry merged commit d67e216 into main Nov 20, 2026
57 checks passed
@chrisguidry
chrisguidry deleted the retry-subclasses branch November 20, 2026 15:53
chrisguidry added a commit that referenced this pull request Nov 23, 2026
docket-rs 0.6.0 adds `cancel_many` (#621) and re-authenticates
subscribed connections when credentials rotate (#624), and it carries
the retry fix from #619. The new API makes it a minor version.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01P7t1QcafMiwAyqmd1EdpQg

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
gaker added a commit that referenced this pull request Nov 23, 2026
Since #619, `Perpetual.__aenter__` returns `copy.copy()` of the declared
instance, and the copy keeps `cancelled` and `_next_when`. A task
function called directly, outside a worker, receives the declared
default itself. A `cancel()`, `after()`, or `at()` from that call then
carried into every later worker run of the task in the same process.
After a direct call that cancelled, each worker run stopped the
perpetual instead of rescheduling it.

`__aenter__` now resets `cancelled` to `False` and `_next_when` to
`None` on the copy. Cron's `__aenter__` already does the same for its
`_iterator`. This matches 0.29, where each run built a new
`Perpetual(every=..., automatic=...)`. A changed `every` still carries
over, as it did in 0.29, because 0.29 also read `self.every`.

The other dependencies that #619 moved to `copy.copy()` don't have this
problem:
- `Retry` and `ExponentialRetry` set `attempt` and `delay` from the
execution on each copy. `Retry.after()` writes `delay`, but 0.29 also
copied `self.delay`.
- `Timeout.run()` calls `start()` on the copy, which sets `_deadline`.
- `ConcurrencyLimit` sets every per-run field on its copy.

The new test calls a perpetual task directly with `after(1h)` and
`cancel()`, then runs it on a worker and checks that it runs three
times. Without the fix, it runs once.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Guidry <chris@theguidrys.us>
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.

ExponentialRetry + Cron/Perpetual: exhausted retries cause endless rescheduling

1 participant