Skip to content

Soft shutdown raises BlockingSwitchOutError on the gevent pool, so the worker never stops #11059

Done

Description

@felipeadeildo

Summary

On the gevent pool, the soft shutdown fails as soon as it starts. on_cold_shutdown calls worker.wait_for_soft_shutdown() from inside the signal handler, and that calls time.sleep, which gevent has patched. Under gevent the signal handler runs as an event loop callback, where a blocking call raises BlockingSwitchOutError. The worker logs Unrecoverable error, never cancels the running task, and stays up until the task ends or something kills it.

This happens with SIGQUIT, and with SIGTERM when REMAP_SIGTERM=SIGQUIT. That combination is what a container platform needs: ECS and Kubernetes send SIGTERM, then SIGKILL after a grace period.

Reproduce

app.py:

import time

from celery import Celery
from celery.signals import worker_ready

app = Celery("app", broker="memory://")
app.conf.worker_soft_shutdown_timeout = 10.0


@app.task
def long_task():
    time.sleep(60)


@worker_ready.connect
def send(**_):
    long_task.delay()
REMAP_SIGTERM=SIGQUIT celery -A app worker --pool=gevent --concurrency=1 --loglevel=INFO
# once "long_task[...] received" shows up, from another shell:
kill -TERM <pid>

Expected: the worker waits 10 s, cancels long_task and exits, the way it does with --pool=prefork.

Actual: it is still running 20 s after the signal (and keeps running until long_task returns). The log:

WARNING/MainProcess] Initiating Soft Shutdown, terminating in 10.0 seconds
WARNING/MainProcess] Traceback (most recent call last):
  File ".../celery/apps/worker.py", line 343, in _handle_request
    callback(worker)
  File ".../celery/apps/worker.py", line 463, in on_cold_shutdown
    worker.wait_for_soft_shutdown()
  File ".../celery/worker/worker.py", line 524, in wait_for_soft_shutdown
    sleep(app.conf.worker_soft_shutdown_timeout)
  File ".../gevent/hub.py", line 166, in sleep
    hub.wait(t)
  ...
gevent.exceptions.BlockingSwitchOutError: Impossible to call blocking function in the event loop callback
CRITICAL/MainProcess] Unrecoverable error: BlockingSwitchOutError('Impossible to call blocking function in the event loop callback')

Versions

Reproduced on celery 5.7.0 and on main (installed from git today), with kombu 5.7.0, billiard 4.3.2, gevent 26.9.0, CPython 3.15.0 and 3.14.7, Linux. It also reproduces on celery 5.6.3 with a Redis broker, so it doesn't depend on the memory transport.

Related

claude opus 5.5 (high effort) [tattler] @ adeildo's pi

Activity

  1. its-amann commented on Nov 9, 2026

    @its-amann
    Contributor

    i can take this. plan is to move the soft shutdown wait out of the signal handler on gevent (spawn it as a greenlet) so the sleep yields instead of raising, plus a test for it

  2. auvipy commented on Nov 10, 2026

    @auvipy
    Member

    it would be better if you can fix the both related issues in a single PR

  3. added this to the 5.7.x milestone on Nov 11, 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