diff --git a/uvicorn/_subprocess.py b/uvicorn/_subprocess.py index 5ac8ade47..36b369256 100644 --- a/uvicorn/_subprocess.py +++ b/uvicorn/_subprocess.py @@ -75,5 +75,10 @@ def subprocess_started( # Logging needs to be setup again for each child. config.configure_logging() - # Now we can call into `Server.run(sockets=sockets)` - target(sockets=sockets) + try: + # Now we can call into `Server.run(sockets=sockets)` + target(sockets=sockets) + except KeyboardInterrupt: # pragma: no cover + # supress the exception to avoid a traceback from subprocess.Popen + # the parent already expects us to end, so no vital information is lost + pass diff --git a/uvicorn/main.py b/uvicorn/main.py index b9d5fd1c0..41e9ec84e 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -566,16 +566,18 @@ def run( logger.warning("You must pass the application as an import string to enable 'reload' or " "'workers'.") sys.exit(1) - if config.should_reload: - sock = config.bind_socket() - ChangeReload(config, target=server.run, sockets=[sock]).run() - elif config.workers > 1: - sock = config.bind_socket() - Multiprocess(config, target=server.run, sockets=[sock]).run() - else: - server.run() - if config.uds and os.path.exists(config.uds): - os.remove(config.uds) # pragma: py-win32 + try: + if config.should_reload: + sock = config.bind_socket() + ChangeReload(config, target=server.run, sockets=[sock]).run() + elif config.workers > 1: + sock = config.bind_socket() + Multiprocess(config, target=server.run, sockets=[sock]).run() + else: + server.run() + finally: + if config.uds and os.path.exists(config.uds): + os.remove(config.uds) # pragma: py-win32 if not server.started and not config.should_reload and config.workers == 1: sys.exit(STARTUP_FAILURE)