Repository navigation
Description
Summary
Signature.clone()'s own inline comment says "need to deepcopy options so origins links etc. is not modified" — and options is indeed deep-copied — but the sibling field kwargs is NOT deep-copied when clone() is called with no override arguments (the common sig.clone() "just give me an independent copy" call). The clone ends up sharing the exact same kwargs dict object as the original.
Root cause
celery/canvas.py, Signature.clone():
def clone(self, args=None, kwargs=None, **opts):
args = args if args else ()
kwargs = kwargs if kwargs else {}
# need to deepcopy options so origins links etc. is not modified.
if args or kwargs or opts:
args, kwargs, opts = self._merge(args, kwargs, opts)
else:
args, kwargs, opts = self.args, self.kwargs, self.options # <-- no copy at all here
signature = Signature.from_dict({..., 'kwargs': kwargs, 'options': deepcopy(opts), ...}, app=self._app)In the no-override branch, kwargs = self.kwargs is passed straight through to from_dict by reference, while opts two lines later is explicitly deepcopy()'d — an inconsistency between two fields that the surrounding comment implies should both be independent.
Steps to Reproduce
from celery import Celery
app = Celery(set_as_current=False)
@app.task
def add(x, y):
return x + y
original = add.s(1, 2)
original.kwargs['extra'] = {'shared': True}
clone_a = original.clone()
clone_b = original.clone()
print(clone_a.kwargs is original.kwargs) # True -- should be False
clone_a.kwargs['x'] = 999
print(original.kwargs) # gained 'x': 999 -- corrupted by mutating a "clone"
print(clone_b.kwargs) # also gained 'x': 999 -- 3-way aliasingOutput confirms original, clone_a, and clone_b all share the identical kwargs object (same id()); mutating one mutates all three.
This propagates into canvas internals that rely on .clone() for independence when fanning a signature out into siblings, e.g. group.clone() (canvas.py) and _chain.unchain_tasks() (tasks = [t.clone() for t in self.tasks]).
Expected Behavior
sig.clone() called with no overrides should return a signature whose kwargs (and ideally args, for consistency) is independent of the original, matching the deep-copy treatment already given to options.
Suggested fix
Mirror the existing deepcopy(opts) handling onto kwargs in the no-override branch, e.g. args, kwargs, opts = self.args, deepcopy(self.kwargs), self.options, or always route through _merge().
Environment
- Celery 5.6.3 (confirmed unchanged on
mainas of 2026-09-04) - Python 3.12.10
- Windows 11
Found via a property-based audit (copy/clone independence) run across several widely-used Python libraries; repro was written and independently re-run before filing. Happy to open a PR if a maintainer confirms the fix direction.
Activity
auvipy commentedon Sep 7, 2026
@tritsystem you are welcome to come with a fix having proper test coverages
Fix Signature.clone() sharing kwargs with the original signature (cel…