From afde8c5e1ac0182c63838bb1670f1ebf6d234fcf 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 4be4ff3c..7a1c8335 100644 --- a/magicbus/plugins/opsys.py +++ b/magicbus/plugins/opsys.py @@ -265,8 +265,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 45e54a5d..f9f9f619 100644 --- a/magicbus/test/test_signals.py +++ b/magicbus/test/test_signals.py @@ -57,6 +57,7 @@ def test_SIGHUP_daemonized(self): kill(pid, SIGHUP) # Give the server some time to restart + pidfile.join() time.sleep(1) for _ in range(6): new_pid = pidfile.wait(5)