Skip to content

Signature.clone() aliases .kwargs with the original when called without override arguments #10560

Closed

Description

@tritsystem

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 aliasing

Output 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 main as 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

  1. auvipy commented on Sep 7, 2026

    @auvipy
    Member

    @tritsystem you are welcome to come with a fix having proper test coverages

  2. added this to the 5.7.0 milestone on Sep 7, 2026
  3. added a commit that references this issue on Sep 7, 2026
    a8e89e5
  4. added theissue type on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions