Skip to content

Commit

Permalink
Fix start duplicate supervisord will remove exists pidfile
Browse files Browse the repository at this point in the history
  • Loading branch information
coldnight committed Jan 17, 2017
1 parent 8be5bc1 commit b264764
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion supervisor/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ def __init__(self):
self.signal_receiver = SignalReceiver()
self.poller = poller.Poller(self)

self._pidfile_wrote = False

def version(self, dummy):
"""Print version to stdout and exit(0).
"""
Expand Down Expand Up @@ -1151,6 +1153,7 @@ def write_pidfile(self):
except (IOError, OSError):
self.logger.critical('could not write pidfile %s' % self.pidfile)
else:
self._pidfile_wrote = True
self.logger.info('supervisord started with pid %s' % pid)

def cleanup(self):
Expand All @@ -1159,7 +1162,9 @@ def cleanup(self):
if self.unlink_socketfiles:
socketname = config['file']
self._try_unlink(socketname)
self._try_unlink(self.pidfile)

if self._pidfile_wrote:
self._try_unlink(self.pidfile)

def _try_unlink(self, path):
try:
Expand Down
21 changes: 21 additions & 0 deletions supervisor/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,25 @@ def test_options_afinet_no_port(self):
self.assertEqual(exc.args[0],
"section [inet_http_server] has no port value")

def test_cleanup_ignore_pidfile(self):
fn = tempfile.mktemp()

with open(fn, 'w') as f:
f.write('1234')

try:
instance = self._makeOne()
instance.pidfile = fn

# cleanup without write pidfile
instance.cleanup()
self.assertTrue(os.path.exists(fn))
finally:
try:
os.unlink(fn)
except OSError:
pass

def test_cleanup_afunix_unlink(self):
fn = tempfile.mktemp()
with open(fn, 'w') as f:
Expand Down Expand Up @@ -1357,6 +1376,8 @@ def test_cleanup_removes_pidfile(self):
f.write('2')
instance = self._makeOne()
instance.pidfile = pidfile
instance.logger = DummyLogger()
instance.write_pidfile()
instance.cleanup()
self.assertFalse(os.path.exists(pidfile))
finally:
Expand Down

0 comments on commit b264764

Please sign in to comment.