Skip to content

Fix autoretry retry kwargs mutation - #10458

Closed
daleselaji-dev wants to merge 1 commit into
celery:mainfrom
daleselaji-dev:codex/fix-autoretry-retry-kwargs
Closed

daleselaji-dev wants to merge 1 commit into
celery:mainfrom
daleselaji-dev:codex/fix-autoretry-retry-kwargs

Conversation

@daleselaji-dev

Copy link
Copy Markdown

Description

Fixes #10456.

The autoretry wrapper now copies retry_kwargs for each retry attempt before adding calculated countdown or max_retries values. This prevents the task-level dictionary captured by the wrapper from being mutated and shared between executions.

Added a regression test covering retry backoff values and verifying that the caller-provided dictionary remains unchanged.

Verification

  • python -m pytest t/unit/tasks/test_tasks.py -k autoretry -q (35 passed)
  • git diff --check

@auvipy
auvipy requested review from auvipy and a lite review from Copilot August 5, 2026 16:38

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

Fixes Celery’s autoretry_for wrapper mutating a single shared retry_kwargs dict captured at task registration time, which could corrupt computed backoff values across executions (including concurrent executions in a worker process).

Changes:

  • Copy retry_kwargs per retry attempt before adding/overriding countdown and max_retries, avoiding cross-execution mutation.
  • Add a regression unit test ensuring the caller-provided retry_kwargs dict remains unchanged while backoff countdowns are still computed correctly.

Reviewed changes

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

File Description
celery/app/autoretry.py Avoids mutating the shared retry_kwargs by copying it per retry attempt before adding calculated values.
t/unit/tasks/test_tasks.py Adds a regression test asserting retry_kwargs is not mutated and that backoff countdowns match expectations.

@codecov

codecov Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.44%. Comparing base (571efe8) to head (bb04d42).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10458   +/-   ##
=======================================
  Coverage   88.44%   88.44%           
=======================================
  Files         153      153           
  Lines       19808    19809    +1     
  Branches     2304     2304           
=======================================
+ Hits        17519    17520    +1     
  Misses       1993     1993           
  Partials      296      296           
Flag Coverage Δ
unittests 88.41% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@Namraa310806

Copy link
Copy Markdown
Contributor

Nice fix, verified it works. One code path this doesn't have test coverage for: retry_kwargs set as a class attribute on a shared base Task (the getattr(task, 'retry_kwargs', {}) fallback), rather than passed per-task via the decorator. That path hits the same shared-dict issue — worse, it's shared across every task class using that base — and unlike the concurrency angle, it's fully deterministic to reproduce sequentially (no interleaving needed): retry task_a once, and the base class's retry_kwargs permanently picks up a countdown key that leaks into task_b.

@aviadatsnyk if you agree this is worth covering, happy to implement the regression test — as I mentioned in the issue, I was already working on this.

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.

autoretry_for + retry_backoff mutate a single retry_kwargs dict shared across all executions of a task, corrupting backoff under concurrent execution

4 participants