Repository navigation
Fix Signature.clone() kwargs aliasing - #10561
SIBTAIN-ASAD wants to merge 14 commits into
Conversation
❌ 10 Tests Failed:
View the top 3 failed test(s) by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
|
can you please check the test failures and fix them? |
There was a problem hiding this comment.
🟡 Changes recommended
clone() can still alias self.kwargs when called with args/opts overrides but no kwargs (via _merge() returning self.kwargs), leaving the same mutation hazard in other common clone call patterns.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes Signature.clone() so a no-override clone does not share the original .kwargs mapping, preventing mutations on a clone from affecting the source signature (and sibling clones). This aligns kwargs handling with the existing defensive deepcopy() already applied to options.
Changes:
- Deep-copy
self.kwargsin theclone()no-overrides branch. - Add a regression test asserting both top-level
kwargsand a nested value are independent.
File summaries
| File | Description |
|---|---|
celery/canvas.py |
Deep-copies kwargs in Signature.clone() when called with no overrides to avoid aliasing. |
t/unit/tasks/test_canvas.py |
Adds regression coverage for clone() kwargs independence, including nested structures. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| @@ -458,7 +458,7 @@ def clone(self, args=None, kwargs=None, **opts): | |||
| if args or kwargs or opts: | |||
| args, kwargs, opts = self._merge(args, kwargs, opts) | |||
| else: | |||
| args, kwargs, opts = self.args, self.kwargs, self.options | |||
| args, kwargs, opts = self.args, deepcopy(self.kwargs), self.options | |||
|
Thanks — I checked the Codecov failures. The reported failures are the existing current_app Trap failures in unrelated canvas tests; the focused clone coverage passes locally. I also addressed the remaining aliasing path when clone receives args or option overrides without kwargs by deep-copying the merged kwargs, and added regression coverage for that case. Pushed as 513a7d6; the checks are rerunning now. |
There was a problem hiding this comment.
🟡 Changes recommended
Deep-copying generator-backed group kwargs eagerly exhausts their lazy task iterator.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| kwargs = deepcopy(kwargs) | ||
| else: | ||
| args, kwargs, opts = self.args, self.kwargs, self.options | ||
| args, kwargs, opts = self.args, deepcopy(self.kwargs), self.options |
|
I found the lazy-generator regression and fixed it in d90ac6e. clone() now preserves lazy _regen inputs while still deep-copying ordinary nested kwargs, so group(generator).clone() does not advance the generator. I added a regression test for that behavior; the focused clone tests now pass (3 passed, 213 deselected), and compileall plus git diff --check are clean. |
|
@Namraa310806 @phanky1 would you mind review this? |
There was a problem hiding this comment.
🟡 Changes recommended
Deep-copying nested signatures converts them to dictionaries, breaking cloned map, starmap, and chunks canvases.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| for value in kwargs.values() | ||
| if isinstance(value, _regen) | ||
| } | ||
| return deepcopy(kwargs, memo) |
|
also the unit tests are failing |
|
I found the nested-signature issue as well and fixed it in 55d5912. Clone kwargs now preserve lazy _regen inputs and explicitly clone nested Signature values, so map, starmap, and chunks keep their task signatures instead of turning them into plain dicts. I added regression coverage for all three canvas types. Focused clone tests pass (6 passed, 213 deselected), and the scoped pre-commit hooks, compileall, and diff checks are clean. |
|
The latest run confirms the remaining pre-commit failure is still unrelated: flake8 reports E301 at t/unit/app/test_schedules.py:161, outside this PR. The scoped hooks pass locally, the six clone regressions pass, and Read the Docs is green. The unit failures are the existing current_app Trap failures in unrelated canvas tests; no failure points to the clone changes. |
There was a problem hiding this comment.
🟡 Changes recommended
_deepcopy_kwargs()’s pre-walk currently has no cycle detection and can recurse infinitely on self-referential containers in kwargs, causing a crash before deepcopy() can handle cycles.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
| memo = {} | ||
|
|
||
| def collect(value): | ||
| if isinstance(value, _regen): | ||
| memo[id(value)] = value | ||
| elif isinstance(value, Signature): | ||
| memo[id(value)] = value.clone() | ||
| elif isinstance(value, dict): | ||
| for nested_value in value.values(): | ||
| collect(nested_value) | ||
| elif isinstance(value, (list, tuple, set, frozenset)): | ||
| for nested_value in value: | ||
| collect(nested_value) | ||
|
|
|
I also addressed the cycle-safety review in 80c1f3e. _deepcopy_kwargs now tracks visited containers, so self-referential kwargs are copied without recursion errors while lazy generators and nested signatures remain preserved. I added regression coverage; the focused clone tests pass (7 passed, 213 deselected), and the scoped pre-commit hooks, compileall, and diff checks are clean. |
|
The new 80c1f3e workflows are currently marked action_required with no jobs, so they appear to be waiting for maintainer approval of fork-originated execution. Once approved, I will recheck the full matrix; the local scoped suite and hooks are already green. |
There was a problem hiding this comment.
🟡 Changes recommended
Generator-backed children, signature-containing cycles, and signatures inside unsupported containers remain incorrectly cloned.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (2)
celery/canvas.py:82
- Could we preserve child-signature independence while keeping this lazy? Memoizing the
_regenobject itself avoids eager consumption, butgroup.from_dict()only wraps that same iterator withmaybe_signature(..., clone=False). Consequently, a generator-backed group's clone yields the exact same childSignatureobjects as the source (and sibling clones), so mutating a child through the clone still mutates the original. A lazy wrapper that clones/deep-copies each item as it is yielded would retain laziness without reintroducing aliasing; the regression test should also assert that the yielded child is distinct.
if isinstance(value, _regen):
memo[id(value)] = value
celery/canvas.py:98
- Could nested
Signaturehandling avoid depending on this fixed container whitelist? For example, withkwargs={'items': collections.deque([child_sig])},collect()never reacheschild_sig; the subsequentdeepcopy()invokesSignature.__deepcopy__, which returns a plaindict, so cloning silently changes the task argument's type and behavior. The same occurs for signatures stored in custom containers/object attributes; handlingSignaturethrough the deepcopy protocol rather than a pre-scan would cover those valid object graphs.
elif isinstance(value, (list, tuple, set, frozenset)):
value_id = id(value)
if value_id in seen:
return
seen.add(value_id)
for nested_value in value:
collect(nested_value)
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| elif isinstance(value, Signature): | ||
| memo[id(value)] = value.clone() |
|
I addressed the remaining cloning edge cases in 15cb4cc. Lazy group clones now yield independent child signatures without consuming the source iterator; nested Signatures are preserved through arbitrary containers and signature cycles; and the pre-walk remains cycle-safe. Added regression coverage for all three cases. Focused canvas coverage passes (9 passed, 213 deselected), plus scoped pre-commit, compileall, and diff checks. |
|
can you also add smoke test for this? and possibly integration test? |
There was a problem hiding this comment.
🟡 Changes recommended
Nested signatures can still lose nested values or required subclass state during cloning.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Balanced
| clone = type(value).__new__(type(value)) | ||
| memo[value_id] = clone | ||
| dict.__init__(clone) | ||
| collect(value) | ||
| for key, nested_value in value.items(): | ||
| dict.__setitem__(clone, deepcopy(key, memo), deepcopy(nested_value, memo)) | ||
| clone._app = value._app | ||
| if '_type' in value.__dict__: | ||
| clone._type = value._type |
There was a problem hiding this comment.
@SIBTAIN-ASAD please fix the test failures along with this suggestions. or you can leave this pr for more experienced contributors
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
|
@phanky1 may be you can retake this if possible |
|
closing in favor of #10571 |
Signature.clone()is expected to return an independent signature when called without overrides. The no-override branch currently reuses the originalkwargsdictionary, so mutating a clone can mutate the source and sibling clones.This change deep-copies
kwargsin that branch, matching the existing defensive copy ofoptions. The regression test covers both the mapping and a nested value.Refs #10560
Tests:
./.venv/bin/python -m pytest -q t/unit/tasks/test_canvas.py -k test_clone_copies_kwargs./.venv/bin/python -m compileall -q celery/canvas.py t/unit/tasks/test_canvas.py