From c6f96b2a8f16319db727785bbd24e9de43a45ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20BERSAC?= Date: Tue, 5 Nov 2024 13:52:56 +0100 Subject: [PATCH 1/2] Check parent in subprocess only --- CHANGELOG.md | 3 ++- agent/temboardagent/web/service.py | 6 ------ ui/temboardui/toolkit/services.py | 2 ++ ui/temboardui/toolkit/syncio.py | 21 +++++++++++++-------- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index feb153adf..dc458ed6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Ensure you use consistent title format. ## UNRELEASED - Fix register-instance CLI. +- Fix running daemonized. - Raise proper error on unknown environment. - Improve alerting performance. @@ -27,7 +28,7 @@ Ensure you use consistent title format. - Drop rhel7 and buster support, minimum versions are now 6.0.2 for tornado and 1.3.2 for sqlalchemy. - Drop plugin hotplug. Just restart temBoard UI or agent. -- Drop daemonization. Use nohup or systemd. +- Drop daemonization. Use systemd. **Other changes** diff --git a/agent/temboardagent/web/service.py b/agent/temboardagent/web/service.py index d1de50297..9b602f3e8 100644 --- a/agent/temboardagent/web/service.py +++ b/agent/temboardagent/web/service.py @@ -15,12 +15,6 @@ class HTTPDService(syncio.Service): name = "web" - def __init__(self, app): - self.app = app - - def __str__(self): - return self.name - # for services.run def setup(self, *_, **__): ServerHandler.server_software = "temBoard-agent/%s" % __version__ diff --git a/ui/temboardui/toolkit/services.py b/ui/temboardui/toolkit/services.py index 8dc69046e..f91f3f982 100644 --- a/ui/temboardui/toolkit/services.py +++ b/ui/temboardui/toolkit/services.py @@ -173,6 +173,8 @@ def fork(self, service): if hasattr(service, "command"): execute(service) else: + # Enable parent pid check. + service.ppid = os.getppid() os._exit(run(service)) def _read_pids(self): diff --git a/ui/temboardui/toolkit/syncio.py b/ui/temboardui/toolkit/syncio.py index 35a76eb3b..3fb80da48 100644 --- a/ui/temboardui/toolkit/syncio.py +++ b/ui/temboardui/toolkit/syncio.py @@ -7,19 +7,20 @@ class Service: - # Mixin for scheduler and worker pool services + # Mixin for scheduler, worker pool and agent httpd services # Manages sync loop and signals. name = None def __init__(self, app): self.app = app + self.ppid = None def __str__(self): return self.name # interface for services.run def create_loop(self): - return Loop(self) + return Loop(self, self.ppid) # Interface for SignalMultiplexer def sighup_handler(self, *a): @@ -27,10 +28,12 @@ def sighup_handler(self, *a): class Loop: - def __init__(self, service): + def __init__(self, service, ppid=None): self.service = service self.signalmngr = SignalManager() self.running = False + # If not None, enable parent pid check. + self.ppid = ppid def __repr__(self): return "" % self.service @@ -49,10 +52,9 @@ def remove_signal_handler(self, sig): # Interface for services.run def start(self): with self.signalmngr: - ppid = os.getppid() self.running = True while self.running: - if self._stop_with_parent(ppid): + if self._stop_with_parent(): break self.service.accept() @@ -63,10 +65,13 @@ def stop(self): def close(self): self.running = False - def _stop_with_parent(self, ppid): - self.running = os.getppid() == ppid + def _stop_with_parent(self): + if self.ppid is None: # Don't check parent. + return + # If ppid changed (parent exited), stop the loop. + self.running = os.getppid() == self.ppid if not self.running: - logger.info("Parent process exited. Exiting. ppid=%s", ppid) + logger.info("Parent process exited. Exiting. ppid=%s", self.ppid) return not self.running From 1b0de8da888c3d6cb8acf43cc30e48ac8a36e7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20BERSAC?= Date: Fri, 8 Nov 2024 09:13:23 +0100 Subject: [PATCH 2/2] Fix successive SIGCHLD Fixes: UnboundLocalError: local variable 'pid' referenced before assignment --- ui/temboardui/toolkit/services.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/temboardui/toolkit/services.py b/ui/temboardui/toolkit/services.py index f91f3f982..23753fb0b 100644 --- a/ui/temboardui/toolkit/services.py +++ b/ui/temboardui/toolkit/services.py @@ -265,7 +265,7 @@ def sigchld_handler(self, *a): if pid == 0: # Still alive continue except ChildProcessError: - pass + pid = self.pids.get("name", "") logger.warning( "Background service dead. Restarting. service=%s pid=%s", name, pid