#3196·sanic

Main process never exits and spins a CPU when SIGTERM arrives while workers are still starting (wait_for_ack ignores the shutdown message)

Author: alexliyu7352Created Sep 30, 2026Updated Sep 30, 2026

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

If the main process receives SIGTERM (or SIGINT) after the workers have been started but before all of them have acknowledged startup, the workers are terminated as expected, but the main process never exits. It stays in WorkerManager.wait_for_ack() and busy-loops (about 0.65 of a CPU core in our measurements) until it is killed with SIGKILL.

What happens, as far as we can tell (line numbers from sanic/worker/manager.py in 25.12.1; the main branch has the same code):

  1. WorkerManager.shutdown_signal() sends None into the manager's own monitor pipe (self.monitor_publisher.send(None)) and then calls shutdown(), which terminates the worker processes.

  2. The steady-state loop, _poll_monitor(), recognises this: if not message: return MonitorCycle.BREAK.

  3. But monitor() first runs wait_for_ack(), and that loop does not recognise it. Every message other than "__TERMINATE_EARLY__" is sent back into the pipe, followed by continue:

    python
    while not self._all_workers_ack():
        if self.monitor_subscriber.poll(0.1):
            monitor_msg = self.monitor_subscriber.recv()
            if monitor_msg != "__TERMINATE_EARLY__":
                self.monitor_publisher.send(monitor_msg)
                continue
            ...
        misses += 1
        if misses > self.THRESHOLD:
            ...
            self.kill()
  4. The continue skips misses += 1, so the timeout never triggers. poll(0.1) returns immediately because the None is back in the pipe, and the workers have been terminated, so _all_workers_ack() never becomes true. The main process receives and re-sends its own None forever.

The log shows Received signal SIGTERM. Shutting down., and nothing happens after that.

Code snippet

A slow before_server_start widens the startup window so the race is easy to hit:

python
import asyncio

from sanic import Sanic
from sanic.response import text

app = Sanic("SlowStart")


@app.get("/")
async def index(request):
    return text("ok")


@app.before_server_start
async def slow(app):
    await asyncio.sleep(4)


if __name__ == "__main__":
    app.run(host="127.0.0.1", port=18431, workers=2, access_log=False)

Steps:

  1. Run python app.py.
  2. Within the first 4 seconds (after Starting worker [...] appears), send SIGTERM to the main process: kill -TERM <main pid>.
  3. The workers exit, but the main process keeps running and using CPU. Ten seconds later it is still alive and only SIGKILL stops it.

It also happens with workers=1, and with Sanic.serve(...), which always goes through WorkerManager.run() → monitor() → wait_for_ack().

Expected Behavior

The main process shuts down the same way it does once the workers are running: monitor() sees the shutdown message, leaves its loop, and the manager joins, terminates and cleans up, so the process exits.

A possible fix is to handle the falsy message in wait_for_ack() the way _poll_monitor() does: put it back for monitor()'s loop and stop waiting.

diff
             monitor_msg = self.monitor_subscriber.recv()
+            if not monitor_msg:
+                # Sent by shutdown_signal(); let monitor() handle the shutdown.
+                self.monitor_publisher.send(monitor_msg)
+                return
             if monitor_msg != "__TERMINATE_EARLY__":
                 self.monitor_publisher.send(monitor_msg)
                 continue

We are currently working around it by replacing WorkerManager.wait_for_ack with a copy that includes this change. With the change, the reproduction above exits about 0.5 s after SIGTERM, and a SIGTERM after startup behaves as before.

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Linux

Sanic Version

Sanic 25.12.1; Routing 23.12.0

Additional context

Python 3.12.12, Linux 5.15. In the real deployment this was hit by a process manager that sent SIGTERM right after a service started, for example during a quick redeploy or a health-check-driven restart.