From 1ff0f5ebe9e4b5bc17d9676a6350574e7caf96cb Mon Sep 17 00:00:00 2001 From: Ben Brown <3638873+benofbrown@users.noreply.github.com> Date: Tue, 29 Aug 2023 09:19:24 +0100 Subject: [PATCH 1/2] Respect hard limit when setting NOFILE In some cases (such as the default settings in Debian Linux) the hard limit for open files is much higher than the minimum open files limit of 10000. By respecting this hard limit instead of requesting an infinite number of open files this allows us to set the open files to 10000, when attepting to set it to unlimited will fail. --- locust/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/locust/main.py b/locust/main.py index 6b4585b58b..a3152f18dc 100644 --- a/locust/main.py +++ b/locust/main.py @@ -188,12 +188,12 @@ def is_valid_percentile(parameter): import resource minimum_open_file_limit = 10000 - current_open_file_limit = resource.getrlimit(resource.RLIMIT_NOFILE)[0] + (current_open_file_limit, current_open_file_limit_hard) = resource.getrlimit(resource.RLIMIT_NOFILE) - if current_open_file_limit < minimum_open_file_limit: + if current_open_file_limit < minimum_open_file_limit and minimum_open_file_limit < current_open_file_limit_hard: # Increasing the limit to 10000 within a running process should work on at least MacOS. # It does not work on all OS:es, but we should be no worse off for trying. - resource.setrlimit(resource.RLIMIT_NOFILE, [minimum_open_file_limit, resource.RLIM_INFINITY]) + resource.setrlimit(resource.RLIMIT_NOFILE, [minimum_open_file_limit, current_open_file_limit_hard]) except BaseException: logger.warning( f"""System open file limit '{current_open_file_limit}' is below minimum setting '{minimum_open_file_limit}'. From a96dc29b2dbbf8369c299945aea4f18f30b303b9 Mon Sep 17 00:00:00 2001 From: Ben Brown <3638873+benofbrown@users.noreply.github.com> Date: Tue, 29 Aug 2023 16:03:11 +0100 Subject: [PATCH 2/2] Improve readability In the instance of the requested new soft limit being higher than the hard limit, the call will raise an exception and the warning will be displayed, so I have removed the explicit check for it. --- locust/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/locust/main.py b/locust/main.py index a3152f18dc..4591c4e2e7 100644 --- a/locust/main.py +++ b/locust/main.py @@ -188,15 +188,15 @@ def is_valid_percentile(parameter): import resource minimum_open_file_limit = 10000 - (current_open_file_limit, current_open_file_limit_hard) = resource.getrlimit(resource.RLIMIT_NOFILE) + (soft_limit, hard_limit) = resource.getrlimit(resource.RLIMIT_NOFILE) - if current_open_file_limit < minimum_open_file_limit and minimum_open_file_limit < current_open_file_limit_hard: + if soft_limit < minimum_open_file_limit: # Increasing the limit to 10000 within a running process should work on at least MacOS. # It does not work on all OS:es, but we should be no worse off for trying. - resource.setrlimit(resource.RLIMIT_NOFILE, [minimum_open_file_limit, current_open_file_limit_hard]) + resource.setrlimit(resource.RLIMIT_NOFILE, [minimum_open_file_limit, hard_limit]) except BaseException: logger.warning( - f"""System open file limit '{current_open_file_limit}' is below minimum setting '{minimum_open_file_limit}'. + f"""System open file limit '{soft_limit} is below minimum setting '{minimum_open_file_limit}'. It's not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.""" )