Skip to content

Celery worker using beat, stays at 100% cpu for long time when used in docker #8306

Open

Description

@vladmihaisima

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

  1. open-collective-bot commented on Jun 10, 2023

    @open-collective-bot

    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.

  2. auvipy commented on Jun 13, 2023

    @auvipy
    Member

    is there any improvement/adjustment you have in mind for billiard & celery related codes?

  3. added a commit that references this issue on Jun 13, 2023
  4. vladmihaisima commented on Jun 13, 2023

    @vladmihaisima
    Author

    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:

    import os
    import time
    
    from billiard.compat import close_open_fds
    
    for fd_number in [ 100, 1_000, 10_000, 100_000, 1_000_000]:
      tmp = []
      for i in range(0,fd_number):
        tmp.append(open(f"file{i%1000}","wt"))
    
      keep = [0,1,2]
    
      start = time.time()
      close_open_fds(keep)
      print(f"Time (new) close_open_fds (for {fd_number:8d} open files):  {time.time()-start:2.5f}")
    
      tmp = []
      for i in range(0, fd_number):
        tmp.append(open(f"file{i%1000}","wt"))
    
      start = time.time()
      os.closerange(4, fd_number)
      print(f"Time close range          (for {fd_number:8d} descriptors): {time.time()-start:2.5f}")
    

    which results in very long execution time without the fix (did not wait to finish), or in the following with the fix:

    Time (new) close_open_fds (for      100 open files):  0.00024
    Time close range          (for      100 descriptors): 0.00012
    Time (new) close_open_fds (for     1000 open files):  0.00207
    Time close range          (for     1000 descriptors): 0.00079
    Time (new) close_open_fds (for    10000 open files):  0.02080
    Time close range          (for    10000 descriptors): 0.00700
    Time (new) close_open_fds (for   100000 open files):  0.19831
    Time close range          (for   100000 descriptors): 0.08291
    Time (new) close_open_fds (for  1000000 open files):  2.08239
    Time close range          (for  1000000 descriptors): 0.86849
    

    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.

  5. andresmrm commented on Jun 27, 2023

    @andresmrm

    Hi! I can confirm this issue with Celery v5.3.1 + RabbitMQ + Docker, both in a VPS and in my local computer.
    Running celery beat in a separate container seems to have solved it.

  6. ofipify commented on Oct 3, 2023

    @ofipify

    @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 ulimits for 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:

    worker:
        ulimits:
          nofile:
            soft: 10000
            hard: 10000

    Celery 5.3.1 + Redis

  7. added a commit that references this issue on Jun 20, 2024
  8. GertBurger commented on Jan 24, 2025

    @GertBurger

    This is still a problem with Celery 5.4.0.

    When starting celery with beats on a system where SC_OPEN_MAX will be very large, results in the process attempting to close SC_OPEN_MAX number of FDs.

    On my system whilst testing k3s this number was 1073741816, which will never complete.

    The problem lies in close_open_fds() of billiard/compat.py attempting 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:

    • Don't start celery with beats at the same time, start them separately
    • Ensure nofile is a reasonable value which can be iterated over quickly
  9. added this to the 5.7.0 milestone on May 19, 2025
  10. added a commit that references this issue on Aug 13, 2025
  11. 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