Skip to content

Commit

Permalink
Merge pull request #1428 from freenas/FIX-35404-stable
Browse files Browse the repository at this point in the history
fix(service): Fix race condition where StartNotify(verb="restart") re…
  • Loading branch information
themylogin committed Jun 22, 2018
2 parents 73949b7 + 7737e8c commit 2e54a29
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/middlewared/middlewared/plugins/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class StartNotify(threading.Thread):
def __init__(self, pidfile, verb, *args, **kwargs):
self._pidfile = pidfile
self._verb = verb

if self._pidfile:
try:
with open(self._pidfile) as f:
self._pid = f.read()
except IOError:
self._pid = None

super(StartNotify, self).__init__(*args, **kwargs)

def run(self):
Expand All @@ -54,11 +62,19 @@ def run(self):
# The file might have been created but it may take a
# little bit for the daemon to write the PID
time.sleep(0.1)
if (
os.path.exists(self._pidfile) and
os.stat(self._pidfile).st_size > 0
):
break
try:
with open(self._pidfile) as f:
pid = f.read()
except IOError:
pid = None

if pid:
if self._verb == 'start':
break
if self._verb == 'restart':
if pid != self._pid:
break
# Otherwise, service has not restarted yet
elif self._verb == "stop" and not os.path.exists(self._pidfile):
break
tries += 1
Expand Down

0 comments on commit 2e54a29

Please sign in to comment.