Skip to content

Commit eeb7150

Browse files
committed
issue #549: increase open file limit automatically if possible
While catching every possible case where "open file limit exceeded" is not possible, we can at least increase the soft limit to the available hard limit without any user effort. Do this in Ansible top-level process, even though we probably only need it in the MuxProcess. It seems there is no reason this could hurt
1 parent acab26d commit eeb7150

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

ansible_mitogen/process.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import logging
3333
import multiprocessing
3434
import os
35+
import resource
3536
import signal
3637
import socket
3738
import sys
@@ -237,9 +238,27 @@ def _setup_responder(responder):
237238
)
238239

239240

241+
def increase_open_file_limit():
242+
"""
243+
#549: in order to reduce the possibility of hitting an open files limit,
244+
increase :data:`resource.RLIMIT_NOFILE` from its soft limit to its hard
245+
limit, if they differ.
246+
247+
It is common that a low soft limit is configured by default, where the hard
248+
limit is much higher.
249+
"""
250+
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
251+
if soft < hard:
252+
LOG.debug('raising soft open file limit from %d to %d', soft, hard)
253+
resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
254+
else:
255+
LOG.debug('cannot increase open file limit; existing limit is %d', hard)
256+
257+
240258
def common_setup(enable_affinity=True, _init_logging=True):
241259
save_pid('controller')
242260
ansible_mitogen.logging.set_process_name('top')
261+
243262
if enable_affinity:
244263
ansible_mitogen.affinity.policy.assign_controller()
245264

@@ -255,6 +274,7 @@ def common_setup(enable_affinity=True, _init_logging=True):
255274
mitogen.core.enable_profiling()
256275

257276
MuxProcess.cls_original_env = dict(os.environ)
277+
increase_open_file_limit()
258278

259279

260280
def get_cpu_count(default=None):

0 commit comments

Comments
 (0)