Skip to content

Chord errback runs for a chord that succeeded when a header task is redelivered (memcached backend) #11036

Done

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.
  • 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.0 (main at 768e476, kombu main at 7dca15a).

celery report Output:
software -> celery:5.7.0 (collider) kombu:5.7.0 py:3.14.6 billiard:4.3.2 redis:8.1.0
broker_url: 'redis://:********@localhost:6379/10'
result_backend: 'cache+memcached://127.0.0.1:11211/'
task_acks_late: True
worker_prefetch_multiplier: 1
broker_transport_options:
    'unacked_restore_interval': 1,
    'unacked_restore_throttle': 1,
    'visibility_timeout': 2}

Steps to Reproduce

Required Dependencies

Minimal versions
  • Minimal Result Backend Client Version: pymemcache 4.0.0
  • Others: N/A or Unknown

Python Packages

pip freeze Output:
amqp==5.4.2
billiard==4.3.2
celery @ main (768e476)
kombu @ main (7dca15a)
pymemcache==4.0.0
redis==8.1.0
vine==5.1.0

Other Dependencies

Redis 7.4 as the broker, memcached 1.6.45 for results.

Minimally Reproducible Test Case

tasks.py, run.py and output
# tasks.py
import time

from celery import Celery

app = Celery("tasks", broker="redis://localhost:6379/10", backend="cache+memcached://127.0.0.1:11211/")
app.conf.update(
    task_acks_late=True,
    worker_prefetch_multiplier=1,
    # Redeliver a task still running after 2 s (default 1 h), checking every second.
    broker_transport_options={"visibility_timeout": 2, "unacked_restore_interval": 1,
                              "unacked_restore_throttle": 1},
)


@app.task(bind=True)
def header(self, i):
    # Only the first delivery outlasts the visibility timeout.
    redelivered = (self.request.delivery_info or {}).get("redelivered")
    time.sleep(0.2 if redelivered else 4)
    return i


@app.task
def body(results):
    print(f"body ran with {results}")
    return sum(results)


@app.task
def on_chord_error(request, exc, traceback):
    print(f"errback called for {request.id}: {exc!r}")
# run.py
import time

from celery import chord

from tasks import body, header, on_chord_error

results = [chord([header.s(0), header.s(1)])(body.s().on_error(on_chord_error.s())) for _ in range(5)]
time.sleep(90)
print("states:", [r.state for r in results])
$ celery -A tasks worker -P prefork -c 2 -l info &
$ python run.py
states: ['SUCCESS', 'SUCCESS', 'SUCCESS', 'SUCCESS', 'SUCCESS']
worker log: "body ran" 5 times, "errback called" 10 times, e.g.
errback called for 1e939fde-...: ChordError('GroupResult a66bb22a-... no longer exists')

Expected Behavior

The body runs once per chord, and the errback doesn't run, because no task failed. The chord docs say errbacks run "just once if any task in the chord header or body fails".

Actual Behavior

With the memcached result backend, the errback runs once for every header task that the broker delivers again after the chord finished, although the body ran and every task succeeded. The worker also logs ERROR ... Chord callback '<group id>' raised: ValueError('<group id>') each time.

5 chords of 2 headers each body runs errback runs
memcached result backend 5 10
Redis result backend, same setup 5 0

In longer runs: memcached 20 of 20 chords, Redis backend 0 of 20, database backend 0 of 10. Without task_acks_late, prefetched headers that waited past the visibility timeout also ran twice (10 of 20 chords). When the errback ran first, a client polling the body saw FAILURE (ChordError) before SUCCESS in 3 of 4 chords.

The memcached backend counts chord parts with incr (BaseKeyValueStoreBackend.on_chord_part_return). The last part deletes the group result after it sends the body. A header that runs again later can't restore the group, so on_chord_part_return calls chord_error_from_stack with "GroupResult no longer exists", which calls the body's errbacks. DynamoDB uses the same code. I didn't test it.

Once someone confirms it, I'm happy to follow up with a PR.

Activity

  1. ChristianMurphy commented on Nov 8, 2026

    @ChristianMurphy
    ContributorAuthor

    The same redelivered header causes a second problem on these backends, before the chord finishes: it is counted twice. #10910 reported that for the Redis backend, and #10919 fixed it there only. memcached, and by the same code DynamoDB and GCS, still count it twice. When a header task runs longer than visibility_timeout, the Redis broker delivers it again and both runs call incr (celery/backends/base.py:1456; gcs.py:265 for GCS). The count hits the chord size while another header is still running. The join then waits result_chord_join_timeout and fails the chord with a TimeoutError, although no header failed. The body never runs.

    Chords of a 3 s and a 12 s header, visibility_timeout=2, acks_late, prefork Chord failed with the join timeout
    memcached 1.6 20 of 20 over two runs, and 3 of 3 with the script below
    Redis backend 0 of 10 (the body ran in 10 of 10)
    memcached, visibility_timeout=60 (no redelivery) 0 of 10 (the body ran in 10 of 10)
    memcached, 3 headers of random length (0.1 to 12 s) 6 of 30

    The short timeout stands for the default of one hour with headers of 1.5 and 6 hours; the join timeout stays at its default of 3 seconds, so with real durations the long header is even further from done. Celery main at 768e476, kombu 5.7.0.

    Counting each header task once, by task id and header index as #10919 does, would fix this here too. memcached's add on a key per header part does that in one call. Should a maintainer want it, I can send that as a PR with a test that fails on main.

    Script and output
    # tasks.py
    import time
    
    from celery import Celery
    
    app = Celery("tasks", broker="redis://localhost:6379/0",
                 backend="cache+memcached://localhost:11211/")
    app.conf.task_acks_late = True
    app.conf.worker_prefetch_multiplier = 1
    app.conf.broker_transport_options = {
        "visibility_timeout": 2,
        "unacked_restore_interval": 1,
        "unacked_restore_throttle": 1,
    }
    
    
    @app.task
    def part(seconds):
        time.sleep(seconds)
        return seconds
    
    
    @app.task
    def total(results):
        return sum(results)
    # run.py
    from collections import Counter
    
    from celery import chord
    
    from tasks import part, total
    
    outcomes = Counter()
    for _ in range(3):
        result = chord([part.s(3), part.s(12)])(total.s())
        try:
            outcomes[repr(result.get(timeout=120))] += 1
        except Exception as exc:  # the chord's error
            outcomes[repr(exc)[:90]] += 1
    print(dict(outcomes))
    $ celery -A tasks worker -P prefork -c 4 -l warning &
    $ python run.py
    {'ChordError("TimeoutError(\'Operation timed out (3.0)\')")': 3}

    The worker log for each chord:

    ERROR/ForkPoolWorker-2] Chord '...' raised: "TimeoutError('Operation timed out (3.0)')"
    
    What each run did

    In every failed chord the short header started twice, about 3 seconds apart (the restore pass after visibility_timeout), and both runs ended before the long header did. The second end made the counter equal the chord size, and the chord error came 3 seconds later, while the long header was still running. In the second memcached run and in the controls each chord had a fresh queue and worker, because the broker keeps redelivering headers that outlive the timeout and those copies would otherwise delay the next chord; the first run waited for each chord's copies to finish instead.

  2. auvipy commented on Nov 8, 2026

    @auvipy
    Member

    is it new bug or old?

  3. ChristianMurphy commented on Nov 8, 2026

    @ChristianMurphy
    ContributorAuthor

    Old. Both problems show up on every release I tried, back to 5.3.6. The errback branch dates from 2013 (for #1094), and #10919 fixed the double count for Redis only.

    Same scripts on each version, with kombu main under each so only Celery changes, memcached 1.6, prefork, acks_late, visibility_timeout=2:

    Celery Errback runs after a chord succeeded (5 chords, 10 late copies) Chord failed by a double count (3 chords)
    5.3.6 4 3 of 3
    5.5.3 4 3 of 3
    5.6.3 4, 4 3 of 3
    main (1a48583) 10, 10 3 of 3

    Main hits the errback more often. On the older releases, the other 6 late copies found the group result still there and only logged Chord counter incremented too many times. The chord code in base.py is the same. The memcached client is not: 5.7 moved to pymemcache, and the older releases run here with python-memcached. I haven't pinned down why that changes the count.

    Happy to send a PR for either part, with tests that fail on main.

  4. added this to the 5.7.x milestone on Nov 9, 2026
  5. auvipy commented on Nov 9, 2026

    @auvipy
    Member

    OK

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions