The server only shuts down when exiting with ctrl-c (keyboard interrupt). Add a try block to always shut down the server when exiting other ways. e.g sys.exit