From 310aed2d5de47235e362b7a82f48e63f4b9fd3b9 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Mon, 27 Dec 2021 09:49:03 +0530 Subject: [PATCH 1/5] sys.exit on SIGINT, SIGHUP, SIGTERM --- proxy/proxy.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/proxy/proxy.py b/proxy/proxy.py index c35e9e0b4a..125d028171 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -11,7 +11,9 @@ import os import sys import time +import signal import logging +from types import FrameType from typing import List, Optional, Any @@ -190,6 +192,7 @@ def setup(self) -> None: ) self.acceptors.setup() # TODO: May be close listener fd as we don't need it now + self._register_signals() def shutdown(self) -> None: assert self.acceptors @@ -215,18 +218,32 @@ def _delete_pid_file(self) -> None: if self.flags.pid_file and os.path.exists(self.flags.pid_file): os.remove(self.flags.pid_file) + def _register_signals(self) -> None: + signal.signal(signal.SIGINT, self._handle_signal) + signal.signal(signal.SIGHUP, self._handle_signal) + signal.signal(signal.SIGTERM, self._handle_signal) + + @staticmethod + def _handle_signal(signum: int, _frame: Optional[FrameType]) -> None: + logger.info('Received signal %d' % signum) + sys.exit(0) + @property def remote_executors_enabled(self) -> bool: return self.flags.threadless and not self.flags.local_executor +def sleep_loop() -> None: + while True: + try: + time.sleep(1) + except KeyboardInterrupt: + break + + def main(**opts: Any) -> None: with Proxy(sys.argv[1:], **opts): - while True: - try: - time.sleep(1) - except KeyboardInterrupt: - break + sleep_loop() def entry_point() -> None: From 144dae1f4fc114676cab1e9bc3e8e248d978921e Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Mon, 27 Dec 2021 10:12:00 +0530 Subject: [PATCH 2/5] Add todo for pending signal actions --- proxy/proxy.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/proxy/proxy.py b/proxy/proxy.py index 125d028171..3790114147 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -207,6 +207,10 @@ def shutdown(self) -> None: self.listener.shutdown() self._delete_pid_file() + @property + def remote_executors_enabled(self) -> bool: + return self.flags.threadless and not self.flags.local_executor + def _write_pid_file(self) -> None: if self.flags.pid_file is not None: # NOTE: Multiple instances of proxy.py running on @@ -219,19 +223,18 @@ def _delete_pid_file(self) -> None: os.remove(self.flags.pid_file) def _register_signals(self) -> None: - signal.signal(signal.SIGINT, self._handle_signal) - signal.signal(signal.SIGHUP, self._handle_signal) - signal.signal(signal.SIGTERM, self._handle_signal) + signal.signal(signal.SIGINT, self._handle_exit_signal) + signal.signal(signal.SIGHUP, self._handle_exit_signal) + # TODO: SIGQUIT is ideally meant for terminate with core dumps + signal.signal(signal.SIGQUIT, self._handle_exit_signal) + signal.signal(signal.SIGTERM, self._handle_exit_signal) + # TODO: SIGINFO, SIGUSR1, SIGUSR2 @staticmethod - def _handle_signal(signum: int, _frame: Optional[FrameType]) -> None: + def _handle_exit_signal(signum: int, _frame: Optional[FrameType]) -> None: logger.info('Received signal %d' % signum) sys.exit(0) - @property - def remote_executors_enabled(self) -> bool: - return self.flags.threadless and not self.flags.local_executor - def sleep_loop() -> None: while True: From 888e9add2cbf99eed37209fecf43a3487a981745 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Mon, 27 Dec 2021 12:48:19 +0530 Subject: [PATCH 3/5] SIGHUP is win only --- proxy/proxy.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/proxy/proxy.py b/proxy/proxy.py index 0cdffd93e2..6051871697 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -21,7 +21,7 @@ from .core.event import EventManager from .common.utils import bytes_ from .common.flag import FlagParser, flags -from .common.constants import DEFAULT_LOCAL_EXECUTOR, DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT, DEFAULT_LOG_LEVEL +from .common.constants import DEFAULT_LOCAL_EXECUTOR, DEFAULT_LOG_FILE, DEFAULT_LOG_FORMAT, DEFAULT_LOG_LEVEL, IS_WINDOWS from .common.constants import DEFAULT_OPEN_FILE_LIMIT, DEFAULT_PLUGINS, DEFAULT_VERSION from .common.constants import DEFAULT_ENABLE_DASHBOARD, DEFAULT_WORK_KLASS, DEFAULT_PID_FILE @@ -224,7 +224,8 @@ def _delete_pid_file(self) -> None: def _register_signals(self) -> None: signal.signal(signal.SIGINT, self._handle_exit_signal) - signal.signal(signal.SIGHUP, self._handle_exit_signal) + if not IS_WINDOWS: + signal.signal(signal.SIGHUP, self._handle_exit_signal) # TODO: SIGQUIT is ideally meant for terminate with core dumps signal.signal(signal.SIGQUIT, self._handle_exit_signal) signal.signal(signal.SIGTERM, self._handle_exit_signal) From d6403ab38b9ff895dbf246b69c45edb5285d56ed Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Mon, 27 Dec 2021 12:50:42 +0530 Subject: [PATCH 4/5] Remove frametype signature as it causes lint issues and we are not using it anyways --- proxy/proxy.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/proxy/proxy.py b/proxy/proxy.py index 6051871697..f67d9ea675 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -13,7 +13,6 @@ import time import signal import logging -from types import FrameType from typing import List, Optional, Any @@ -232,7 +231,7 @@ def _register_signals(self) -> None: # TODO: SIGINFO, SIGUSR1, SIGUSR2 @staticmethod - def _handle_exit_signal(signum: int, _frame: Optional[FrameType]) -> None: + def _handle_exit_signal(signum: int, _frame: Any) -> None: logger.info('Received signal %d' % signum) sys.exit(0) From e13165c4e682501cf8b516d082907fdc25f4a696 Mon Sep 17 00:00:00 2001 From: Abhinav Singh Date: Mon, 27 Dec 2021 13:00:46 +0530 Subject: [PATCH 5/5] SIGQUIT is not on Win --- proxy/proxy.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/proxy/proxy.py b/proxy/proxy.py index f67d9ea675..43a90593af 100644 --- a/proxy/proxy.py +++ b/proxy/proxy.py @@ -222,13 +222,13 @@ def _delete_pid_file(self) -> None: os.remove(self.flags.pid_file) def _register_signals(self) -> None: + # TODO: Handle SIGINFO, SIGUSR1, SIGUSR2 signal.signal(signal.SIGINT, self._handle_exit_signal) + signal.signal(signal.SIGTERM, self._handle_exit_signal) if not IS_WINDOWS: signal.signal(signal.SIGHUP, self._handle_exit_signal) - # TODO: SIGQUIT is ideally meant for terminate with core dumps - signal.signal(signal.SIGQUIT, self._handle_exit_signal) - signal.signal(signal.SIGTERM, self._handle_exit_signal) - # TODO: SIGINFO, SIGUSR1, SIGUSR2 + # TODO: SIGQUIT is ideally meant for terminate with core dumps + signal.signal(signal.SIGQUIT, self._handle_exit_signal) @staticmethod def _handle_exit_signal(signum: int, _frame: Any) -> None: