SIGINT/SIGTERM doesn't stop Sanic?

Hi!

I’m facing an issue and can’t find a working solution.

I have Sanic running via Supervisor, with the following configuration file:

[program:project]
user = me
command=/var/www/project/env/bin/python /var/www/project/www/server.py --prod
autostart=true
autorestart=true
stopsignal=INT
startsecs=2
stopwaitsecs=5

(I also tried with stopsignal=TERM)

Here’s the logs in supervisor:

[2023-03-23 10:11:01 +0000] [2124160] [INFO] Received signal SIGTERM. Shutting down.
[2023-03-23 10:11:01 +0000] [2124160] [INFO] Server Stopped
[2023-03-23 10:11:01 +0000] [2124186] [INFO] Stopping worker [2124186]
[2023-03-23 10:11:01 +0000] [2124185] [INFO] Stopping worker [2124185]

But then, nothing happen. It never start again.

Looking at the process, there are still three process running and never stopping:

[email protected]:~$ ps aux | grep python | grep mult
user 2125387 0.0 0.1 14808 11016 ? S 10:12 0:00 /var/www/project/env/bin/python -c from multiprocessing.resource_tracker import main;main(15)
user 2125388 0.0 0.4 44892 28592 ? S 10:12 0:00 /var/www/project/env/bin/python -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=16, pipe_handle=18) --multiprocessing-fork
user 2125389 0.3 0.3 43528 26360 ? S 10:12 0:00 /var/www/project/env/bin/python -c from multiprocessing.spawn import spawn_main; spawn_main(tracker_fd=16, pipe_handle=20) --multiprocessing-fork

I have to kill -9 them in order to have the server really restart.

Why?

How can I tell Sanic to properly restart?


Running the same above Sanic code in dev mode (debug=true), it handles requests properly, and once I hit CTRL+C, it properly stops process, waits for the requests to finish, then fully stop the instance.

Looking on the web, CTRL+C is the equivalent of SIGINT, but for some reason, when not in debug, Sanic ignores the SIGINT signal.

I’d like a way to properly restart Sanic while waiting for the current requests to finish first. How can I do that?

Thanks in advance!

What version of Sanic? It absolutely does end on SIGINT or SIGTERM. Do you have any code to share?

I went on to try and have a smaller reproducible code, and realized that I had other instances of Sanic running (one for Celery and one for an automatic deployment server), that’s why I still had those multiprocessing-fork instances.

I’ll check if I can make a reproducible issue but hasn’t been able to do so for now. The main issue I’ve been having is that when I try to restart Sanic via Supervisor, I sometimes fall on a “Sanic is already running on this port” issue (and it’s the only one in this port) so somehow, the main process is not properly stopped (or restarted it seems ; when I try to kill it manually via kill -9 it reappears again automatically).

But I’m on 22.9.1, so maybe you made some key changes on how the process are running after?

Oh that could be. Between 22.9 and 22.12 I changed how processes get cleaned up and terminated.

Ok I’ll upgrade and let you know if I still have issues!

Thanks!

1 Like