Repository navigation
Description
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
auvipy commentedon Feb 5, 2019
which version of celery you are facing this?
mkielak commentedon Feb 5, 2019
I wrote it above - Celery 4.2.1
brabiega commentedon Feb 15, 2019
Probably the same issue here
#5000
@mkielak please check memory consumption
mkielak commentedon Feb 21, 2019
@brabiega Memory consumption comes to 100%
thedrow commentedon Mar 26, 2019
This probably happens because the serialized payload becomes too big.
brabiega commentedon Aug 22, 2019
You might want to try my workaround-ish solution
https://github.com/ovh/celery-dyrygent
pySilver commentedon Oct 21, 2020
Just curious. Is this still an issue with Celery 5? Canvas was supposed to be hugely refactored (apparently).
auvipy commentedon Oct 31, 2020
@maybe-sybr
thedrow commentedon Nov 2, 2020
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.
use pickle for serialization due to:
17 remaining items
DAAworld commentedon Nov 10, 2023
Hi,
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}"
brettelliot commentedon Mar 14, 2024
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.
Nusnus commentedon Mar 15, 2024
May I ask you check this on the latest version? v5.4.0rc1/
mainThank you 🙏
chrisbarber commentedon Jun 5, 2025
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)