From 90acef9a309db8948f0a35a013a71dc260027b29 Mon Sep 17 00:00:00 2001 From: Scott Wilson Date: Mon, 7 Jan 2013 19:51:58 -0800 Subject: [PATCH 1/7] Quit child processes when supervisor crashes (Linux only) Use prctl(PR_SET_PDEATHSIG, SIGTERM) to send signal when parent dies --- supervisor/process.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/supervisor/process.py b/supervisor/process.py index a724b3ca8..b3eb470a7 100644 --- a/supervisor/process.py +++ b/supervisor/process.py @@ -23,6 +23,7 @@ from supervisor import events from supervisor.datatypes import RestartUnconditionally +from supervisor.datatypes import signal_number from supervisor.socket_manager import SocketManager @@ -284,6 +285,19 @@ def _spawn_as_child(self, filename, argv): # Presumably it also prevents HUP, etc received by # supervisord from being sent to children. options.setpgrp() + + # Send this process a stop signal if supervisor crashes. + # Uses system call prctl(PR_SET_PDEATHSIG, ). + # This will only work on Linux. + try: + import ctypes + import ctypes.util + libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')) + libc.prctl(1, signal_number(self.config.stopsignal)) + except Exception, e: + options.logger.debug("Could not set parent death signal. " + "This is expected if not running on Linux.") + self._prepare_child_fds() # sending to fd 2 will put this output in the stderr log msg = self.set_uid() From d32be887d04f496489bbf7616a91b5ea1f90696e Mon Sep 17 00:00:00 2001 From: Scott Wilson Date: Thu, 18 Apr 2013 18:03:47 -0700 Subject: [PATCH 2/7] ENG-8730 PDEATHSIG is SIGKILL instead of SIGTERM --- supervisor/process.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supervisor/process.py b/supervisor/process.py index b3eb470a7..43d918cb4 100644 --- a/supervisor/process.py +++ b/supervisor/process.py @@ -286,14 +286,14 @@ def _spawn_as_child(self, filename, argv): # supervisord from being sent to children. options.setpgrp() - # Send this process a stop signal if supervisor crashes. + # Send this process a kill signal if supervisor crashes. # Uses system call prctl(PR_SET_PDEATHSIG, ). # This will only work on Linux. try: import ctypes import ctypes.util libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')) - libc.prctl(1, signal_number(self.config.stopsignal)) + libc.prctl(1, signal.SIGKILL) except Exception, e: options.logger.debug("Could not set parent death signal. " "This is expected if not running on Linux.") From d459858918d176bfc813a1a1e085d312eb9d4570 Mon Sep 17 00:00:00 2001 From: Luke Weber Date: Tue, 18 Aug 2015 16:21:55 -0700 Subject: [PATCH 3/7] Only run linux code if linux --- supervisor/process.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/supervisor/process.py b/supervisor/process.py index f27a86b92..fbec79902 100644 --- a/supervisor/process.py +++ b/supervisor/process.py @@ -1,3 +1,5 @@ +import platform +import sys import os import time import errno @@ -297,14 +299,15 @@ def _spawn_as_child(self, filename, argv): # Send this process a kill signal if supervisor crashes. # Uses system call prctl(PR_SET_PDEATHSIG, ). # This will only work on Linux. - try: - import ctypes - import ctypes.util - libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')) - libc.prctl(1, signal.SIGKILL) - except Exception, e: - options.logger.debug("Could not set parent death signal. " - "This is expected if not running on Linux.") + if sys.platform.startswith("linux"): + try: + import ctypes + import ctypes.util + libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')) + PR_SET_PDEATHSIG = 1 + libc.prctl(PR_SET_PDEATHSIG, signal.SIGKILL) + except Exception: + options.logger.debug("Could not set parent death signal.") self._prepare_child_fds() # sending to fd 2 will put this output in the stderr log From 2bfa0bb7ddc407590f25ceec04576610cc9fcce5 Mon Sep 17 00:00:00 2001 From: Luke Weber Date: Tue, 18 Aug 2015 16:22:50 -0700 Subject: [PATCH 4/7] remove unused import --- supervisor/process.py | 1 - 1 file changed, 1 deletion(-) diff --git a/supervisor/process.py b/supervisor/process.py index fbec79902..5d0bbc558 100644 --- a/supervisor/process.py +++ b/supervisor/process.py @@ -1,4 +1,3 @@ -import platform import sys import os import time From c9a39d08e837224b762653fb1780762ed898b198 Mon Sep 17 00:00:00 2001 From: Luke Weber Date: Tue, 18 Aug 2015 16:33:19 -0700 Subject: [PATCH 5/7] Add constant outside of class, remove unused import --- supervisor/process.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/supervisor/process.py b/supervisor/process.py index 5d0bbc558..ee81aa962 100644 --- a/supervisor/process.py +++ b/supervisor/process.py @@ -26,10 +26,12 @@ from supervisor import events from supervisor.datatypes import RestartUnconditionally -from supervisor.datatypes import signal_number from supervisor.socket_manager import SocketManager +# Constant from http://linux.die.net/include/linux/prctl.h +PR_SET_PDEATHSIG = 1 + @total_ordering class Subprocess(object): @@ -303,7 +305,6 @@ def _spawn_as_child(self, filename, argv): import ctypes import ctypes.util libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')) - PR_SET_PDEATHSIG = 1 libc.prctl(PR_SET_PDEATHSIG, signal.SIGKILL) except Exception: options.logger.debug("Could not set parent death signal.") From d81f7db2da8d6a58330f1a726609621163a48045 Mon Sep 17 00:00:00 2001 From: Luke Weber Date: Tue, 25 Aug 2015 22:25:06 -0700 Subject: [PATCH 6/7] Add prsetpdeathsig as a linux only program config option --- supervisor/options.py | 14 ++++++- supervisor/process.py | 10 ++--- supervisor/tests/base.py | 3 +- supervisor/tests/test_options.py | 68 ++++++++++++++++++++++++++++++-- 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/supervisor/options.py b/supervisor/options.py index 563b8a547..23f55c168 100644 --- a/supervisor/options.py +++ b/supervisor/options.py @@ -900,6 +900,15 @@ def get(section, opt, *args, **kwargs): process_name = process_or_group_name( get(section, 'process_name', '%(program_name)s', do_expand=False)) + prsetpdeathsig = get(section, 'prsetpdeathsig', None) + if prsetpdeathsig is not None: + if sys.platform.startswith("linux"): + prsetpdeathsig = signal_number(prsetpdeathsig) + else: + raise ValueError( + "Cannot set prsetpdeathsig on non-linux os" + ) + if numprocs > 1: if not '%(process_num)' in process_name: # process_name needs to include process_num when we @@ -987,7 +996,8 @@ def get(section, opt, *args, **kwargs): exitcodes=exitcodes, redirect_stderr=redirect_stderr, environment=environment, - serverurl=serverurl) + serverurl=serverurl, + prsetpdeathsig=prsetpdeathsig) programs.append(pconfig) @@ -1776,7 +1786,7 @@ class ProcessConfig(Config): 'stderr_logfile_backups', 'stderr_logfile_maxbytes', 'stderr_events_enabled', 'stderr_syslog', 'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup', - 'exitcodes', 'redirect_stderr' ] + 'exitcodes', 'redirect_stderr', 'prsetpdeathsig' ] optional_param_names = [ 'environment', 'serverurl' ] def __init__(self, options, **params): diff --git a/supervisor/process.py b/supervisor/process.py index ee81aa962..cf6a2a799 100644 --- a/supervisor/process.py +++ b/supervisor/process.py @@ -29,9 +29,6 @@ from supervisor.socket_manager import SocketManager -# Constant from http://linux.die.net/include/linux/prctl.h -PR_SET_PDEATHSIG = 1 - @total_ordering class Subprocess(object): @@ -300,12 +297,15 @@ def _spawn_as_child(self, filename, argv): # Send this process a kill signal if supervisor crashes. # Uses system call prctl(PR_SET_PDEATHSIG, ). # This will only work on Linux. - if sys.platform.startswith("linux"): + if self.config.prsetpdeathsig is not None \ + and sys.platform.startswith("linux"): + # Constant from http://linux.die.net/include/linux/prctl.h + PR_SET_PDEATHSIG = 1 try: import ctypes import ctypes.util libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('c')) - libc.prctl(PR_SET_PDEATHSIG, signal.SIGKILL) + libc.prctl(PR_SET_PDEATHSIG, self.config.prsetpdeathsig) except Exception: options.logger.debug("Could not set parent death signal.") diff --git a/supervisor/tests/base.py b/supervisor/tests/base.py index 94932f3b6..23cca42a5 100644 --- a/supervisor/tests/base.py +++ b/supervisor/tests/base.py @@ -516,7 +516,7 @@ def __init__(self, options, name, command, directory=None, umask=None, stderr_logfile_backups=0, stderr_logfile_maxbytes=0, redirect_stderr=False, stopsignal=None, stopwaitsecs=10, stopasgroup=False, killasgroup=False, - exitcodes=(0,2), environment=None, serverurl=None): + exitcodes=(0,2), environment=None, serverurl=None, prsetpdeathsig=None): self.options = options self.name = name self.command = command @@ -550,6 +550,7 @@ def __init__(self, options, name, command, directory=None, umask=None, self.umask = umask self.autochildlogs_created = False self.serverurl = serverurl + self.prsetpdeathsig = prsetpdeathsig def create_autochildlogs(self): self.autochildlogs_created = True diff --git a/supervisor/tests/test_options.py b/supervisor/tests/test_options.py index 88cb0c014..93a6757bc 100644 --- a/supervisor/tests/test_options.py +++ b/supervisor/tests/test_options.py @@ -1423,6 +1423,68 @@ def test_processes_from_section_host_node_name_expansion(self): expected = "/bin/foo --host=" + platform.node() self.assertEqual(pconfigs[0].command, expected) + def test_processes_from_section_prsetdeathsig_error(self): + instance = self._makeOne() + text = lstrip("""\ + [program:foo] + command = /bin/foo + prsetpdeathsig = SIGKILL + """) + from supervisor.options import UnhosedConfigParser + config = UnhosedConfigParser() + config.read_string(text) + + platform_mock = Mock() + platform_mock.return_value = "darwin" + @patch('sys.platform', platform_mock) + def parse_config(instance, config): + instance.processes_from_section(config, 'program:foo', 'bar') + + try: + parse_config(instance, config) + except ValueError as exc: + self.assertTrue(exc.args[0].startswith( + 'Cannot set prsetpdeathsig on non-linux os in section')) + + def test_processes_from_section_prsetdeathsig_linux(self): + instance = self._makeOne() + text = lstrip("""\ + [program:foo] + command = /bin/foo + prsetpdeathsig = SIGKILL + """) + from supervisor.options import UnhosedConfigParser + config = UnhosedConfigParser() + config.read_string(text) + + platform_mock = Mock() + platform_mock.return_value = "linux" + @patch('sys.platform', platform_mock) + def parse_config(instance, config): + return instance.processes_from_section(config, 'program:foo', 'bar') + + pconfig = parse_config(instance, config) + self.assertEqual(pconfig[0].prsetpdeathsig, signal.SIGKILL) + + def test_processes_from_section_prsetdeathsig_linux_default(self): + instance = self._makeOne() + text = lstrip("""\ + [program:foo] + command = /bin/foo + """) + from supervisor.options import UnhosedConfigParser + config = UnhosedConfigParser() + config.read_string(text) + + platform_mock = Mock() + platform_mock.return_value = "linux" + @patch('sys.platform', platform_mock) + def parse_config(instance, config): + return instance.processes_from_section(config, 'program:foo', 'bar') + + pconfig = parse_config(instance, config) + self.assertEqual(pconfig[0].prsetpdeathsig, None) + def test_processes_from_section_process_num_expansion(self): instance = self._makeOne() text = lstrip("""\ @@ -2645,7 +2707,7 @@ def _makeOne(self, *arg, **kw): 'stderr_events_enabled', 'stderr_syslog', 'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup', 'exitcodes', 'redirect_stderr', - 'environment'): + 'environment', 'prsetpdeathsig'): defaults[name] = name for name in ('stdout_logfile_backups', 'stdout_logfile_maxbytes', 'stderr_logfile_backups', 'stderr_logfile_maxbytes'): @@ -2727,7 +2789,7 @@ def _makeOne(self, *arg, **kw): 'stderr_events_enabled', 'stderr_syslog', 'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup', 'exitcodes', 'redirect_stderr', - 'environment'): + 'environment', 'prsetpdeathsig'): defaults[name] = name for name in ('stdout_logfile_backups', 'stdout_logfile_maxbytes', 'stderr_logfile_backups', 'stderr_logfile_maxbytes'): @@ -2775,7 +2837,7 @@ def _makeOne(self, *arg, **kw): 'stderr_events_enabled', 'stderr_syslog', 'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup', 'exitcodes', 'redirect_stderr', - 'environment'): + 'environment', 'prsetpdeathsig'): defaults[name] = name for name in ('stdout_logfile_backups', 'stdout_logfile_maxbytes', 'stderr_logfile_backups', 'stderr_logfile_maxbytes'): From dcf52d150a4ef65db6c9ff2a0d3cff67b36a40aa Mon Sep 17 00:00:00 2001 From: Luke Weber Date: Tue, 25 Aug 2015 22:29:04 -0700 Subject: [PATCH 7/7] Fix make_pconfig in test_supervisord --- supervisor/tests/test_supervisord.py | 1 + 1 file changed, 1 insertion(+) diff --git a/supervisor/tests/test_supervisord.py b/supervisor/tests/test_supervisord.py index df1523296..4ca08b19a 100644 --- a/supervisor/tests/test_supervisord.py +++ b/supervisor/tests/test_supervisord.py @@ -332,6 +332,7 @@ def make_pconfig(name, command, **params): 'stopasgroup': False, 'killasgroup': False, 'exitcodes': (0,2), 'environment': None, 'serverurl': None, + 'prsetpdeathsig': None } result.update(params) return ProcessConfig(options, **result)