diff --git a/Cargo.lock b/Cargo.lock index ce1e60d14..f412462b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -795,9 +795,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.155" +version = "0.2.157" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +checksum = "374af5f94e54fa97cf75e945cce8a6b201e88a1a07e688b47dfd2a59c66dbd86" [[package]] name = "libm" @@ -1198,6 +1198,7 @@ dependencies = [ "digest 0.11.0-pre.9", "hmac", "image", + "libc", "log", "mio", "num-bigint", diff --git a/pumpkin/Cargo.toml b/pumpkin/Cargo.toml index 3085385ea..5adf2f361 100644 --- a/pumpkin/Cargo.toml +++ b/pumpkin/Cargo.toml @@ -65,3 +65,4 @@ uuid = { version = "1.10", features = ["serde", "v3"]} tokio.workspace = true rayon.workspace = true +libc = "0.2.157" diff --git a/pumpkin/src/main.rs b/pumpkin/src/main.rs index 308140a01..1dd68ce79 100644 --- a/pumpkin/src/main.rs +++ b/pumpkin/src/main.rs @@ -33,6 +33,9 @@ fn main() -> io::Result<()> { let _profiler = dhat::Profiler::new_heap(); #[cfg(feature = "dhat-heap")] println!("Using a memory profiler"); + + adjust_file_descriptor_limits(); + let rt = tokio::runtime::Builder::new_multi_thread() .enable_all() .build() @@ -175,6 +178,36 @@ fn main() -> io::Result<()> { }) } +fn adjust_file_descriptor_limits() { + let mut limits = libc::rlimit { + rlim_cur: 0, + rlim_max: 0, + }; + + if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) } != 0 { + panic!( + "Failed to get the current file handle limits {}", + std::io::Error::last_os_error() + ); + }; + + let limit_before = limits.rlim_cur; + limits.rlim_cur = limits.rlim_max; + + if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limits) } != 0 { + panic!( + "Failed to set the file handle limits {}", + std::io::Error::last_os_error() + ); + } + + log::debug!( + "file descriptor adjusted to {} from {}", + limits.rlim_max, + limit_before + ); +} + fn next(current: &mut Token) -> Token { let next = current.0; current.0 += 1;