Restart worker after idle period · Issue #9031 · celery/celery · GitHub
Skip to content

Restart worker after idle period #9031

Open

Description

@prhbrt

I'll work around this, so this is just a suggestion, to help the AI-comrads, as these often have long running tasks and hw/memory requirements.

  • I have checked the issues list
    for similar or identical feature requests.
  • I have checked the pull requests list
    for existing proposed implementations of this feature.
  • I have checked the commit log
    to find out if the same feature was already implemented in the
    main branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).

Related Issues and Possible Duplicates

  • None

Related Issues

  • None

Possible Duplicates

  • None

Brief Summary

To leverage loading (AI/LLM-)models in (GPU-)memory and having memory available, an option similar to worker_max_tasks_per_child that kills a worker after an idle-time to free memory would be helpful. This naturally implements caching and loading if needed.

Design

Architectural Considerations

I couldn't quit grasp the celery comsumer-loop, but I'm going to mimic this behavior by spinning off a thread that checks idle-time and releases memory when too idle.

However, my best guess is that this is the consumer loop. My next guess is that it would have some sort of condition-variable to wait for jobs. I'd suggest adding a timeout there to sometimes check worker_max_idle.

Proposed Behavior

After a worker has been idle for at least worker_max_idle, the worker is either killed or restarted.

Proposed UI/UX

worker_max_idle=120

Activity

  1. thedrow commented on Sep 5, 2024

    @thedrow
    Contributor

    Contributions are welcome!

  2. Nusnus commented on Sep 5, 2024

    @Nusnus
    Member

    @prhbrt

    Proposed Behavior

    After a worker has been idle for at least worker_max_idle, the worker is either killed or restarted.

    I’m not sure about the restarted, but the killed can make sense.
    A host system can instantiate a new worker if the previous one was killed to “restart” the consumer

    Proposed UI/UX

    worker_max_idle=120

    Would you like this value to say, “Kill the worker in 120 seconds since the last running task finished (and no other task is currently running)“?

    Lastly,

    To leverage loading (AI/LLM-)models in (GPU-)memory and having memory available

    Can you better define the problem you’re trying to solve with this?

  3. yingw787 commented on Aug 3, 2025

    @yingw787

    I just found this issue, but it's applicable to me:

    • Our company just came off of 2.5 weeks of debugging Celery workers - after a lot of root cause analysis, we discovered that DigitalOcean's Managed Database for Valkey disables connections without notification after an idle period timeout of 300 seconds. @lzhangbb can talk more about this.
    • We might have some memory leaks in our Celery task definitions, but idle workers don't seem to give up memory back to the compute instance. We did set --max-memory-per-child=200000 (or 200MB) and that does help, but it'd be nice to get back to a relaxed memory state.
  4. myayush commented on Oct 15, 2026

    @myayush

    I'd like to work on this. Proposal: a worker_max_idle setting (seconds, off by default). If the worker has no running, reserved or scheduled tasks for that long, it shuts down cleanly (exit code 0), and a process manager like systemd or Kubernetes can start a new one. I'll open a PR with tests and docs shortly.

  5. auvipy commented on Oct 17, 2026

    @auvipy
    Member

    3. I’m not sure about the restarted, but the killed can make sense.
    A host system can instantiate a new worker if the previous one was killed to “restart” the consumer
    > ## Proposed UI/UX
    > worker_max_idle=120

    Would you like this value to say, “Kill the worker in 120 seconds since the last running task finished (and no other task is currently running)“?
    Lastly,
    > To leverage loading (AI/LLM-)models in (GPU-)memory and having memory available
    
    
    Can you better define the problem you’re trying to solve with this?
    

    @myayush did you check this?

  6. myayush commented on Oct 17, 2026

    @myayush

    @auvipy Yes:

    1. Yes. If the worker has no tasks for worker_max_idle seconds, it stops cleanly. Then systemd or Kubernetes can start a new worker.
    2. The problem: a worker can hold a lot of memory (for example, an ML model on the GPU) even when it has no work for hours. The current limits only work while tasks are running, so they can't free this memory.
  7. added this to the 5.7.x milestone on Oct 18, 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