Skip to content

Chord body will not be called if result_expires is too low #4207

Description

@jheld

Checklist

  • 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).

software -> celery:4.0.2 (latentcall) kombu:4.0.2 py:2.7.12
billiard:3.5.0.2 redis:2.10.5
platform -> system:Linux arch:64bit, ELF imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://localhost:6379/

BROKER_TRANSPORT_OPTIONS: {
'fanout_patterns': True, 'fanout_prefix': True}
CELERY_TASK_COMPRESSION: 'gzip'
CELERY_RESULT_SERIALIZER: 'json'
CELERY_BROKER_URL: u'redis://localhost:6379//'
DEBUG_PROPAGATE_EXCEPTIONS: False
CELERY_ACCEPT_CONTENT: ['application/json']
CELERY_TIMEZONE: 'UTC'
CELERY_MESSAGE_COMPRESSION: 'gzip'
USE_TZ: True
CELERY_RESULT_BACKEND: u'redis://localhost:6379/'
CELERY_FLOWER_URL: 'http://localhost:5556'

  • I have verified that the issue exists against the master branch of Celery.
    I do not have the time resource to test different versions of celery while at work. This project is at work and I have other things to do while worrying about callbacks not being queued/called. If I do get time to test on master, I will.

Steps to reproduce

Run about 100 task header of a chord, with a callback. The chord is started through .delay().
Usage is similar to what the docs have:
chord((import_contact.s(c) for c in contacts), notify_complete.si(import_id)).apply_async(). In my case, I do not use an immutable signature, and i pass many kwargs into the callback/body.

Expected behavior

I expect that once my header has completed, that the callback will run.

Actual behavior

Depending on how long the header tasks run for, the callback is not called. I'm still trying to figure out if it is a time or memory or other sort of issue. But, I wanted to get the issue created while I'm looking through it further.

It's also possible that it doesn't have to do with how long it takes at all. I'll try updating further.

Activity

  1. changed the title [-]Chord callback not always called when header tasks complete.[/-] [+]Chord callback/body not always called when header tasks complete.[/+] on Aug 17, 2017
  2. auvipy commented on Dec 19, 2017

    @auvipy
    Member

    If you could verify this again on top of latest master plz ping me to reopen

  3. jheld commented on Nov 20, 2018

    @jheld
    ContributorAuthor

    My new leading hypothesis is that this is related to my project's use of result_expires. We used to have this set at 60 seconds, and now we have it at 10 seconds.

    Tonight I decided to revisit this (we're using celery 4.2.0). There are a few workflows that would benefit us from having this work all of the time; we've been hesitant to utilize the chord because we were having issues with it running the body all the time.

    Now, if we have, say, 1 task, and we have a header of 2 of them being called (say add each), and then let the body also be an add, if we lower the result expire to 1 second, and also include a time.sleep(2) in the add implementation, I've never seen the body task run.

    Playing with the result expires and the "task time" (fake long task with a time.sleep) seems like the culprit.

    There is documentation around result expires, however, if this is the issue for the redis backend, then the docs should warn against setting a low task expire results time, in the case of a chord.

    At this point, I'm fairly confident that this is the bug.
    Below is the section of code which introduces the notion of the adjusted completed task count.
    When I add more logs around the readycount vs total (chord_size), and when I see the issue rear its head, its because it never reaches the total. You'll note the self.expires

                _, readycount, totaldiff, _, _ = pipe \
                    .rpush(jkey, self.encode([1, tid, state, result])) \
                    .llen(jkey) \
                    .get(tkey) \
                    .expire(jkey, self.expires) \
                    .expire(tkey, self.expires) \
                    .execute()
    

    And self.expires gets its value from:

            if value is None:
                value = self.app.conf.result_expires
            if isinstance(value, timedelta):
                value = value.total_seconds()
            if value is not None and type:
                return type(value)
            return value
    

    So, I wonder, should the tkey and jkey be honoring the result expires (or at least, the one set in the config, as opposed to the day default)? Is there any harm in having it be no less than 1 day, or perhaps, making it its own setting?

    Or, is this really just a documentation update to help users?

  4. jheld commented on Nov 20, 2018

    @jheld
    ContributorAuthor

    @auvipy please see above.

  5. jheld commented on Nov 20, 2018

    @jheld
    ContributorAuthor

    Would it be possible to have a per-task (possibly even on chord, too) result expires setting? My team would be very interested in that usage.

    We don't care about most of the results of our task runs (in practice), but we don't necessarily wish to always use ignore_result, especially if we need the task result around at least briefly but we forget about the global setting.

  6. reopened this on Nov 21, 2018
  7. changed the title [-]Chord callback/body not always called when header tasks complete.[/-] [+]Chord body will not be called if result expires is too low[/+] on Nov 21, 2018
  8. changed the title [-]Chord body will not be called if result expires is too low[/-] [+]Chord body will not be called if result_expires is too low[/+] on Nov 21, 2018
  9. added this to the 4.5 milestone on Jun 26, 2019
  10. modified the milestones: 4.5, 5.3 on Feb 17, 2021
  11. auvipy commented on Feb 17, 2021

    @auvipy
    Member

    Would it be possible to have a per-task (possibly even on chord, too) result expires setting? My team would be very interested in that usage.

    We don't care about most of the results of our task runs (in practice), but we don't necessarily wish to always use ignore_result, especially if we need the task result around at least briefly but we forget about the global setting.

    when you have time can you revisit the current codes & share your current consensus on that? we can dig further based on your finding. I know it so old & sorry for taking so long to reply.

  12. removed this from the 5.3 milestone on Feb 19, 2023
  13. ChristianMurphy commented on Oct 6, 2026

    @ChristianMurphy
    Contributor

    This still reproduces on 5.6.3 with the Redis backend. A chord of three tasks that each sleep 3 seconds, on a single worker with result_expires=1, never runs its body. With result_expires=60 it runs.

    The cause looks like on_chord_part_return: each finished header task resets the chord's .j, .t and .s keys to expire after result_expires. If the gap between two header tasks finishing is longer than that, the keys are gone and the body never fires. main has the same code.

    One narrow fix would be to keep those keys until the body is sent. They're already deleted at that point, so no expiry is needed while the chord is open. Another option is a separate expiry for chord keys. Would either be welcome? Happy to send a PR with tests.

  14. auvipy commented on Oct 8, 2026

    @auvipy
    Member

    can you reproduce with v5.7.0b1?

  15. ChristianMurphy commented on Oct 8, 2026

    @ChristianMurphy
    Contributor

    Yes, it still reproduces on 5.7.0b1 with the same chord setup. I can send a PR.

  16. ChristianMurphy commented on Oct 9, 2026

    @ChristianMurphy
    Contributor

    The same chord also fails on the memcached backend with 5.7.0b1, through a different path. The group record saved at chord start is never refreshed. Once it expires, each header task fails with ValueError(<group id>) in on_chord_part_return, and the body never runs. With result_expires=60 the body runs, and with result_expires=1 it doesn't. #10827 only covers Redis. I can look at memcached separately if that's useful.

  17. added this to the 5.7.0 milestone on Oct 9, 2026
  18. auvipy commented on Oct 9, 2026

    @auvipy
    Member

    sure thing.

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