Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: explicit signal handler for SIGINT and SIGTERM #3595

Merged
merged 1 commit into from
Jan 19, 2021
Merged

server: explicit signal handler for SIGINT and SIGTERM #3595

merged 1 commit into from
Jan 19, 2021

Conversation

ploxiln
Copy link
Contributor

@ploxiln ploxiln commented Jan 19, 2021

When running alone as pid 1 in a container, most signals that would
cause the process to exit by default, are instead ignored by default.
The container runtime will send SIGTERM, wait 10 seconds, then send
SIGKILL, which will work, but moto.server can exit much faster if
it has an explicit SIGTERM handler.


An alternative way to fix the slow-container-stop problem is to use a separate "init" process, which forwards signals to the actual container command running as a child process (which then has PID != 1). You can get very easily get this with "docker run --init ..." which uses the "tini" init process included in docker ... but kubernetes doesn't offer this option, and the separate init process isn't really necessary.


If I could switch from werkzeug.serving.run_simple() to werkzeug.serving.make_server(), it would be more elegant: I could probably use the server object's log method and shutdown method (though I might need to spawn a thread for that too, long story). But only run_simple() provides convenient access to the auto-reloader functionality, it would be a lot of copied code to get that back for the make_server() way.

Anyway, I've tested by running this locally, it works :)

When running alone as pid 1 in a container, most signals that would
cause the process to exit by default, are instead ignored by default.
The container runtime will send SIGTERM, wait 10 seconds, then send
SIGKILL, which will work, but moto.server can exit much faster if
it has an explicit SIGTERM handler.
@coveralls
Copy link

coveralls commented Jan 19, 2021

Coverage Status

Coverage decreased (-0.004%) to 94.61% when pulling 908fa1c on ploxiln:server_signal_exit into 02ac5ca on spulec:master.

Copy link
Collaborator

@bblommers bblommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Thanks @ploxiln!

@bblommers bblommers merged commit 2bdba88 into getmoto:master Jan 19, 2021
@ploxiln
Copy link
Contributor Author

ploxiln commented Jan 19, 2021

Thank you for maintaining this useful tool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants