Skip to content

Commit

Permalink
Exit from pid=1 only if the child process exits
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Kukushkin committed Nov 14, 2016
1 parent a8a4b39 commit 43ee601
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
5 changes: 1 addition & 4 deletions patroni/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,14 @@ def sigchld_handler(signo, stack_frame):
if ret == (0, 0):
break
elif ret[0] != pid:
logging.info('Reaped unknow pid=%s, exit status=%s', *ret)
logging.info('Reaped pid=%s, exit status=%s', *ret)
except OSError:
pass

def passtochild(signo, stack_frame):
if pid:
os.kill(pid, signo)

if signo in (signal.SIGTERM, signal.SIGINT, signal.SIGQUIT):
sys.exit(0)

signal.signal(signal.SIGCHLD, sigchld_handler)
signal.signal(signal.SIGHUP, passtochild)
signal.signal(signal.SIGINT, passtochild)
Expand Down
14 changes: 5 additions & 9 deletions tests/test_patroni.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,13 @@ def test_patroni_main(self, mock_popen, mock_getpid):
mock_getpid.return_value = 1

def mock_signal(signo, handler):
if signo not in (signal.SIGTERM, signal.SIGINT, signal.SIGQUIT):
handler(signo, None)

with patch('signal.signal', mock_signal), patch('os.waitpid', Mock(side_effect=[(1, 0), (0, 0)])):
_main()

def mock_sigterm(signo, handler):
handler(signo, None)

with patch('signal.signal', mock_sigterm), patch('os.waitpid', Mock(side_effect=OSError)):
self.assertRaises(SystemExit, _main)
with patch('signal.signal', mock_signal):
with patch('os.waitpid', Mock(side_effect=[(1, 0), (0, 0)])):
_main()
with patch('os.waitpid', Mock(side_effect=OSError)):
_main()

ref = {'passtochild': lambda signo, stack_frame: 0}

Expand Down

0 comments on commit 43ee601

Please sign in to comment.