Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rlimit configuration for programs #229

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions supervisor/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ def __init__(self):
"a:", "minfds=", int, default=1024)
self.add("minprocs", "supervisord.minprocs",
"", "minprocs=", int, default=200)

self.add("stacksize", "supervisord.stacksize",
"", "stacksize=", int, default=10 *1024**2)

self.add("nocleanup", "supervisord.nocleanup",
"k", "nocleanup", flag=1, default=0)
self.add("strip_ansi", "supervisord.strip_ansi",
Expand Down Expand Up @@ -551,6 +555,8 @@ def read_config(self, fp):
get = parser.getdefault
section.minfds = integer(get('minfds', 1024))
section.minprocs = integer(get('minprocs', 200))
section.stacksize = integer(get('stacksize', 10 * 1024000))


directory = get('directory', None)
if directory is None:
Expand Down Expand Up @@ -785,6 +791,9 @@ def processes_from_section(self, parser, section, group_name,
numprocs = integer(get(section, 'numprocs', 1))
numprocs_start = integer(get(section, 'numprocs_start', 0))
process_name = get(section, 'process_name', '%(program_name)s')
minprocs = integer(get(section, 'minprocs', 4096))
minfds = integer(get(section, 'minfds', 4096))
stacksize = integer(get(section, 'stacksize', 10 * 1024000))
environment_str = get(section, 'environment', '')
stdout_cmaxbytes = byte_size(get(section,'stdout_capture_maxbytes','0'))
stdout_events = boolean(get(section, 'stdout_events_enabled','false'))
Expand Down Expand Up @@ -863,6 +872,9 @@ def processes_from_section(self, parser, section, group_name,
priority=priority,
autostart=autostart,
autorestart=autorestart,
stacksize=stacksize,
minprocs=minprocs,
minfds=minfds,
startsecs=startsecs,
startretries=startretries,
uid=uid,
Expand Down Expand Up @@ -1249,6 +1261,19 @@ def set_rlimits(self):
'name':'RLIMIT_NPROC',
})

if hasattr(resource, 'RLIMIT_STACK'):
limits.append(
{
'msg':('The maximum stack size in bytes'
'to run this program is %(min)s as per the "stacksize" '
'command-line argument or config file setting. '
'The current environment will only allow you '
'to allocate %(hard)s bytes for stack.'),
'min':self.stacksize,
'resource':resource.RLIMIT_STACK,
'name':'RLIMIT_STACK',
})

msgs = []

for limit in limits:
Expand All @@ -1272,6 +1297,16 @@ def set_rlimits(self):
locals())
except (resource.error, ValueError):
self.usage(msg % locals())

if (soft > min) or (soft == -1): # -1 means unlimited
try:
resource.setrlimit(res, (min, min))
msgs.append('Lowered %(name)s limit to %(min)s' %
locals())
except (resource.error, ValueError):
self.usage(msg % locals())


return msgs

def make_logger(self, critical_messages, warn_messages, info_messages):
Expand Down Expand Up @@ -1599,6 +1634,7 @@ class ProcessConfig(Config):
'stderr_logfile', 'stderr_capture_maxbytes',
'stderr_logfile_backups', 'stderr_logfile_maxbytes',
'stderr_events_enabled',
'minprocs', 'minfds', 'stacksize',
'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup',
'exitcodes', 'redirect_stderr' ]
optional_param_names = [ 'environment', 'serverurl' ]
Expand Down
18 changes: 18 additions & 0 deletions supervisor/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from supervisor.options import decode_wait_status
from supervisor.options import signame
from supervisor.options import ProcessException, BadCommand
from supervisor.options import ServerOptions

from supervisor.dispatchers import EventListenerStates

Expand All @@ -26,6 +27,12 @@

from supervisor.socket_manager import SocketManager

class ServerStub(ServerOptions):
def __init__(self, config):
self.minfds = config.minfds
self.minprocs = config.minprocs
self.stacksize = config.stacksize

class Subprocess:

"""A class to manage a subprocess."""
Expand Down Expand Up @@ -60,6 +67,9 @@ def __init__(self, config):
self.pipes = {}
self.state = ProcessStates.STOPPED

def set_rlimits(self):
return ServerStub(self.config).set_rlimits()

def removelogs(self):
for dispatcher in self.dispatchers.values():
if hasattr(dispatcher, 'removelogs'):
Expand Down Expand Up @@ -287,6 +297,14 @@ def _spawn_as_child(self, filename, argv):
# Presumably it also prevents HUP, etc received by
# supervisord from being sent to children.
options.setpgrp()

msg = self.set_rlimits()
if msg:
uid = self.config.uid
s = 'supervisor: error trying to setrlimit to'
options.write(2, s)
options.write(2, "(%s)\n" % msg)

self._prepare_child_fds()
# sending to fd 2 will put this output in the stderr log
msg = self.set_uid()
Expand Down
4 changes: 4 additions & 0 deletions supervisor/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ def __init__(self, options, name, command, directory=None, umask=None,
stdout_logfile_backups=0, stdout_logfile_maxbytes=0,
stderr_logfile=None, stderr_capture_maxbytes=0,
stderr_events_enabled=False,
minfds=1024, minprocs=1024, stacksize=1024**2,
stderr_logfile_backups=0, stderr_logfile_maxbytes=0,
redirect_stderr=False,
stopsignal=None, stopwaitsecs=10, stopasgroup=False, killasgroup=False,
Expand All @@ -494,6 +495,9 @@ def __init__(self, options, name, command, directory=None, umask=None,
self.startsecs = startsecs
self.startretries = startretries
self.uid = uid
self.minfds = minfds
self.minprocs = minprocs
self.stacksize = stacksize
self.stdout_logfile = stdout_logfile
self.stdout_capture_maxbytes = stdout_capture_maxbytes
self.stdout_events_enabled = stdout_events_enabled
Expand Down
2 changes: 2 additions & 0 deletions supervisor/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,7 @@ def _makeOne(self, *arg, **kw):
'stdout_logfile_backups', 'stdout_logfile_maxbytes',
'stderr_logfile', 'stderr_capture_maxbytes',
'stderr_events_enabled',
'minprocs', 'minfds', 'stacksize',
'stderr_logfile_backups', 'stderr_logfile_maxbytes',
'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup', 'exitcodes',
'redirect_stderr', 'environment'):
Expand Down Expand Up @@ -1517,6 +1518,7 @@ def _makeOne(self, *arg, **kw):
'stdout_logfile_backups', 'stdout_logfile_maxbytes',
'stderr_logfile', 'stderr_capture_maxbytes',
'stderr_events_enabled',
'minprocs', 'minfds', 'stacksize',
'stderr_logfile_backups', 'stderr_logfile_maxbytes',
'stopsignal', 'stopwaitsecs', 'stopasgroup', 'killasgroup', 'exitcodes',
'redirect_stderr', 'environment'):
Expand Down
1 change: 1 addition & 0 deletions supervisor/tests/test_supervisord.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ def make_pconfig(name, command, **params):
'directory': None, 'umask': None, 'priority': 999, 'autostart': True,
'autorestart': True, 'startsecs': 10, 'startretries': 999,
'uid': None, 'stdout_logfile': None, 'stdout_capture_maxbytes': 0,
'minprocs': 1024, 'minfds': 1024, 'stacksize': 1024**2,
'stdout_events_enabled': False,
'stdout_logfile_backups': 0, 'stdout_logfile_maxbytes': 0,
'stderr_logfile': None, 'stderr_capture_maxbytes': 0,
Expand Down