Main process never exits and spins a CPU when SIGTERM arrives while workers are still starting (wait_for_ack ignores the shutdown message)
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):
WorkerManager.shutdown_signal()sendsNoneinto the manager's own monitor pipe (self.monitor_publisher.send(None)) and then callsshutdown(), which terminates the worker processes.The steady-state loop,
_poll_monitor(), recognises this:if not message: return MonitorCycle.BREAK.But
monitor()first runswait_for_ack(), and that loop does not recognise it. Every message other than"__TERMINATE_EARLY__"is sent back into the pipe, followed bycontinue: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()The
continueskipsmisses += 1, so the timeout never triggers.poll(0.1)returns immediately because theNoneis 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 ownNoneforever.
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:
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:
- Run
python app.py. - Within the first 4 seconds (after
Starting worker [...]appears), send SIGTERM to the main process:kill -TERM <main pid>. - 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.
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)
continueWe 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.
Source: sanic-org/sanic