From b264764bbcbe2262f8b58fcc9fa75b80164caead Mon Sep 17 00:00:00 2001 From: wanghui Date: Mon, 16 Jan 2017 19:40:46 +0800 Subject: [PATCH] Fix start duplicate supervisord will remove exists pidfile --- supervisor/options.py | 7 ++++++- supervisor/tests/test_options.py | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/supervisor/options.py b/supervisor/options.py index 87d83e4f8..8679f9fa2 100644 --- a/supervisor/options.py +++ b/supervisor/options.py @@ -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). """ @@ -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): @@ -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: diff --git a/supervisor/tests/test_options.py b/supervisor/tests/test_options.py index 815ffa963..1a7267829 100644 --- a/supervisor/tests/test_options.py +++ b/supervisor/tests/test_options.py @@ -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: @@ -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: