Fix orderly shutdown on SIGINT, SIGTERM, etc.