From a6e2207ec0f97a319fd3ac780e306df40cbb4b6d Mon Sep 17 00:00:00 2001 From: xianml Date: Wed, 13 Nov 2024 13:14:58 +0800 Subject: [PATCH 1/3] fix: make sure always execute the shutdown logic, even if server received a signal during startup --- uvicorn/server.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uvicorn/server.py b/uvicorn/server.py index f14026f16..e089f3240 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -82,8 +82,9 @@ async def _serve(self, sockets: list[socket.socket] | None = None) -> None: logger.info(message, process_id, extra={"color_message": color_message}) await self.startup(sockets=sockets) - if self.should_exit: - return + # FIX: make sure always execute the shutdown logic, even if server received a signal during startup + # if self.should_exit: + # return await self.main_loop() await self.shutdown(sockets=sockets) From 152da33de80524157cfe408bf9e7bffd519a59d2 Mon Sep 17 00:00:00 2001 From: xianml Date: Thu, 21 Nov 2024 22:31:59 +0800 Subject: [PATCH 2/3] fix: test case error --- uvicorn/server.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uvicorn/server.py b/uvicorn/server.py index e089f3240..852617fc2 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -263,8 +263,9 @@ async def shutdown(self, sockets: list[socket.socket] | None = None) -> None: logger.info("Shutting down") # Stop accepting new connections. - for server in self.servers: - server.close() + if hasattr(self, "servers") and self.servers: + for server in self.servers: + server.close() for sock in sockets or []: sock.close() # pragma: full coverage From 05b1ec22223d9efd1527536dfda49ffe62dfc243 Mon Sep 17 00:00:00 2001 From: xianml Date: Thu, 21 Nov 2024 22:49:22 +0800 Subject: [PATCH 3/3] fix: test case --- uvicorn/server.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uvicorn/server.py b/uvicorn/server.py index 852617fc2..048b00205 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -310,8 +310,9 @@ async def _wait_tasks_to_complete(self) -> None: while self.server_state.tasks and not self.force_exit: await asyncio.sleep(0.1) - for server in self.servers: - await server.wait_closed() + if hasattr(self, "servers") and self.servers: + for server in self.servers: + await server.wait_closed() @contextlib.contextmanager def capture_signals(self) -> Generator[None, None, None]: