server: explicit signal handler for SIGINT and SIGTERM #3595
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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()
towerkzeug.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 onlyrun_simple()
provides convenient access to the auto-reloader functionality, it would be a lot of copied code to get that back for themake_server()
way.Anyway, I've tested by running this locally, it works :)