From 9a1fe9f295741c110a71fef3911cd4e0e1e793af Mon Sep 17 00:00:00 2001 From: Beau Barker Date: Wed, 31 May 2023 11:13:46 +1000 Subject: [PATCH] Always stop the server when exiting serve() Closes #264 --- jsonrpcserver/server.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/jsonrpcserver/server.py b/jsonrpcserver/server.py index 643b6a7..ec0ddb8 100644 --- a/jsonrpcserver/server.py +++ b/jsonrpcserver/server.py @@ -25,4 +25,11 @@ def do_POST(self) -> None: # pylint: disable=invalid-name def serve(name: str = "", port: int = 5000) -> None: """A simple function to serve HTTP requests""" logging.info(" * Listening on port %s", port) - HTTPServer((name, port), RequestHandler).serve_forever() + try: + httpd = HTTPServer((name, port), RequestHandler) + httpd.serve_forever() + except KeyboardInterrupt: + pass + except Exception: + httpd.shutdown() + raise