actix-web
actix-web copied to clipboard
SIGTERM and SIGINT always shut down tokio runtime when using `#[actix::main]`
I have a project where I spawn some background tasks before starting the HttpServer
, and when the server has stopped, I notify the tasks and join them, which means there is still some code running between the server shutdown and the program exit. However, when stopping the server with SIGINT (Ctrl+C) or SIGTERM (using kill), it immediatly shuts down Tokio, which results in errors like error importing requests: Execution Error: error communicating with database: A Tokio 1.x context was found, but it is being shutdown.
from the background tasks. The only thing I found was HttpServer::system_exit
, which is supposed to "[flag] the System to exit after server shutdown", however, in my case I don't call this function and the system shutdown still seems to happen. My current workaround for this is to use #[tokio::main]
instead of #[actix::main]
, but I think it would be nice to have the entire system not shutdown after the server stops.
Hey, thanks for the issue.
I'd be useful to get a trimmed down reproduction of your problem here. I suspect I know what's going on but it's gonna be hard to come up with a good solution for you without one.
Apparently, this bug seems to be a little bit more involved, to a point where I don't know in which project this bug actually occurs. My project uses actix-web and sea-orm and the bug does not occur deterministically. It also has different behaviour depending on the version of sea-orm and whether it is compiled in debug or release. Here is my attempt at a minimal reproduction: https://github.com/tp971/weird-actix-sea_orm-tokio-bug
I believe this is caused by forcing the system shutdown when handling signals in actix-server
:
https://github.com/actix/actix-net/blob/14272a1762680aec65b59ac084819e4bd0d4e4a6/actix-server/src/server.rs#L316-L345
This seems unexpected, at least for the SIGTERM case.