Repository navigation
Fix shared retry_kwargs mutation in autoretry - #10461
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a concurrency/state-leak bug in Celery’s autoretry wrapper by ensuring the retry_kwargs mapping captured at task-registration time is not mutated across retry attempts, including when retry_kwargs is inherited as a shared class attribute from a base Task.
Changes:
- Copy
retry_kwargsper retry attempt insideadd_autoretry_behaviour()before injecting calculated values likecountdown/max_retries. - Add a regression test that specifically exercises the
getattr(task, "retry_kwargs", {})branch whenretry_kwargsis shared via a baseTaskclass. - Verify the shared base dict remains unchanged after retries and that tasks inheriting from the same base don’t share mutated retry state.
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 shared retry_kwargs by copying into a per-attempt dict before adding backoff/override values. |
t/unit/tasks/test_tasks.py |
Adds regression coverage for inherited/shared base-class retry_kwargs to prevent cross-task leakage. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10461 +/- ##
=======================================
Coverage 88.68% 88.68%
=======================================
Files 153 153
Lines 19852 19853 +1
Branches 2311 2311
=======================================
+ Hits 17606 17607 +1
Misses 1948 1948
Partials 298 298
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
can you cross check the changes or approach followed in this PR #10465 and see which might be better |
|
Thanks for pointing me to #10465. I cross-checked its approach locally against the relevant autoretry tests. Both approaches are trying to solve the same underlying issue: avoiding mutation of the The main difference I found is how the per-invocation copy is introduced. Approach in #10465#10465 currently does: retry_kwargs = dict(retry_kwargs)inside the nested The problem with this approach is Python's scoping behavior. Since Therefore, when evaluating: retry_kwargs = dict(retry_kwargs)the I applied the implementation from #10465 locally and ran the same autoretry-focused unit test selection. The result was:
The failures included existing autoretry behavior such as:
There is also a related effect on the regression test in #10465. The test attempts to retrieve the captured dictionary using: free_vars = task.run.__code__.co_freevars
shared_retry_kwargs = task.run.__closure__[
free_vars.index("retry_kwargs")
].cell_contentsHowever, once Approach in #10461In #10461, I keep the captured configuration and the per-invocation retry state separate: call_retry_kwargs = dict(retry_kwargs)The original Only the per-invocation dictionary receives dynamically generated values: call_retry_kwargs["countdown"] = ...
call_retry_kwargs["max_retries"] = ...and that dictionary is then passed to: task.retry(exc=exc, **call_retry_kwargs)This keeps the two responsibilities separate:
I ran the same autoretry-focused test selection with this approach and got:
This includes the existing backoff/jitter, inherited configuration, and class-based autoretry tests, as well as the regression coverage for shared base-class |
|
Based on the Python scoping behavior and the results from running the same It avoids mutating the captured That's my conclusion from comparing the two implementations locally. |
|
great! would you mind checking the failing integration tests as well? |
|
I looked into the failing/canceled checks — they're not related to this diff. The root cause is that This then cascades into the larger set of canceled jobs through matrix fail-fast. I confirmed that this is broken on If it's useful, I'm happy to open a separate PR adding the missing The unit test jobs that actually exercise this diff aren't among the failures, for what it's worth. |
|
can you check the possibility of improving the test coverage? |
|
Thanks for the suggestion! I took another look through the existing autoretry tests and the regression coverage added in this PR. One area that looks like a genuine gap is the I'll work on extending the regression coverage to verify that both invocation-specific values ( |


Description
Add a regression test covering the
getattr(task, "retry_kwargs", {})code path inadd_autoretry_behaviour().The existing regression test added in #10458 verifies that
retry_kwargspassed explicitly via the task decorator is not mutated during autoretry. However, it does not cover the case whereretry_kwargsis defined as a class attribute on a shared baseTaskclass.This test verifies that:
retry_kwargsinherited from a shared baseTaskclass is not mutated during autoretry.countdown) between each other.This adds regression coverage for the
getattr(task, "retry_kwargs", {})branch without modifying the implementation.Fixes #10456
Test:
