From 5af7a3531ef0171e98df5e7d2508151406478925 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Wed, 11 Mar 2020 21:03:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=91=20Check=20for=20a=20replaced=20PID?= =?UTF-8?q?=20file=20in=20join?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch works around an issue described in cherrypy/magicbus#7 by also comparing the stat of PID files. Otherwise it misses cases when PID file gets recreated faster that the check loop hits another existence check. --- magicbus/plugins/opsys.py | 12 ++++++++++-- magicbus/test/test_signals.py | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/magicbus/plugins/opsys.py b/magicbus/plugins/opsys.py index d2fd5ad2..b39566e8 100644 --- a/magicbus/plugins/opsys.py +++ b/magicbus/plugins/opsys.py @@ -261,8 +261,16 @@ def wait(self, timeout=None, poll_interval=0.1): def join(self, timeout=None, poll_interval=0.1): """Return when the PID file does not exist, or the timeout expires.""" + try: + initial_stat = os.stat(self.pidfile) + except OSError: # file does not exist + return + starttime = time.time() while timeout is None or time.time() - starttime <= timeout: - if not os.path.exists(self.pidfile): - return + try: + if initial_stat != os.stat(self.pidfile): + return # file has been changed/replaced + except OSError: + return # file does not exist time.sleep(poll_interval) diff --git a/magicbus/test/test_signals.py b/magicbus/test/test_signals.py index dcde1f5d..4b87a7eb 100644 --- a/magicbus/test/test_signals.py +++ b/magicbus/test/test_signals.py @@ -56,6 +56,7 @@ def test_SIGHUP_daemonized(self): kill(pid, SIGHUP) # Give the server some time to restart + pidfile.join() time.sleep(1) new_pid = pidfile.wait(5) assertNotEqual(new_pid, None)