Repository navigation
Description
Checklist
Verified in both the released 4.2.1 and the master (which is reported as 4.2.0).
- I have included the output of
celery -A proj reportin the issue.
(if you are not able to do this, then at least specify the Celery
version affected).
software -> celery:4.2.1 (windowlicker) kombu:4.2.1 py:3.6.5
billiard:3.5.0.4 py-amqp:2.3.2
platform -> system:Linux arch:64bit, ELF imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:pyamqp results:disabled
broker_url: 'amqp://guest:********@172.17.0.1:5672//'
- I have verified that the issue exists against the
masterbranch of Celery.
software -> celery:4.2.0 (windowlicker) kombu:4.2.1 py:3.6.5
billiard:3.5.0.4 py-amqp:2.3.2
platform -> system:Linux arch:64bit, ELF imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:pyamqp results:disabled
broker_url: 'amqp://guest:********@172.17.0.1:5672//'
Description
The celery workers of several different applications I work on would sporadically start leaking memory in a continuous fashion, until the machine ran out of memory. We initially tried to work around that by making use of the flag that automatically restart the workers after a certain number of tasks was completed. However, the leak turned out to be in the main worker process that manages workers, so this did not help.
I've found 3 issues that together cause this to happen. One of them clearly seems to be a celery/kombu issue, which is what this report is about. For the other two it's not clear yet but I'll include them for context and reproducibility.
The first issue is that a high CPU (or maybe IO) load can cause Celery workers to lose connections. There's been mention of this in #3377 and #3932 . I've been able to reproduce that by using the stress tool with cpu and io flags enabled. I see celery worker connection resets and missed heartbeats appearing in the logs when I do that. In particular I see the connection resets in the worker log for application A when restarting the worker for application B, while under that load. While I saw similar log messages right before memory leaking started to happen, the leaking didn't happen consistently. I believe this is because the connections that are killed are somewhat random, and some of them can recover from that while others don't.
The second issue, for which the reproduction steps are listed below, is that if the celery worker main process' connection used for sending heartbeats to the rabbitmq broker is broken, it will stay broken. In an strace you can see it repeatedly getting SIGPIPE / broken pipe errors. The process keeps 1-2 other connections open to the broker, one of them consistently (I think for receiving the status of other workers and maybe incoming jobs) another intermittent. If I kill the consistent one, another one is immediately recreated. I've also tried swapping out rabbitmq for redis as the broker. When the same connections are killed in that case, they are also recreated right after. I've only found the rabbitmq connection that sends heartbeats to be affected. If the --without-heartbeat flag is used, the connection is still opened, but there's no longer a heartbeat being sent over it. #3921, #3773, #3377 and #3932 appear to be related.
The third issue is that when the repeated writing attempts to the broken connection happen, the main worker process starts leaking memory. I'm not sure yet if this is at the Python object level or something lower.
Potential workarounds appear to be to use a different broker or the --without-heartbeat flag (although I'm not sure if the connection that still gets opened in that case is also used for other things).
Steps to reproduce
Assuming a Ubuntu 18.04 system, you'll need to have the strace and dsniff (for tcpkill) packages installed.
- Create a simple
tasks.pythat uses rabbitmq as the broker. Set the appropriate credentials for your broker. Make sure the ip address used is an external interface, not127.0.0.1, because we want an internet socket to be used, not a unix one.
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest:[email protected]//')
@app.task
def add(x, y):
return x + y
-
Run
python -m celery worker -A tasks -
Run
sudo ss -a -p | grep amq. Assuming your celery test worker is the only thing connected to rabbitmq, you should see something like:
tcp ESTAB 0 0 172.17.0.1:57522 172.17.0.1:amqp users:(("python",pid=6051,fd=46))
tcp ESTAB 0 0 172.17.0.1:57518 172.17.0.1:amqp users:(("python",pid=6051,fd=44))
tcp ESTAB 0 0 172.17.0.1:57520 172.17.0.1:amqp users:(("python",pid=6051,fd=45))
tcp LISTEN 0 128 *:amqp *:* users:(("beam.smp",pid=1741,fd=56))
tcp ESTAB 0 0 [::ffff:172.17.0.1]:amqp [::ffff:192.168.1.59]:57518 users:(("beam.smp",pid=1741,fd=14))
tcp ESTAB 0 0 [::ffff:172.17.0.1]:amqp [::ffff:192.168.1.59]:57520 users:(("beam.smp",pid=1741,fd=15))
tcp ESTAB 0 0 [::ffff:172.17.0.1]:amqp [::ffff:192.168.1.59]:57522 users:(("beam.smp",pid=1741,fd=16))
Your worker will be the python process. If you have more processes running you'll need to narrow it down to the pid of the main worker process using ps aux or similar. There will be 2-3 file descriptors that the process has open towards rabbitmq, which increase linearly from some starting value. We're interested in the second one, fd=45 in this example. I'll refer to the pid of the main worker process as $PID in subsequent commands. Also make note of the port associated with that file descriptor, 57520 in this example. I'll refer to it as $PORT subsequently.
- Run
sudo strace -tt -f -s 1000 -p $PID -e trace=sendto. You'll see celery sending heartbeat messages to the mentioned second file descriptor (45 in this example).
10:40:20.011043 sendto(45, "\1\0\1\0\0\0!\0<\0(\0\0\10celeryev\20worker.heartbeat\0\316\2\0\1\0\0\0H\0<\0\0\0\0\0\0\0\0\1\20\370\0\20application/json\5utf-8\0\0\0\35\10hostnameS\0\0\0\17celery@amaranth\1\0\316\3\0\1\0\0\1\20{\"hostname\": \"celery@amaranth\", \"utcoffset\": -2, \"pid\": 6051, \"clock\": 481, \"freq\": 2.0, \"active\": 0, \"processed\": 0, \"loadavg\": [0.56, 0.68, 0.61], \"sw_ident\": \"py-celery\", \"sw_ver\": \"4.2.1\", \"sw_sys\": \"Linux\", \"timestamp\": 1536741620.0103393, \"type\": \"worker-heartbeat\"}\316", 401, 0, NULL, 0) = 401
-
Now we're going to use
tcpkillto simulate the issue where celery workers can lose connections under high load. Runsudo tcpkill -i lo host 172.17.0.1 and port $PORT, substituting the network interface passed to-iand the ip address with the ones you're using.tcpkillwill run continuously, but you can close it after it's printed its initial output. You can verify withssthat the connection is gone. -
Run
sudo strace -tt -f -s 1000 -p $PID -e trace=sendtoagain. There will be repeated SIGPIPE errors.
11:14:53.978952 sendto(45, "\1\0\1\0\0\0!\0<\0(\0\0\10celeryev\20worker.heartbeat\0\316\2\0\1\0\0\0H\0<\0\0\0\0\0\0\0\0\1\17\370\0\20application/json\5utf-8\0\0\0\35\10hostnameS\0\0\0\17celery@amaranth\1\0\316\3\0\1\0\0\1\17{\"hostname\": \"celery@amaranth\", \"utcoffset\": -2, \"pid\": 6051, \"clock\": 2476, \"freq\": 2.0, \"active\": 0, \"processed\": 0, \"loadavg\": [0.79, 0.7, 0.73], \"sw_ident\": \"py-celery\", \"sw_ver\": \"4.2.1\", \"sw_sys\": \"Linux\", \"timestamp\": 1536743693.978148, \"type\": \"worker-heartbeat\"}\316", 400, 0, NULL, 0) = -1 EPIPE (Broken pipe)
11:14:53.979275 --- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=6051, si_uid=1000} ---
- Monitor the resident memory size of the main worker process over a longer time period, say half an hour. For example using
pidstat -r -p $PID 1. In my case the memory size increases and keeps increasing by about 20 megabytes per hour.
Expected behavior
- The connection reappears after killing it
- There are no SIGPIPE errors
- Ideally no leaking, but that might not be because of Celery
Example log message when issue 1 (losing connections under load) happens.
This is the log from the worker for app1. app2 is a different application that has its own workers.
[2018-08-30 09:19:02,621: INFO/MainProcess] [2018-08-30 09:19:02Z] missed heartbeat from app2@worker-1
[2018-08-30 09:19:08,588: INFO/MainProcess] [2018-08-30 09:19:08Z] sync with app2@worker-2
[2018-08-30 09:19:08,589: ERROR/MainProcess] [2018-08-30 09:19:08Z] Control command error: ConnectionResetError(104, 'Connection reset by peer')
[2018-08-30 09:19:10,349: INFO/MainProcess] [2018-08-30 09:19:10Z] sync with app2@worker-1
Activity
mgeens commentedon Sep 12, 2018
I was able to reproduce the connection loss under load issue on my local machine using the above example. This is using the master branch at https://github.com/celery/celery/tree/ed17e12b14051bac51b84593cf7c8cf21c77e010
Start two celery workers running each of the two tasks, preferably in two side-by-side terminals:
python -m celery worker -A tasks --loglevel=debug -n tasks@%hpython -m celery worker -A tasks2 --loglevel=debug -n tasks2@%hIn another terminal runUpdate 2: indeed a red herring, you just need to wait until the heartbeat timeouts in step 4 happen.stressand max out your cores, I usedstress --cpu 8 --io 8. It seems it can sometimes also reproduce without this step, but it seems to happen less frequently. Maybe a red herring.Wait 5 minutes. Update: you can also watch the rabbitmq log with
tail -f. After a few minutes, it will report closing two AMQP connections due to a heartbeat timeout. See the log below. After that happens, proceed to the next step.Close the first worker with ctrl-c. The second worker will note its departure
tasks@amaranth left.Start the first worker again.
The second worker will show a connection reset error:
This method has been fairly consistent for me, but it seems some chance is still involved.
Rabbitmq log:
auvipy commentedon Sep 12, 2018
thanks for the detailed report. do you have any fix in mind? or any suggestions?
mgeens commentedon Sep 13, 2018
I haven't looked into a fix yet. At work they're switching to a redis broker which would seem to avoid the whole issue, so I probably can't justify spending more time on it there. However, I'm still interested in finding the root causes for these issues and fixing them, so I intend to spend some of my spare time on it.
auvipy commentedon Sep 13, 2018
I have tagged this for django con us sprint 2018
Retry if the heartbeat connection dies (celery#5047)
mgeens commentedon Sep 22, 2018
I found the cause of the broken pipe error and leak and made a fix. More details in the pull request.
mgeens commentedon Oct 28, 2018
I had a look at the AMQP network traffic when doing #5047 (comment). When the two tasks are running, rabbitmq sends 4 AMQP heartbeat messages to the workers every 60 seconds (2 for each worker). This seems to make sense given that they each have 2 connections open to rabbitmq. However, each worker only sends back 1 heartbeat in response. After about 3 minutes 2
closing AMQP connection <snip> missed heartbeats from client, timeout: 60smessages appear in the rabbitmq server log. In the network traffic, rabbitmq only sends 2 AMQP heartbeat messages from this point on, which receive 2 replies.In other words, it looks like 1 of the connections on each worker isn't responding to rabbitmq's heartbeat, and rabbitmq ends up terminating the connection in response eventually.
It should be noted that this is using the amq library. When I switched to librabbitmq (updating the broker urls to start with
librabbitmq://), I noticed there were no AMQP heartbeats in the network traffic. The issue also didn't reproduce anymore.So I suspect the connection loss issue either has to do with how Celery is currently using the amq library or something in amq itself going wrong. It also suggests that using librabbitmq instead of amq could be a workaround for people having these connection loss issues.
Packet dumps from doing #5047 (comment) with both amq and librabbitmq: celery-rabbitmq-pcap.zip . The amq one has everything except heartbeats filtered out, the librabbitmq one has all amqp communication.
kosmos342 commentedon Dec 28, 2018
Why this issue was closed? I though that py-amqp is recommended celery/librabbitmq#118 (comment)
iwinux commentedon Mar 21, 2019
This might not be completely related to RabbitMQ. I was trying out the experimental Redis Cluster transport, and encountered serious memory leak as well. My observations:
--without-heartbeatis specified.Channelinstances somewhere (confirmed by printing logs inChannelconstructor)mgeens commentedon Mar 21, 2019
Yes, the memory leak wasn't tied to rabbitmq as such, it was because of the heartbeat connection in Celery leaking if it died, which is why disabling it helps. A fix for that should be in 4.3.
The reason the connection failed in the first place does have something to do with rabbitmq, and I'm not aware of that having been resolved yet.
Add reproduction for Celery gevent EventDispatcher memory leak
Celery EventDispatcher memory leak reproduction + fix