Skip to content

Commit 843b890

Browse files
authored
bpo-43592: Raise RLIMIT_NOFILE in test.libregrtest (GH-29127)
Raise RLIMIT_NOFILE in test.libregrtest. On macOS the default is often too low for our testsuite to succeed. Co-authored by reviewer: Victor Stinner
1 parent 01cf4fb commit 843b890

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Lib/test/libregrtest/setup.py

+24
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def setup_tests(ns):
3939
for signum in signals:
4040
faulthandler.register(signum, chain=True, file=stderr_fd)
4141

42+
_adjust_resource_limits()
4243
replace_stdout()
4344
support.record_original_stdout(sys.stdout)
4445

@@ -133,3 +134,26 @@ def restore_stdout():
133134
sys.stdout.close()
134135
sys.stdout = stdout
135136
atexit.register(restore_stdout)
137+
138+
139+
def _adjust_resource_limits():
140+
"""Adjust the system resource limits (ulimit) if needed."""
141+
try:
142+
import resource
143+
from resource import RLIMIT_NOFILE, RLIM_INFINITY
144+
except ImportError:
145+
return
146+
fd_limit, max_fds = resource.getrlimit(RLIMIT_NOFILE)
147+
# On macOS the default fd limit is sometimes too low (256) for our
148+
# test suite to succeed. Raise it to something more reasonable.
149+
# 1024 is a common Linux default.
150+
desired_fds = 1024
151+
if fd_limit < desired_fds and fd_limit < max_fds:
152+
new_fd_limit = min(desired_fds, max_fds)
153+
try:
154+
resource.setrlimit(RLIMIT_NOFILE, (new_fd_limit, max_fds))
155+
print(f"Raised RLIMIT_NOFILE: {fd_limit} -> {new_fd_limit}")
156+
except (ValueError, OSError) as err:
157+
print(f"Unable to raise RLIMIT_NOFILE from {fd_limit} to "
158+
f"{new_fd_limit}: {err}.")
159+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:mod:`test.libregrtest` now raises the soft resource limit for the maximum
2+
number of file descriptors when the default is too low for our test suite as
3+
was often the case on macOS.

0 commit comments

Comments
 (0)