Skip to content

fix: copy retry_kwargs before mutating inside autoretry handler to prevent shared-dict corruption under concurrent execution - #10465

Closed
waterWang wants to merge 2 commits into
celery:mainfrom
waterWang:fix/autoretry-shared-dict-copy
Closed

waterWang wants to merge 2 commits into
celery:mainfrom
waterWang:fix/autoretry-shared-dict-copy

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Description

Fix a bug where autoretry_for + retry_backoff mutate a single retry_kwargs dict shared across all executions of a task.

Root Cause

In celery/app/autoretry.py, add_autoretry_behaviour() builds the retry_kwargs dict once per task class at registration time. The generated run() closure captures this single dict and mutates it in place on every retry:

retry_kwargs['countdown'] = get_exponential_backoff_interval(...)

Under concurrent execution (eventlet/gevent pools), 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.

Fix

Copy the retry_kwargs dict inside the except block before mutating it, so each execution uses its own copy.

Test

Added test_autoretry_shared_retry_kwargs_not_mutated regression test that:

  1. Creates a task with autoretry_for + retry_backoff
  2. Inspects the closure-captured retry_kwargs dict via __closure__
  3. Verifies it is empty before any retries
  4. Retries 3 times via task.run() with Retry exception
  5. Asserts the shared dict is still empty (was mutated to {'countdown': 4} before the fix)

Closes #10456

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

closing in favor of another pr

@auvipy auvipy closed this Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

autoretry_for + retry_backoff mutate a single retry_kwargs dict shared across all executions of a task, corrupting backoff under concurrent execution

2 participants