Bug 1616790 - Keep the hard limit for `ulimit -n` unchanged. r=froydnj

The hard limit prevents program to set a value higher than the soft
limit for themselves, and it turns out Firefox does that.

Also, keep the soft limit to its original value if it was already less
than 1024.

Differential Revision: https://phabricator.services.mozilla.com/D63623

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Mike Hommey 2020-02-21 16:25:13 +00:00
Родитель a19a0ef372
Коммит 4ae6ad3247
1 изменённых файлов: 14 добавлений и 2 удалений

Просмотреть файл

@ -692,8 +692,20 @@ def main(args):
# Processes spawned by this one will inherit the limit set here.
try:
import resource
limit = int(os.environ.get('MOZ_LIMIT_NOFILE', '1024'))
resource.setrlimit(resource.RLIMIT_NOFILE, (limit, limit))
# Keep the hard limit the same, though, allowing processes to change their
# soft limit if they need to (Firefox does, for instance).
(soft, hard) = resource.getrlimit(resource.RLIMIT_NOFILE)
limit = os.environ.get('MOZ_LIMIT_NOFILE')
if limit:
limit = int(limit)
else:
# If no explicit limit is given, use 1024 if it's less than the current
# soft limit. For instance, the default on macOS is 256, so we'd pick
# that rather than 1024.
limit = min(soft, 1024)
# Now apply the limit, if it's different from the original one.
if limit != soft:
resource.setrlimit(resource.RLIMIT_NOFILE, (limit, hard))
except ImportError:
# The resource module is UNIX only.
pass