Skip to content

5.1.0b1 chord of chains dont work #6721

Closed

Description

@Sobes76rus

Checklist

  • I have verified that the issue exists against the master branch of Celery.
  • This has already been asked to the discussion group 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 master branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).

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 master 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

  • None

Possible Duplicates

  • None

Environment & Settings

Celery version: 5.1.0b1 (singularity)

celery report Output:

software -> celery:5.1.0b1 (singularity) kombu:5.1.0b1 py:3.8.5
            billiard:3.6.4.0 py-amqp:5.0.6
platform -> system:Linux arch:64bit, ELF
            kernel version:5.8.0-48-generic imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:amqp results:redis://localhost/

broker_url: 'amqp://guest:********@localhost:5672//'
result_backend: 'redis://localhost/'
deprecated_settings: None

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: N/A or Unknown
  • Minimal Celery Version: N/A or Unknown
  • 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:

Other Dependencies

Details

N/A

Minimally Reproducible Test Case

Details

import celery

app = celery.Celery(
    broker='amqp://localhost',
    backend='redis://localhost',
)


@app.task
def task_a(number):
    print(f'Task A: {number}')
    return number


@app.task
def task_b(number):
    print(f'Task B: {number}')
    return number


@app.task
def task_c(number):
    print(f'Task C: {number}')


if __name__ == '__main__':
    celery.chord(
      (celery.chain(task_a.s(i), task_b.s()))
      for i in range(3)
    )(task_c.s())  # yapf: disable

    app.start()

Expected Behavior

Expected output:

[2021-04-11 16:44:51,364: WARNING/ForkPoolWorker-15] Task A: 0
[2021-04-11 16:44:51,364: WARNING/ForkPoolWorker-16] Task A: 1
[2021-04-11 16:44:51,364: WARNING/ForkPoolWorker-1] Task A: 2
[2021-04-11 16:44:51,387: WARNING/ForkPoolWorker-4] Task B: 2
[2021-04-11 16:44:51,387: WARNING/ForkPoolWorker-3] Task B: 0
[2021-04-11 16:44:51,387: WARNING/ForkPoolWorker-2] Task B: 1
[2021-04-11 16:44:51,413: WARNING/ForkPoolWorker-15] Task C: [0, 1, 2]

On celery==5.0.5 it works fine

Actual Behavior

[2021-04-11 16:33:24,583: WARNING/ForkPoolWorker-15] Task A: 0
[2021-04-11 16:33:24,583: WARNING/ForkPoolWorker-1] Task A: 2
[2021-04-11 16:33:24,583: WARNING/ForkPoolWorker-16] Task A: 1
[2021-04-11 16:33:24,608: WARNING/ForkPoolWorker-4] Task B: 2
[2021-04-11 16:33:24,608: WARNING/ForkPoolWorker-2] Task B: 0
[2021-04-11 16:33:24,608: WARNING/ForkPoolWorker-3] Task B: 1

Activity

  1. open-collective-bot commented on Apr 11, 2021

    @open-collective-bot

    Hey @Sobes76rus 👋,
    Thank you for opening an issue. We will get back to you as soon as we can.
    Also, check out our Open Collective and consider backing us - every little helps!

    We also offer priority support for our sponsors.
    If you require immediate assistance please consider sponsoring us.

  2. thedrow commented on Apr 12, 2021

    @thedrow
    Contributor

    @maybe-sybr If you can't get to it, I will.
    I marked it as a blocker for 5.1.0 so we have to do it.

  3. maybe-sybr commented on Apr 12, 2021

    @maybe-sybr
    Contributor

    Looks like a bug in chord size counting again. For some reason we've counted 8 chord parts to return which is way off what it should be:

    [2021-04-13 09:52:54,712: INFO/MainProcess] Connected to amqp://guest:**@127.13.37.1:5672//
    [2021-04-13 09:52:54,717: INFO/MainProcess] mingle: searching for neighbors
    [2021-04-13 09:52:55,737: INFO/MainProcess] mingle: all alone
    [2021-04-13 09:52:55,752: INFO/MainProcess] celery@karma ready.
    [2021-04-13 09:52:55,752: INFO/MainProcess] Received task: __main__.task_a[8c3bd499-cf53-4086-8c9d-73cc6bd981f4]
    [2021-04-13 09:52:55,753: INFO/MainProcess] Received task: __main__.task_a[664d91de-4430-49ff-8965-476c2b4c80e2]
    [2021-04-13 09:52:55,753: INFO/MainProcess] Received task: __main__.task_a[86ae66bb-4f43-46a8-8851-b62674cc3ad7]
    [2021-04-13 09:52:55,855: WARNING/ForkPoolWorker-7] Task A: 0
    [2021-04-13 09:52:55,855: WARNING/ForkPoolWorker-8] Task A: 1
    [2021-04-13 09:52:55,856: WARNING/ForkPoolWorker-1] Task A: 2
    [2021-04-13 09:52:55,868: INFO/MainProcess] Received task: __main__.task_b[d3161d3c-ab8f-4610-83ff-24fd3d12f538]
    [2021-04-13 09:52:55,868: INFO/ForkPoolWorker-8] Task __main__.task_a[664d91de-4430-49ff-8965-476c2b4c80e2] succeeded in 0.013156214000446198s: 1
    [2021-04-13 09:52:55,868: INFO/MainProcess] Received task: __main__.task_b[44dec7e8-152e-4e11-bd70-ce4e7cb8c84b]
    [2021-04-13 09:52:55,868: INFO/ForkPoolWorker-7] Task __main__.task_a[8c3bd499-cf53-4086-8c9d-73cc6bd981f4] succeeded in 0.013253123000140477s: 0
    [2021-04-13 09:52:55,868: INFO/ForkPoolWorker-1] Task __main__.task_a[86ae66bb-4f43-46a8-8851-b62674cc3ad7] succeeded in 0.01293888700001844s: 2
    [2021-04-13 09:52:55,868: INFO/MainProcess] Received task: __main__.task_b[6da81e65-af87-4770-92fd-4ce3ce9e5937]
    [2021-04-13 09:52:55,869: WARNING/ForkPoolWorker-2] Task B: 0
    [2021-04-13 09:52:55,869: WARNING/ForkPoolWorker-3] Task B: 1
    [2021-04-13 09:52:55,869: WARNING/ForkPoolWorker-4] Task B: 2
    [2021-04-13 09:52:55,871: WARNING/ForkPoolWorker-2] OCPR: chord_size_bytes=b'8' ready=1
    [2021-04-13 09:52:55,871: WARNING/ForkPoolWorker-4] OCPR: chord_size_bytes=b'8' ready=2
    [2021-04-13 09:52:55,871: WARNING/ForkPoolWorker-3] OCPR: chord_size_bytes=b'8' ready=3
    [2021-04-13 09:52:55,871: INFO/ForkPoolWorker-2] Task __main__.task_b[d3161d3c-ab8f-4610-83ff-24fd3d12f538] succeeded in 0.001984218999496079s: 0
    [2021-04-13 09:52:55,871: INFO/ForkPoolWorker-4] Task __main__.task_b[6da81e65-af87-4770-92fd-4ce3ce9e5937] succeeded in 0.0020933550003974233s: 2
    [2021-04-13 09:52:55,871: INFO/ForkPoolWorker-3] Task __main__.task_b[44dec7e8-152e-4e11-bd70-ce4e7cb8c84b] succeeded in 0.002132425999661791s: 1
    
  4. maybe-sybr commented on Apr 13, 2021

    @maybe-sybr
    Contributor

    Counting of chord size children of groups is busted.

    celery/celery/canvas.py

    Lines 1178 to 1185 in 1901ea8

    if _chord is not None and next_task is None:
    chord_size = task_index + 1
    if isinstance(sig, _chain):
    if sig.tasks[-1].subtask_type == 'chord':
    chord_size = sig.tasks[-1].__length_hint__()
    else:
    chord_size = task_index + len(sig.tasks[-1])
    app.backend.set_chord_size(group_id, chord_size)

    The else case here is counting the length of a simple signature which is the length of its top level attributes (args, kwargs, and friends). Running the MRTC with the following diff on celery master:

    diff --git a/celery/backends/redis.py b/celery/backends/redis.py
    index 74a2e18b5..c642fe88e 100644
    --- a/celery/backends/redis.py
    +++ b/celery/backends/redis.py
    @@ -415,6 +415,7 @@ class RedisBackend(BaseKeyValueStoreBackend, AsyncBackendMixin):
             return retval
     
         def set_chord_size(self, group_id, chord_size):
    +        raise Exception(chord_size)
             self.set(self.get_key_for_group(group_id, '.s'), chord_size)
     
         def apply_chord(self, header_result_args, body, **kwargs):
    @@ -467,6 +468,7 @@ class RedisBackend(BaseKeyValueStoreBackend, AsyncBackendMixin):
                 _, readycount, totaldiff, chord_size_bytes = pipeline.execute()[:4]
     
             totaldiff = int(totaldiff or 0)
    +        print(f"OCPR: chord_size_bytes={chord_size_bytes} ready={readycount}")
     
             if chord_size_bytes:
                 try:
    diff --git a/celery/canvas.py b/celery/canvas.py
    index 57b0aea06..8aa533f43 100644
    --- a/celery/canvas.py
    +++ b/celery/canvas.py
    @@ -1182,6 +1182,9 @@ class group(Signature):
                                 chord_size = sig.tasks[-1].__length_hint__()
                             else:
                                 chord_size = task_index + len(sig.tasks[-1])
    +                            print(f"subtask is not chord -> {chord_size=}")
    +                            print(f"{len(sig.tasks[-1])=}")
    +                            print(f"{list(sig.tasks[-1])=}")
                         app.backend.set_chord_size(group_id, chord_size)
                     sig.apply_async(producer=producer, add_to_parent=False,
                                     chord=_chord, args=args, kwargs=kwargs,

    ->

    subtask is not chord -> chord_size=8
    len(sig.tasks[-1])=6
    list(sig.tasks[-1])=['task', 'args', 'kwargs', 'options', 'subtask_type', 'immutable']
    Traceback (most recent call last):
      File "/home/maybe/dev/foss/github/celery/scratch/6721/app.py", line 27, in <module>
        celery.chord(
      File "/home/maybe/dev/foss/github/celery/celery/canvas.py", line 1354, in __call__
        return self.apply_async((), {'body': body} if body else {}, **options)
      File "/home/maybe/dev/foss/github/celery/celery/canvas.py", line 1407, in apply_async
        return self.run(tasks, body, args, task_id=task_id, **merged_options)
      File "/home/maybe/dev/foss/github/celery/celery/canvas.py", line 1474, in run
        header_result = header(*partial_args, task_id=group_id, **options)
      File "/home/maybe/dev/foss/github/celery/celery/canvas.py", line 1064, in __call__
        return self.apply_async(partial_args, **options)
      File "/home/maybe/dev/foss/github/celery/celery/canvas.py", line 1089, in apply_async
        results = list(self._apply_tasks(tasks, producer, app, p,
      File "/home/maybe/dev/foss/github/celery/celery/canvas.py", line 1188, in _apply_tasks
        app.backend.set_chord_size(group_id, chord_size)
      File "/home/maybe/dev/foss/github/celery/celery/backends/redis.py", line 418, in set_chord_size
        raise Exception(chord_size)
    Exception: 8
    

    It looks like the else case on L1183 is assuming that the trailing task of the signature being checked (which is a chain since we passed the condition on L1180) is also a chain. In the MRTC the trailing task is a simple signature!

    Couple of takeaways:

    • Using string equality on subtask_type is highly suspicious first of all. We should probably be doing an isinstance check since I think the objects we're touch at this stage are well formed Signature instances (otherwise .subtask_type would blow up).
    • But the actual fix for this is likely to be to modify that conditional to correctly handle a tail task which is a simple signature in the else case, and add a preceding elif to handle when the trailing task is a chain

    I'll try to find some time to get a patch up this evening. We'll also want to add a couple of regression tests. This should affect all backends with chords since it's a counting error so unit tests and a chord supporting backend integration test would be nice.

    Edit: FTR the broken lines were added in #6576 but IDK if it functioned as expected before then

  5. maybe-sybr commented on Apr 13, 2021

    @maybe-sybr
    Contributor
    diff --git a/celery/canvas.py b/celery/canvas.py
    index 57b0aea06..e3e8e3d45 100644
    --- a/celery/canvas.py
    +++ b/celery/canvas.py
    @@ -1180,7 +1180,7 @@ class group(Signature):
                         if isinstance(sig, _chain):
                             if sig.tasks[-1].subtask_type == 'chord':
                                 chord_size = sig.tasks[-1].__length_hint__()
    -                        else:
    +                        elif sig.tasks[-1].subtask_type == "group":
                                 chord_size = task_index + len(sig.tasks[-1])
                         app.backend.set_chord_size(group_id, chord_size)
                     sig.apply_async(producer=producer, add_to_parent=False,

    This diff does appear to run the MRTC as expected ->

    [2021-04-13 16:11:47,828: INFO/MainProcess] celery@karma ready.
    [2021-04-13 16:11:47,828: INFO/MainProcess] Received task: __main__.task_a[65331ad0-7ace-4939-b451-f3eda9ec157c]
    [2021-04-13 16:11:47,829: INFO/MainProcess] Received task: __main__.task_a[c3524b98-ae11-441d-b68d-fa895253dea0]
    [2021-04-13 16:11:47,829: INFO/MainProcess] Received task: __main__.task_a[5c6fe9ee-5a28-4571-8c09-57540503aeb0]
    [2021-04-13 16:11:47,931: WARNING/ForkPoolWorker-8] Task A: 1
    [2021-04-13 16:11:47,931: WARNING/ForkPoolWorker-7] Task A: 0
    [2021-04-13 16:11:47,932: WARNING/ForkPoolWorker-1] Task A: 2
    [2021-04-13 16:11:47,942: INFO/MainProcess] Received task: __main__.task_b[228c3d19-4a26-4e9f-9613-57c095cb0078]
    [2021-04-13 16:11:47,943: INFO/ForkPoolWorker-7] Task __main__.task_a[65331ad0-7ace-4939-b451-f3eda9ec157c] succeeded in 0.011826527999801328s: 0
    [2021-04-13 16:11:47,943: INFO/MainProcess] Received task: __main__.task_b[3fe44fe4-47a2-4b41-adac-cf0ea7e7cf44]
    [2021-04-13 16:11:47,943: INFO/ForkPoolWorker-8] Task __main__.task_a[c3524b98-ae11-441d-b68d-fa895253dea0] succeeded in 0.011867351000546478s: 1
    [2021-04-13 16:11:47,944: WARNING/ForkPoolWorker-2] Task B: 0
    [2021-04-13 16:11:47,944: WARNING/ForkPoolWorker-3] Task B: 1
    [2021-04-13 16:11:47,945: INFO/MainProcess] Received task: __main__.task_b[abe8305a-7c95-43f2-a519-074b0e36959b]
    [2021-04-13 16:11:47,945: WARNING/ForkPoolWorker-3] OCPR: chord_size_bytes=b'3' ready=2
    [2021-04-13 16:11:47,945: WARNING/ForkPoolWorker-2] OCPR: chord_size_bytes=b'3' ready=1
    [2021-04-13 16:11:47,945: INFO/ForkPoolWorker-1] Task __main__.task_a[5c6fe9ee-5a28-4571-8c09-57540503aeb0] succeeded in 0.014302862000477035s: 2
    [2021-04-13 16:11:47,946: INFO/ForkPoolWorker-3] Task __main__.task_b[3fe44fe4-47a2-4b41-adac-cf0ea7e7cf44] succeeded in 0.0021919120008533355s: 1
    [2021-04-13 16:11:47,946: WARNING/ForkPoolWorker-7] Task B: 2
    [2021-04-13 16:11:47,946: INFO/ForkPoolWorker-2] Task __main__.task_b[228c3d19-4a26-4e9f-9613-57c095cb0078] succeeded in 0.002304372999788029s: 0
    [2021-04-13 16:11:47,946: WARNING/ForkPoolWorker-7] OCPR: chord_size_bytes=b'3' ready=3
    [2021-04-13 16:11:47,948: INFO/ForkPoolWorker-7] Task __main__.task_b[abe8305a-7c95-43f2-a519-074b0e36959b] succeeded in 0.0020840869983658195s: 2
    [2021-04-13 16:11:47,948: INFO/MainProcess] Received task: __main__.task_c[28abaab2-7c2a-48c3-a83a-d099d678cbbf]
    [2021-04-13 16:11:47,948: WARNING/ForkPoolWorker-7] Task C: [0, 1, 2]
    [2021-04-13 16:11:47,949: INFO/ForkPoolWorker-7] Task __main__.task_c[28abaab2-7c2a-48c3-a83a-d099d678cbbf] succeeded in 0.0005527139983314555s: None
    ^C
    worker: Hitting Ctrl+C again will terminate all running tasks!
    

    The logic is this block is actually a little more tortured than I thought. It's re-writing the chord size each time it loops but I think it needs to do this in an additive way which it doesn't appear to be doing, as well as possibly handling tail groups and chords but not chains. We'll have to think about this pretty carefully, like usual with these chord counting issues :/

  6. thedrow commented on Apr 13, 2021

    @thedrow
    Contributor

    Thank you for the analysis, @maybe-sybr. ❤️
    Would you be able to come up with a fix?

  7. maybe-sybr commented on Apr 14, 2021

    @maybe-sybr
    Contributor

    Thank you for the analysis, @maybe-sybr. heart
    Would you be able to come up with a fix?

    I should have time to sit down and work through this is in the next day or two, yeah. I've actually found another edge case which might be related to this code and is affecting my own work so I'll be able to spend some time digging into it and hopefully get a PR up before this weekend.

  8. maybe-sybr commented on Apr 18, 2021

    @maybe-sybr
    Contributor

    This is on my list for today.

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

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions