When run under a systemd service, the default stop command is to kill the
process; By default, Python doesn't trap sigterm and terminates the process
immediately, so even finally blocks don't run.
This traps SIGTERM and SIGINT (^C) signals and raises an exception, allowing the
process to exit in an orderly fashion.