Skip to content

Chain of chords takes all resources and fail #5286

Open

Description

@mkielak

I'm using celery for simple system executing tasks in order. I.e. [[1], [2,3], [4]] - in such case I want task 1 to be executed first, after it finishes I want to execute 2 and 3 parallel and then when both of these tasks finished I want to execute task 4. Simple.

For that case I'm using chain and chords (for parallel) or tasks inside chain. It seems that everyting works untill number od chords inside is small, but if it is about 15 or more celery hangs on apply async method, takes more and more memory untill it comes to 100% and system hangs.

Here is sample code:

@app.task
def sample_task():
    pass

@app.task
def sample_finish(ret):
    pass

ordered_tasks = [[0], [1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11], [12, 13], [14, 15], [16, 17], [18, 19], [20], [21], [22], [23], [24, 25], [26, 27], [28, 29], [30, 31], [32, 33], [34, 35], [36], [37]]
celery_tasks = [sample_task.s()]
for tasks in ordered_tasks:
    if len(tasks) > 1:
        celery_tasks.append(
            chord([sample_task.s() for _ in tasks],
                       sample_finish.s()))
    else:
        celery_tasks.append(sample_task.s())
celery_tasks.append(sample_finish.s())
chain(celery_tasks).apply_async()

I'm using Python 3.6, Celery 4.2.1 and Redis. I checked it also on RabbitMQ but result is same.
After debugging I found that it hangs when it tries to send message to backend in JSONEncoder class and iterencode() method.

It works well if I reduce ordered_tasks:

ordered_tasks = [[0], [1], [2, 3], [16, 17], [18, 19], [20], [24, 25], [26, 27], [34, 35], [36], [37]]

With longer lists It's working slower and slower (also taking more memory) and than cross some threshold after witch it is stuck.

UPDATE
Changing serializer to pickle solved the problem but it's rather workaround.

Activity

  1. auvipy commented on Feb 5, 2019

    @auvipy
    Member

    which version of celery you are facing this?

  2. mkielak commented on Feb 5, 2019

    @mkielak
    Author

    I wrote it above - Celery 4.2.1

  3. brabiega commented on Feb 15, 2019

    @brabiega
    Contributor

    Probably the same issue here
    #5000

    @mkielak please check memory consumption

  4. mkielak commented on Feb 21, 2019

    @mkielak
    Author

    @brabiega Memory consumption comes to 100%

  5. thedrow commented on Mar 26, 2019

    @thedrow
    Contributor

    This probably happens because the serialized payload becomes too big.

  6. added this to the 4.5 milestone on May 10, 2019
  7. brabiega commented on Aug 22, 2019

    @brabiega
    Contributor
  8. modified the milestones: 4.5, 4.4.x on Dec 16, 2019
  9. pySilver commented on Oct 21, 2020

    @pySilver

    Just curious. Is this still an issue with Celery 5? Canvas was supposed to be hugely refactored (apparently).

  10. auvipy commented on Oct 31, 2020

    @auvipy
    Member
  11. modified the milestones: 4.4.x, 5.0.2 on Oct 31, 2020
  12. thedrow commented on Nov 2, 2020

    @thedrow
    Contributor

    Canvas was supposed to be hugely refactored (apparently).

    We've elected to narrow down the scope of the major releases to avoid hitting to many bugs at once.
    I'm working on it.

  13. 17 remaining items

  14. modified the milestones: 5.3, Future on Feb 19, 2023
  15. DAAworld commented on Nov 10, 2023

    @DAAworld

    我没有时间进一步研究这个问题。它仍然是有问题的 AFAICT,最近在 5.0.5 上调试深度构造的画布任务时,我在自己的工作中看到了它的症状。我现在将取消分配并将其放入 5.2 中,但如果它在此之前登陆 5.1.x 版本会很好(尽管 5.1.0 似乎不太可能!如果可能,Ping 并重新分发。

    没有压力。我们将很快重新审视这个问题和相关的画布问题。我有一些积压工作。清洁后会检查

    Hi,

    I haven't had time to look further into this issue. It remains problematic AFAICT and I've seen symptoms of it in my own work when debugging deeply constructed canvas tasks recently on 5.0.5. I'm going to unassign myself and put this in 5.2 for now, but it would be nice if this were to land prior to that in a 5.1.x release (5.1.0 seems unlikely though!). Pinging @thedrow and @auvipy for redistribution if possible.

    no stress. we will revisit this and related canvas issues soon. I have some backlogs. will check after cleaning them

    Hi,I have same error too.I started the worker and beat to connect to redis as the broker. After a while, I disconnected from the network for about a minute. Then I reconnected to the network, celery beat will reconnect successfully, and celery worker will report an error:
    [2023-11-10 11:40:13,360: WARNING/MainProcess] consumer: Connection to broker lost. Trying to re-establish the connection...
    Traceback (most recent call last):
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/redis/connection.py", line 436, in send_packed_command
    self._sock.sendall(item)
    ConnectionResetError: [Errno 104] Connection reset by peer

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 340, in start
    blueprint.start(self)
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/celery/bootsteps.py", line 116, in start
    step.start(parent)
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 742, in start
    c.loop(*c.loop_args())
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/celery/worker/loops.py", line 97, in asynloop
    next(loop)
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/kombu/asynchronous/hub.py", line 373, in create_loop
    cb(*cbargs)
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/kombu/transport/redis.py", line 1341, in on_readable
    self.cycle.on_readable(fileno)
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/kombu/transport/redis.py", line 566, in on_readable
    chan.handlerstype
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/kombu/transport/redis.py", line 910, in _receive
    ret.append(self._receive_one(c))
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/kombu/transport/redis.py", line 920, in _receive_one
    response = c.parse_response()
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/redis/client.py", line 786, in parse_response
    self.check_health()
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/redis/client.py", line 824, in check_health
    conn.send_command("PING", self.HEALTH_CHECK_MESSAGE, check_health=False)
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/redis/connection.py", line 458, in send_command
    self.send_packed_command(
    File "/home/ddd/miniconda3/envs/ccc/lib/python3.10/site-packages/redis/connection.py", line 447, in send_packed_command
    raise ConnectionError(f"Error {errno} while writing to socket. {errmsg}.")
    redis.exceptions.ConnectionError: Error 104 while writing to socket. Connection reset by peer.

    I debugged it and found the problem.When disconnected network,it will call the function "on_connection_error_after_connected",
    def on_connection_error_after_connected(self, exc):
    warn(CONNECTION_RETRY, exc_info=True)
    try:
    self.connection.collect()
    except Exception: # pylint: disable=broad-except
    pass
    The program will block in function "self.connection.collect()".Finally it run " **string_keys_to_dict("BLPOP BRPOP", lambda r: r and tuple(r) or None),".

    If I comment out these four lines of code,
    try:
    self.connection.collect()
    except Exception: # pylint: disable=broad-except
    pass
    The worker can reconnect redis too.

    My celery config:
    BROKER_URL = f"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/{REDIS_CELERY_DB}"

    CELERYD_CONCURRENCY = 1#3  
    CELERYD_MAX_TASKS_PER_CHILD = 200
    CELERYD_FORCE_EXECV = True
    CELERY_TIMEZONE = "Asia/Shanghai"
    CELERYD_PREFETCH_MULTIPLIER = 1 
    
    CELERY_REDIS_RETRY_ON_TIMEOUT = True
    CELERY_REDIS_SOCKET_CONNECT_TIMEOUT = 7200
    REDIS_BACKEND_HEALTH_CHECK_INTERVAL = 15
    CELERY_REDIS_SOCKET_TIMEOUT = 7200
    redis_retry_on_timeout = True
    BROKER_CHANNEL_ERROR_RETRY = True
    CELERY_REDIS_SOCKET_KEEPALIVE = True
    
    
    BROKER_CONNECTION_MAX_RETRIES = 0
    BROKER_HEARTBEAT = 90
    BROKER_HEARTBEAT_CHECKRATE = 3
    
  16. modified the milestones: Future, 5.4 on Nov 10, 2023
  17. brettelliot commented on Mar 14, 2024

    @brettelliot

    Definitely been bitten by this bug. Chains of chords just don't work (using 5.2.7). And since if you chain a group together with a group, it becomes a chord, thus, this problem manifests itself again.

  18. Nusnus commented on Mar 15, 2024

    @Nusnus
    Member

    Definitely been bitten by this bug. Chains of chords just don't work (using 5.2.7). And since if you chain a group together with a group, it becomes a chord, thus, this problem manifests itself again.

    May I ask you check this on the latest version? v5.4.0rc1/main

    Thank you 🙏

  19. modified the milestones: 5.4, 5.5 on Jun 25, 2024
  20. chrisbarber commented on Jun 5, 2025

    @chrisbarber
    Contributor

    Anyone following, recommend trying pickle serializer instead of default (Json), and reporting back whether it fixes the issue for you. See #7029 (reply in thread)

  21. modified the milestones: 5.5.0, 5.7.0 on Jun 11, 2025
  22. modified the milestones: 5.7.0, 5.8.0 on Aug 31, 2026
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