Repository navigation
Description
When trying to invoke worker with beat (celery -A tasks worker -B), it starts but one thread stays at 100% processor and the beat worker does not log anything. See minimal reproducing steps below, which I extracted from a larger project.
The root cause is: when started in a docker environment where SC_OPEN_MAX is very large (my current docker sets it to 1073741816 by default) the function close_open_fds invoked at https://github.com/celery/celery/blob/5.2/celery/beat.py#L715 , which invokes https://github.com/celery/billiard/blob/v4.1.0/billiard/compat.py#L133 , will iterate through the 1 billion file descriptors before returning (so technically might finish but I did not wait longer than couple of minutes).
While the root cause is in billiard, I realized that while trying to debug the celery issue, so might help other people having similar issues.
I consider this a bug, as having a very large total possible number of open files should not incur a starting penalty of minutes - other mechanisms for closing potentially open files could be used (like iterating over /proc/self/fd).
You can use the script below to reproduce:
#!/bin/bash
# Start a broker
docker run -d --network host -p 6379:6379 redis
# Create a task file
cat > tasks.py <<EOF
from celery import Celery
app = Celery('tasks', broker='redis://localhost')
@app.task
def add(x, y):
return x + y
EOF
# Try to start celery worker with -B
# This will keep 1 cpu at 100% and not display 'beat: Starting...'.
# Note that while I set the ulimit to unreasonable values, my system
# had this by default, this is just to make a reproducible case
docker run \
--network host \
--ulimit nofile=1073741816:1073741816 \
-v $PWD:$PWD \
python:3.11 \
bash -c "pip3 install celery[redis]; cd $PWD; celery -A tasks worker -B --loglevel=DEBUG"
# Workaround (which results in celery displaying immediately 'beat: Starting...') is to reduce nofile to reasonable values:
# docker run \
# --network host \
# --ulimit nofile=2048:2048 \
# -v $PWD:$PWD \
# python:3.11 \
# bash -c "pip3 install celery[redis]; cd $PWD; celery -A tasks worker -B --loglevel=DEBUG"
Activity
open-collective-bot commentedon Jun 10, 2023
Hey @vladmihaisima 👋,
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.
auvipy commentedon Jun 13, 2023
is there any improvement/adjustment you have in mind for billiard & celery related codes?
Fix for celery/celery#8306
vladmihaisima commentedon Jun 13, 2023
You can see below what I had in mind:
https://github.com/celery/billiard/compare/main...vladmihaisima:billiard:fix_celery_8306?expand=1
I did not run all billiard tests/wondered about all platforms that it should run etc.
For my environment it works with the following test code:
which results in very long execution time without the fix (did not wait to finish), or in the following with the fix:
For a system that does not have a huge SC_OPEN_MAX set the actual times will be the ones shown in close range. For a system that does have a huge SC_OPEN_MAX set the actual times will be the ones for "new close_open_fds".
The only case in which the new close_open_fds is slower is when SC_OPEN_MAX is more than a million, and number of of open files is close to SC_OPEN_MAX - but I doubt this ever happens, and anyhow, 2x is not that bad considering that current status is for function to take more than 1 hour in the worst case of SC_OPEN_MAX being millions and couple of open files.
andresmrm commentedon Jun 27, 2023
Hi! I can confirm this issue with Celery v5.3.1 + RabbitMQ + Docker, both in a VPS and in my local computer.
Running
celery beatin a separate container seems to have solved it.Run celery beat in a separate container
ofipify commentedon Oct 3, 2023
@vladmihaisima thanks for posting this. You just restored my sanity, as I couldn't figure out why my Celery beat wasn't starting up. My Docker is also in the three commas club when it comes to
ulimitsfor open files (1,073,741,816).As we start Celery in a Docker container via
docker compose, I managed to alleviate the time penalty by adding the following to the celery service:Celery 5.3.1 + Redis
Run celery beat in a separate container
Run celery beat in a separate container
GertBurger commentedon Jan 24, 2025
This is still a problem with Celery 5.4.0.
When starting celery with beats on a system where
SC_OPEN_MAXwill be very large, results in the process attempting to closeSC_OPEN_MAXnumber of FDs.On my system whilst testing k3s this number was
1073741816, which will never complete.The problem lies in
close_open_fds()ofbilliard/compat.pyattempting to close a stupid amount of FDs which will either take a while or never finish.So technically this issue is in celery/billiard#399
Workarounds are:
nofileis a reasonable value which can be iterated over quicklydocker-compose: Work around high cpu usage for celery containers due …
docker-compose: Work around high cpu usage for celery containers due …
docker-compose: Work around high cpu usage for celery containers due …
docker-compose: Work around high cpu usage for celery containers due …
docker-compose: Work around high cpu usage for celery containers due …
docker-compose: Work around high cpu usage for celery containers due …
docker-compose: Work around high cpu usage for celery containers due …
[fix]docker-compose: Work around high cpu usage for celery containers…
docker-compose: Work around high cpu usage for celery containers due …
Combined changes from master branch
docker-compose: Work around high cpu usage for celery containers due …