From 1b105e716c0d136389f5663903564d1e12b24e5e Mon Sep 17 00:00:00 2001 From: Adnaan Bheda Date: Mon, 17 Oct 2022 03:01:17 +0530 Subject: [PATCH 1/5] sigquit handling in uvicorn worker --- uvicorn/workers.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/uvicorn/workers.py b/uvicorn/workers.py index c7d16ff6e..44a72d29a 100644 --- a/uvicorn/workers.py +++ b/uvicorn/workers.py @@ -3,6 +3,7 @@ import signal import sys from typing import Any +import threading from gunicorn.arbiter import Arbiter from gunicorn.workers.base import Worker @@ -76,9 +77,22 @@ def init_signals(self) -> None: # Don't let SIGUSR1 disturb active requests by interrupting system calls signal.siginterrupt(signal.SIGUSR1, False) + def _install_sigquit_handler(self) -> None: + """Workaround to install a SIGQUIT handler on workers. + - https://github.com/encode/uvicorn/issues/1116 + - https://github.com/benoitc/gunicorn/issues/2604 + """ + if threading.current_thread() is not threading.main_thread(): + # Signals can only be listened to from the main thread. + return + + loop = asyncio.get_running_loop() + loop.add_signal_handler(signal.SIGQUIT, self.handle_exit, signal.SIGQUIT, None) + async def _serve(self) -> None: self.config.app = self.wsgi server = Server(config=self.config) + self._install_sigquit_handler() await server.serve(sockets=self.sockets) if not server.started: sys.exit(Arbiter.WORKER_BOOT_ERROR) From 06a69d1b84ae3acc0e31715c0d4c1fa7c5a32e8a Mon Sep 17 00:00:00 2001 From: Adnaan Bheda Date: Sat, 29 Oct 2022 04:02:35 +0530 Subject: [PATCH 2/5] check for main thread not required --- uvicorn/workers.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/uvicorn/workers.py b/uvicorn/workers.py index 44a72d29a..860b7318f 100644 --- a/uvicorn/workers.py +++ b/uvicorn/workers.py @@ -3,7 +3,6 @@ import signal import sys from typing import Any -import threading from gunicorn.arbiter import Arbiter from gunicorn.workers.base import Worker @@ -82,9 +81,6 @@ def _install_sigquit_handler(self) -> None: - https://github.com/encode/uvicorn/issues/1116 - https://github.com/benoitc/gunicorn/issues/2604 """ - if threading.current_thread() is not threading.main_thread(): - # Signals can only be listened to from the main thread. - return loop = asyncio.get_running_loop() loop.add_signal_handler(signal.SIGQUIT, self.handle_exit, signal.SIGQUIT, None) From 2b348ef68b062f5b3de3436d2701d2e717935f04 Mon Sep 17 00:00:00 2001 From: Adnaan Bheda Date: Sat, 29 Oct 2022 04:31:09 +0530 Subject: [PATCH 3/5] close the sockets before closing down the servers --- uvicorn/server.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uvicorn/server.py b/uvicorn/server.py index 494e2b658..8f2f230c2 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -252,11 +252,12 @@ async def on_tick(self, counter: int) -> bool: async def shutdown(self, sockets: Optional[List[socket.socket]] = None) -> None: logger.info("Shutting down") + # Close the sockets first before closing the servers + for sock in sockets or []: + sock.close() # Stop accepting new connections. for server in self.servers: server.close() - for sock in sockets or []: - sock.close() for server in self.servers: await server.wait_closed() From 6d5c58a4ba11d305b7e11cd9578b97100ac911d4 Mon Sep 17 00:00:00 2001 From: Adnaan Bheda Date: Mon, 31 Oct 2022 19:17:53 +0530 Subject: [PATCH 4/5] bRevert "close the sockets before closing down the servers" This reverts commit 2b348ef68b062f5b3de3436d2701d2e717935f04. --- uvicorn/server.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/uvicorn/server.py b/uvicorn/server.py index 8f2f230c2..494e2b658 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -252,12 +252,11 @@ async def on_tick(self, counter: int) -> bool: async def shutdown(self, sockets: Optional[List[socket.socket]] = None) -> None: logger.info("Shutting down") - # Close the sockets first before closing the servers - for sock in sockets or []: - sock.close() # Stop accepting new connections. for server in self.servers: server.close() + for sock in sockets or []: + sock.close() for server in self.servers: await server.wait_closed() From aa2431bc472ba4d4692e5368330fbd0f01f1aa7b Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Tue, 1 Nov 2022 08:51:06 +0100 Subject: [PATCH 5/5] Update uvicorn/workers.py --- uvicorn/workers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/uvicorn/workers.py b/uvicorn/workers.py index 860b7318f..73caf073d 100644 --- a/uvicorn/workers.py +++ b/uvicorn/workers.py @@ -77,7 +77,8 @@ def init_signals(self) -> None: signal.siginterrupt(signal.SIGUSR1, False) def _install_sigquit_handler(self) -> None: - """Workaround to install a SIGQUIT handler on workers. + """Install a SIGQUIT handler on workers. + - https://github.com/encode/uvicorn/issues/1116 - https://github.com/benoitc/gunicorn/issues/2604 """