From 395b03a77d79e5edfae2a44f0086ec01a1654d51 Mon Sep 17 00:00:00 2001 From: David Wilson Date: Fri, 2 Aug 2019 22:30:58 +0100 Subject: [PATCH] issue #549: fix setrlimit() crash and hard-wire OS X default OS X advertised unlimited, but really it means kern.maxfilesperproc. --- ansible_mitogen/process.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ansible_mitogen/process.py b/ansible_mitogen/process.py index b09b54eb..f9c59e50 100644 --- a/ansible_mitogen/process.py +++ b/ansible_mitogen/process.py @@ -246,11 +246,22 @@ def increase_open_file_limit(): limit is much higher. """ soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) - if soft < hard: - LOG.debug('raising soft open file limit from %d to %d', soft, hard) - resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) - else: - LOG.debug('cannot increase open file limit; existing limit is %d', hard) + LOG.debug('inherited open file limits: soft=%d hard=%d', soft, hard) + if soft >= hard: + LOG.debug('max open files already set to hard limit: %d', hard) + return + + # OS X is limited by kern.maxfilesperproc sysctl, rather than the + # advertised unlimited hard RLIMIT_NOFILE. Just hard-wire known defaults + # for that sysctl, to avoid the mess of querying it. + for value in (hard, 10240): + try: + resource.setrlimit(resource.RLIMIT_NOFILE, (value, hard)) + LOG.debug('raised soft open file limit from %d to %d', soft, value) + break + except ValueError as e: + LOG.debug('could not raise soft open file limit from %d to %d: %s', + soft, value, e) def common_setup(enable_affinity=True, _init_logging=True):