Repository navigation
Description
Checklist
- I have verified that the issue exists against the
mainbranch of Celery. - This has already been asked to the [discussions forum](https://github.com/celery/celery/discussions) first.
- I have read the relevant section in the
[contribution guide](https://docs.celeryq.dev/en/main/contributing.html#other-bugs)
on reporting bugs. - I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
for similar or identical bug reports. - I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
for existing proposed fixes. - I have checked the [commit log](https://github.com/celery/celery/commits/main)
to find out if the bug was already fixed in the main branch. - I have included all related issues and possible duplicate issues
in this issue (If there are none, check this box anyway). - I have tried to reproduce the issue with [pytest-celery](https://docs.celeryq.dev/projects/pytest-celery/en/latest/userguide/celery-bug-report.html) and added the reproduction script below.
Mandatory Debugging Information
- I have included the output of
celery -A proj reportin the issue.
(if you are not able to do this, then at least specify the Celery
version affected). - I have verified that the issue exists against the
mainbranch of Celery. - I have included the contents of
pip freezein the issue. - I have included all the versions of all the external dependencies required
to reproduce this bug.
Optional Debugging Information
- I have tried reproducing the issue on more than one Python version
and/or implementation. - I have tried reproducing the issue on more than one message broker and/or
result backend. - I have tried reproducing the issue on more than one version of the message
broker and/or result backend. - I have tried reproducing the issue on more than one operating system.
- I have tried reproducing the issue on more than one workers pool.
- I have tried reproducing the issue with autoscaling, retries,
ETA/Countdown & rate limits disabled. - I have tried reproducing the issue after downgrading
and/or upgrading Celery and its dependencies.
Related Issues and Possible Duplicates
Related Issues
- None found
Possible Duplicates
- None found
Environment & Settings
Celery version: 5.6.2
celery report Output:
software -> celery:5.6.2 (immunity) kombu:5.6.2 py:3.12.3
billiard:4.2.4
platform -> system:Linux arch:64bit
result_backend: disabled
Steps to Reproduce
Required Dependencies
- Minimal Python Version: N/A or Unknown
- Minimal Celery Version: 5.6.2 (bug is present in
mainas well; the affected code has not changed since at least the 5.6.x line) - Minimal Kombu Version: N/A — not broker-related, bug is purely in
celery.app.autoretry - Minimal Broker Version: N/A or Unknown
- Minimal Result Backend Version: N/A or Unknown
- Minimal OS and/or Kernel Version: N/A or Unknown
- Minimal Broker Client Version: N/A or Unknown
- Minimal Result Backend Client Version: N/A or Unknown
Python Packages
pip freeze Output:
amqp==5.3.1
billiard==4.2.4
celery==5.6.2
click==8.3.1
click-didyoumean==0.3.1
click-plugins==1.1.1.2
click-repl==0.3.0
kombu==5.6.2
vine==5.1.0
Other Dependencies
Details
No broker or result backend is needed to reproduce this — the bug is entirely
in-process, in celery.app.autoretry.add_autoretry_behaviour, and can be
demonstrated by calling a task's run() directly.
Minimally Reproducible Test Case
Details
"""
add_autoretry_behaviour() builds the `retry_kwargs` dict ONCE, when a task
class is registered (celery/app/base.py calls it exactly once per task
class, never per invocation). That single dict is captured by the
generated run() closure and reused for the entire lifetime of the worker
process -- but run() mutates it in place on every retry:
retry_kwargs['countdown'] = get_exponential_backoff_interval(...)
...
ret = task.retry(exc=exc, **retry_kwargs)
This script proves the shared dict gets permanently mutated by a task
execution, which is not supposed to happen, and which is unsafe under any
concurrent execution of the same task (eventlet/gevent pools, or any other
cooperative/threaded execution of the same task within one process).
"""
from unittest.mock import patch
from celery import Celery
from celery.exceptions import Retry
app = Celery(set_as_current=False)
@app.task(
bind=True,
shared=False,
autoretry_for=(ZeroDivisionError,),
retry_backoff=True,
retry_jitter=False,
max_retries=3,
)
def task(self_, x, y):
return x / y
# Locate the `retry_kwargs` dict captured by the run() closure at
# registration time -- this is the exact object that is shared across
# every future execution of this task in the same worker process.
free_vars = task.run.__code__.co_freevars
shared_retry_kwargs = task.run.__closure__[
free_vars.index("retry_kwargs")
].cell_contents
print("shared retry_kwargs BEFORE any retries:", shared_retry_kwargs)
assert shared_retry_kwargs == {}
with patch.object(task, "retry", side_effect=Retry):
for retries in range(3):
task.push_request(retries=retries)
try:
task.run(1, 0)
except Retry:
pass
finally:
task.pop_request()
print("shared retry_kwargs AFTER 3 retries: ", shared_retry_kwargs)
assert shared_retry_kwargs == {}, (
"BUG: shared retry_kwargs dict was mutated!"
)Expected Behavior
The retry_kwargs dict captured by the run() closure at task-registration
time should never be mutated.
Running the script above should print:
shared retry_kwargs BEFORE any retries: {}
shared retry_kwargs AFTER 3 retries: {}
and complete without raising AssertionError.
More importantly, two concurrent executions of the same task (e.g. under an
eventlet/gevent pool) should each retry using their own backoff
countdown, computed from their own task.request.retries, and should never
be able to see or use a countdown/max_retries value written by a
different, concurrently-running execution of the same task.
Actual Behavior
Running the script above prints:
shared retry_kwargs BEFORE any retries: {}
shared retry_kwargs AFTER 3 retries: {'countdown': 4}
Traceback (most recent call last):
...
AssertionError: BUG: shared retry_kwargs dict was mutated!
The single retry_kwargs dict shared by every execution of the task
permanently picks up a 'countdown' key (and, if override_max_retries is
used, a 'max_retries' key) from whichever execution most recently retried.
Because it is the same dict object across all executions of the task for
the whole lifetime of the process, this is unsafe under concurrent execution
of the same task (eventlet/gevent pools, or any other cooperative/threaded
execution of the same task in one process): one execution's write of
countdown/max_retries can land in the window between another execution's
write and its use of that dict in task.retry(**retry_kwargs), so that other
execution ends up retrying with the wrong backoff delay (or the wrong
max_retries).
This silently defeats retry_backoff/retry_jitter, whose purpose is
specifically to prevent a thundering herd of retries against a struggling
downstream service.
It also means that if a user sets:
retry_kwargs = {"max_retries": 5}as a task class attribute, this code mutates that exact class-attribute
dict in place, permanently injecting a 'countdown' key that was never
there.
Affected file: celery/app/autoretry.py (the except autoretry_for as exc: branch inside the generated run() closure in
add_autoretry_behaviour).
Root cause: retry_kwargs is built once per task class at
registration time (celery/app/base.py, Celery.task() and
celery/app/registry.py, register_task() each call
add_autoretry_behaviour(task, **options) exactly once) and then mutated
in place on every retry instead of being copied per call.
A possible fix seems to be working on a per-invocation copy of the dictionary inside the except block instead of mutating the closed-over retry_kwargs.
If this behavior is confirmed as a bug and that approach looks appropriate, I'd be happy to work on the fix and add a regression test.
Activity
test: add regression test for shared retry_kwargs dict mutation (cele…
DresdenGman commentedon Aug 10, 2026
"This is a clear per-invocation state bug: the closed-over retry_kwargs dict is mutated by each retry, so concurrent executions can leak countdown/max_retries into one another. I can prepare a minimal fix that copies the kwargs inside the retry path and a regression test covering both sequential mutation and concurrent isolation. If this scope matches the maintainers' expectations, I'd be happy to submit a focused PR."