Skip to content

Commit

Permalink
🚑 Check for a replaced PID file in join
Browse files Browse the repository at this point in the history
This patch works around an issue described in
#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.
  • Loading branch information
webknjaz committed Mar 13, 2021
1 parent f9da977 commit 3c9af1b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions magicbus/plugins/opsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions magicbus/test/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c9af1b

Please sign in to comment.