-
Notifications
You must be signed in to change notification settings - Fork 25
fix: raise RLIMIT_NOFILE soft limit at startup #422
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
Merged
+159
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Copyright 2016-2020 Parity Technologies (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| // Ported from ethrex (`crates/common/fd_limit.rs`); the `Outcome` enum was | ||
| // dropped because the binary's call site doesn't need it. | ||
|
|
||
| /// Errors that happen when trying to raise file descriptor resource limit | ||
| #[derive(Debug, thiserror::Error)] | ||
| #[allow(clippy::enum_variant_names)] | ||
| pub enum Error { | ||
| /// Failed to call sysctl to get max supported value configured in sysctl | ||
| #[error("Failed to call sysctl to get max supported value configured in sysctl: {0}")] | ||
| #[cfg(any(target_os = "macos", target_os = "ios"))] | ||
| FailedToCallSysctl(std::io::Error), | ||
| /// Failed to get current limit | ||
| #[error("Failed to get current limit: {0}")] | ||
| FailedToGetLimit(std::io::Error), | ||
| /// Failed to set new limit | ||
| #[error("Failed to set new limit ({from}->{to}): {error}")] | ||
| FailedToSetLimit { | ||
| /// Current limit | ||
| from: u64, | ||
| /// New desired limit | ||
| to: u64, | ||
| /// Low level OS error | ||
| error: std::io::Error, | ||
| }, | ||
| } | ||
|
|
||
| /// Raise the soft open file descriptor resource limit to the smaller of the | ||
| /// kernel limit and the hard resource limit. | ||
| /// | ||
| /// darwin_fd_limit exists to work around an issue where launchctl on Mac OS X | ||
| /// defaults the rlimit maxfiles to 256/unlimited. The default soft limit of 256 | ||
| /// ends up being far too low for our multithreaded scheduler testing, depending | ||
| /// on the number of cores available. | ||
| #[cfg(any(target_os = "macos", target_os = "ios"))] | ||
| #[allow(clippy::useless_conversion, non_camel_case_types)] | ||
| pub fn raise_fd_limit() -> Result<(), Error> { | ||
| use std::cmp; | ||
| use std::io; | ||
| use std::mem::size_of_val; | ||
| use std::ptr::null_mut; | ||
|
|
||
| unsafe { | ||
| static CTL_KERN: libc::c_int = 1; | ||
| static KERN_MAXFILESPERPROC: libc::c_int = 29; | ||
|
|
||
| // The strategy here is to fetch the current resource limits, read the | ||
| // kern.maxfilesperproc sysctl value, and bump the soft resource limit for | ||
| // maxfiles up to the sysctl value. | ||
|
|
||
| // Fetch the kern.maxfilesperproc value | ||
| let mut mib: [libc::c_int; 2] = [CTL_KERN, KERN_MAXFILESPERPROC]; | ||
| let mut maxfiles: libc::c_int = 0; | ||
| let mut size: libc::size_t = size_of_val(&maxfiles) as libc::size_t; | ||
| if libc::sysctl( | ||
| &mut mib[0], | ||
| 2, | ||
| &mut maxfiles as *mut _ as *mut _, | ||
| &mut size, | ||
| null_mut(), | ||
| 0, | ||
| ) != 0 | ||
| { | ||
| return Err(Error::FailedToCallSysctl(io::Error::last_os_error())); | ||
| } | ||
|
|
||
| // Fetch the current resource limits | ||
| let mut rlim = libc::rlimit { | ||
| rlim_cur: 0, | ||
| rlim_max: 0, | ||
| }; | ||
| if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 { | ||
| return Err(Error::FailedToGetLimit(io::Error::last_os_error())); | ||
| } | ||
|
|
||
| let old_value = rlim.rlim_cur; | ||
|
|
||
| // Bump the soft limit to the smaller of kern.maxfilesperproc and the hard | ||
| // limit | ||
| rlim.rlim_cur = cmp::min(maxfiles as libc::rlim_t, rlim.rlim_max); | ||
|
|
||
| // Set our newly-increased resource limit | ||
| if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 { | ||
| return Err(Error::FailedToSetLimit { | ||
| from: old_value.into(), | ||
| to: rlim.rlim_cur.into(), | ||
| error: io::Error::last_os_error(), | ||
| }); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Raise the soft open file descriptor resource limit to the hard resource | ||
| /// limit. | ||
| #[cfg(target_os = "linux")] | ||
| #[allow(clippy::useless_conversion, non_camel_case_types)] | ||
| pub fn raise_fd_limit() -> Result<(), Error> { | ||
| use std::io; | ||
|
|
||
| unsafe { | ||
| // Fetch the current resource limits | ||
| let mut rlim = libc::rlimit { | ||
| rlim_cur: 0, | ||
| rlim_max: 0, | ||
| }; | ||
| if libc::getrlimit(libc::RLIMIT_NOFILE, &mut rlim) != 0 { | ||
| return Err(Error::FailedToGetLimit(io::Error::last_os_error())); | ||
| } | ||
|
|
||
| let old_value = rlim.rlim_cur; | ||
|
|
||
| // Set soft limit to hard limit | ||
| rlim.rlim_cur = rlim.rlim_max; | ||
|
|
||
| // Set our newly-increased resource limit | ||
| if libc::setrlimit(libc::RLIMIT_NOFILE, &rlim) != 0 { | ||
| return Err(Error::FailedToSetLimit { | ||
| from: old_value.into(), | ||
| to: rlim.rlim_cur.into(), | ||
| error: io::Error::last_os_error(), | ||
| }); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Does nothing on unsupported platform | ||
| #[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))] | ||
| pub fn raise_fd_limit() -> Result<(), Error> { | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the process hard limit for
RLIMIT_NOFILEisRLIM_INFINITY(e.g.,ulimit -H -n unlimited),rlim_curis set toRLIM_INFINITY(u64::MAX). Linux rejects this withEINVALbecauseRLIMIT_NOFILEis silently capped by/proc/sys/fs/nr_open(default 1048576). The call lands in theErrbranch inmain.rsand only a warning is logged, so the fix has no effect on those hosts. The macOS path avoids this by capping againstkern.maxfilesperproc; the same approach — reading/proc/sys/fs/nr_openand usingmin(rlim_max, nr_open)— would make the Linux path robust in this scenario.Prompt To Fix With AI