Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.9] bpo-43592: Raise RLIMIT_NOFILE in test.libregrtest (GH-29127) #29145

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/libregrtest/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setup_tests(ns):
for signum in signals:
faulthandler.register(signum, chain=True, file=stderr_fd)

_adjust_resource_limits()
replace_stdout()
support.record_original_stdout(sys.stdout)

Expand Down Expand Up @@ -117,3 +118,26 @@ def restore_stdout():
sys.stdout.close()
sys.stdout = stdout
atexit.register(restore_stdout)


def _adjust_resource_limits():
"""Adjust the system resource limits (ulimit) if needed."""
try:
import resource
from resource import RLIMIT_NOFILE, RLIM_INFINITY
except ImportError:
return
fd_limit, max_fds = resource.getrlimit(RLIMIT_NOFILE)
# On macOS the default fd limit is sometimes too low (256) for our
# test suite to succeed. Raise it to something more reasonable.
# 1024 is a common Linux default.
desired_fds = 1024
if fd_limit < desired_fds and fd_limit < max_fds:
new_fd_limit = min(desired_fds, max_fds)
try:
resource.setrlimit(RLIMIT_NOFILE, (new_fd_limit, max_fds))
print(f"Raised RLIMIT_NOFILE: {fd_limit} -> {new_fd_limit}")
except (ValueError, OSError) as err:
print(f"Unable to raise RLIMIT_NOFILE from {fd_limit} to "
f"{new_fd_limit}: {err}.")

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`test.libregrtest` now raises the soft resource limit for the maximum
number of file descriptors when the default is too low for our test suite as
was often the case on macOS.