Skip to content

Commit 216c040

Browse files
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 (cherry picked from commit 843b890) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent c53428f commit 216c040

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Lib/test/libregrtest/setup.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def setup_tests(ns):
3535
for signum in signals:
3636
faulthandler.register(signum, chain=True, file=stderr_fd)
3737

38+
_adjust_resource_limits()
3839
replace_stdout()
3940
support.record_original_stdout(sys.stdout)
4041

@@ -117,3 +118,26 @@ def restore_stdout():
117118
sys.stdout.close()
118119
sys.stdout = stdout
119120
atexit.register(restore_stdout)
121+
122+
123+
def _adjust_resource_limits():
124+
"""Adjust the system resource limits (ulimit) if needed."""
125+
try:
126+
import resource
127+
from resource import RLIMIT_NOFILE, RLIM_INFINITY
128+
except ImportError:
129+
return
130+
fd_limit, max_fds = resource.getrlimit(RLIMIT_NOFILE)
131+
# On macOS the default fd limit is sometimes too low (256) for our
132+
# test suite to succeed. Raise it to something more reasonable.
133+
# 1024 is a common Linux default.
134+
desired_fds = 1024
135+
if fd_limit < desired_fds and fd_limit < max_fds:
136+
new_fd_limit = min(desired_fds, max_fds)
137+
try:
138+
resource.setrlimit(RLIMIT_NOFILE, (new_fd_limit, max_fds))
139+
print(f"Raised RLIMIT_NOFILE: {fd_limit} -> {new_fd_limit}")
140+
except (ValueError, OSError) as err:
141+
print(f"Unable to raise RLIMIT_NOFILE from {fd_limit} to "
142+
f"{new_fd_limit}: {err}.")
143+
Lines changed: 3 additions & 0 deletions
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)