Skip to content

Redis backend: a chord header that runs twice makes the body run early with the wrong input #10910

Closed

Description

@ChristianMurphy

Checklist

  • I have verified that the issue exists against the main branch of Celery.
  • This has already been asked to the discussions forum first.
  • I have read the relevant section in the
    contribution guide
    on reporting bugs.
  • I have checked the issues list
    for similar or identical bug reports.
  • I have checked the pull requests list
    for existing proposed fixes.
  • I have checked the commit log
    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 and added the reproduction script below.

Mandatory Debugging Information

  • I have included the output of celery -A proj report in 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 main branch of Celery.
  • I have included the contents of pip freeze in 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

Possible Duplicates

  • None

Environment & Settings

Celery version: 5.7.0rc1 (collider). It also happens on main (d6d754e), 5.6.3, and every older release I tried, back to 4.4.7.

celery report Output:

software -> celery:5.7.0rc1 (collider) kombu:5.7.0rc1 py:3.13.16
            billiard:4.3.1 redis:8.1.0
platform -> system:Linux arch:64bit, ELF
            kernel version:6.18.44-fc-v139 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://localhost:6379/1

broker_url: 'redis://:********@localhost:6379/0'
result_backend: 'redis://localhost:6379/1'
deprecated_settings: None
task_acks_late: True
broker_transport_options: 
 'visibility_timeout': 10}

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: N/A or Unknown
  • Minimal Celery Version: 4.4.7 or older (the oldest I tried)
  • Minimal Kombu Version: N/A or Unknown
  • 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.4.1
billiard==4.3.1
celery==5.7.0rc1
click==8.5.0
click-didyoumean==0.3.1
click-plugins==1.1.1.2
click-repl==0.4.1
kombu==5.7.0rc1
packaging==26.3
prompt-toolkit==3.0.53
python-dateutil==2.9.0.post0
redis==8.1.0
six==1.17.0
typing-extensions==4.16.0
tzdata==2026.4
tzlocal==5.4.4
vine==5.1.0
wcwidth==0.9.3

Other Dependencies

Details

Redis 7.0.15 on localhost:6379, as both broker and result backend.

Minimally Reproducible Test Case

tasks.py, run.py, and the commands to run them

# tasks.py
import os
import random
import signal
import time

from celery import Celery
from celery.signals import task_success

app = Celery("tasks", broker="redis://localhost:6379/0", backend="redis://localhost:6379/1")
app.conf.update(
    task_acks_late=True,
    broker_transport_options={"visibility_timeout": 10},  # redeliver after 10 s, not 1 hour
)


@app.task
def header(name, seconds):
    time.sleep(seconds)
    return f"{name}-{random.randint(1000, 9999)}"


@app.task
def body(results):
    return results


@task_success.connect
def kill_worker_once(sender=None, result=None, **kwargs):
    # Header "a" saved its result but hasn't acked yet. Kill the worker here, once.
    if sender.name == "tasks.header" and result.startswith("a-") and not os.path.exists("killed"):
        open("killed", "w").close()
        os.killpg(os.getpgid(os.getppid()), signal.SIGKILL)
# run.py
from celery import chord

from tasks import body, header

result = chord([header.s("a", 1), header.s("b", 0), header.s("c", 20)])(body.s())
print("body got:", result.get(timeout=120))
celery -A tasks worker -c 3    # terminal 1
python run.py                  # terminal 2
# The worker dies about a second later. Wait 15 seconds, then start it again:
celery -A tasks worker -c 3    # terminal 1

Expected Behavior

The body runs once all three headers are done, and gets one result from each:

body got: ['a-4930', 'b-3339', 'c-5537']

Actual Behavior

The body runs shortly after the restart, while header c is still running:

body got: ['a-5654', 'a-6300', 'b-3016']

It has two results for header a and none for header c. When header c finishes, Celery drops its result and doesn't log an error.

After the restart, Redis hands back the message for header a, and header a runs again with a new value. The Redis backend counts finished headers in a sorted set. Each one adds [1, tid, state, result], and the body runs when the count equals the number of headers. The second run of header a adds a second member, so it counts twice.

Any crash between saving a result and the ack can do this under acks_late. So can a broker that delivers a message twice. In a fault test with RabbitMQ and 3 second network drops, 2 of 60 chords got the wrong input on main, and 4 of 60 on 5.6.3. worker_deduplicate_successful_tasks helps after a crash, but not when two copies run in parallel.

I have a patch that counts each task id once, with a Lua script. I can open a PR.

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

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions