Skip to content

KV-store chords restore the whole group on every header task (O(N²)) #11045

Open

Description

@patrick-immometrica

For backends that count chords with incr (memcached, DynamoDB, ...), BaseKeyValueStoreBackend.on_chord_part_return calls GroupResult.restore(gid) on every header task before incrementing:

https://github.com/celery/celery/blob/1a48583d4/celery/backends/base.py#L1438-L1462

With the default result_cache_max=-1 nothing is cached, so each part fetches and decodes the whole group (N results). That is O(N²) for a chord of N tasks, e.g. ~10k reads of the full group blob for a 10k header.

The code expects request.chord["chord_size"] to avoid using len(deps), but nothing sets it since #6576 removed body.chord_size = ... from canvas. GCS (gcs.py) relies on the same key.

Fix idea: set chord_size on the body in _chord.run (header leaf count, as _descend already computes), then in on_chord_part_return incr first and only restore the group when the counter reaches the size.

Found by code reading (AI-assisted), not measured yet. Happy to send a PR with tests if this is confirmed.

Activity

  1. ChristianMurphy commented on Nov 16, 2026

    @ChristianMurphy
    Contributor

    This reproduces on main (9c2f238) and on 5.6.2 with the cache+memory:// backend: GroupResult.restore() runs once per header task.

    Header tasks in the chord GroupResult.restore() calls
    5 5
    10 10
    20 20
    40 40

    Each call loads the whole group, so the work grows with the square of the header size. By blame, chord_size stopped being set on the body in #6576, first released in 5.1.0. I did not run a release before that.

  2. binnguyenx commented on Nov 27, 2026

    @binnguyenx

    I tested the fix from the description on main (22be744): _chord.run sets body['chord_size'] = _descend(header) (the same count Redis uses), and on_chord_part_return calls incr first and restores the group only when the counter reaches that size. With cache+memory:// and a real worker, GroupResult.restore() now runs once per chord instead of once per header task (5/10/20/40 → 1). Headers built from a generator keep the old fallback, so they stay lazy (#3021).

    While testing I found a related bug on main: chord([chain(a.s(), group(b.si(), c.si())), d.s()], body) hangs with this backend. len(deps) is 2 but 3 parts report, so the chord never completes; with the size from _descend it completes.

    I have a branch with unit tests. @auvipy, could you confirm this is wanted? If so, and the author hasn't already started, I'll open a PR.

  3. auvipy commented on Nov 28, 2026

    @auvipy
    Member

    @binnguyenx ok come with a proper fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions