forge
Explore
Log in

Don't you dare go Hollow

Increase RLIMIT_NOFILE to maximum

Browse files at this commit

bcca03cce7da9dc4125aa34943041cb65e0fd4bbIvan MolodetskikhParent: efb39e4
Fixes Xwayland + RustRover crashing.

See similar changes:
* https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2235
* https://github.com/swaywm/sway/pull/6629
src/main.rsmodified+4 -1
@@ -18,7 +18,8 @@ use niri::dbus;
18 use niri::ipc::client::handle_msg;18 use niri::ipc::client::handle_msg;
19 use niri::niri::State;19 use niri::niri::State;
20 use niri::utils::spawning::{20 use niri::utils::spawning::{
21- spawn, CHILD_ENV, REMOVE_ENV_RUST_BACKTRACE, REMOVE_ENV_RUST_LIB_BACKTRACE,21+ spawn, store_and_increase_nofile_rlimit, CHILD_ENV, REMOVE_ENV_RUST_BACKTRACE,
22+ REMOVE_ENV_RUST_LIB_BACKTRACE,
22 };23 };
23 use niri::utils::watcher::Watcher;24 use niri::utils::watcher::Watcher;
24 use niri::utils::{cause_panic, version, IS_SYSTEMD_SERVICE};25 use niri::utils::{cause_panic, version, IS_SYSTEMD_SERVICE};
@@ -173,6 +174,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
173 let spawn_at_startup = mem::take(&mut config.spawn_at_startup);174 let spawn_at_startup = mem::take(&mut config.spawn_at_startup);
174 *CHILD_ENV.write().unwrap() = mem::take(&mut config.environment);175 *CHILD_ENV.write().unwrap() = mem::take(&mut config.environment);
175 176
177+ store_and_increase_nofile_rlimit();
178+
176 // Create the compositor.179 // Create the compositor.
177 let mut event_loop = EventLoop::try_new().unwrap();180 let mut event_loop = EventLoop::try_new().unwrap();
178 let display = Display::new().unwrap();181 let display = Display::new().unwrap();
src/utils/spawning.rsmodified+50 -1
@@ -2,10 +2,11 @@ use std::ffi::OsStr;
2 use std::os::unix::process::CommandExt;2 use std::os::unix::process::CommandExt;
3 use std::path::Path;3 use std::path::Path;
4 use std::process::{Child, Command, Stdio};4 use std::process::{Child, Command, Stdio};
5-use std::sync::atomic::{AtomicBool, Ordering};5+use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
6 use std::sync::RwLock;6 use std::sync::RwLock;
7 use std::{io, thread};7 use std::{io, thread};
8 8
9+use libc::{getrlimit, rlimit, setrlimit, RLIMIT_NOFILE};
9 use niri_config::Environment;10 use niri_config::Environment;
10 11
11 use crate::utils::expand_home;12 use crate::utils::expand_home;
@@ -14,6 +15,50 @@ pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
14 pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);15 pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
15 pub static CHILD_ENV: RwLock<Environment> = RwLock::new(Environment(Vec::new()));16 pub static CHILD_ENV: RwLock<Environment> = RwLock::new(Environment(Vec::new()));
16 17
18+static ORIGINAL_NOFILE_RLIMIT_CUR: AtomicU64 = AtomicU64::new(0);
19+static ORIGINAL_NOFILE_RLIMIT_MAX: AtomicU64 = AtomicU64::new(0);
20+
21+/// Increases the nofile rlimit to the maximum and stores the original value.
22+pub fn store_and_increase_nofile_rlimit() {
23+ let mut rlim = rlimit {
24+ rlim_cur: 0,
25+ rlim_max: 0,
26+ };
27+ if unsafe { getrlimit(RLIMIT_NOFILE, &mut rlim) } != 0 {
28+ let err = io::Error::last_os_error();
29+ warn!("error getting nofile rlimit: {err:?}");
30+ return;
31+ }
32+
33+ ORIGINAL_NOFILE_RLIMIT_CUR.store(rlim.rlim_cur, Ordering::SeqCst);
34+ ORIGINAL_NOFILE_RLIMIT_MAX.store(rlim.rlim_max, Ordering::SeqCst);
35+
36+ trace!(
37+ "changing nofile rlimit from {} to {}",
38+ rlim.rlim_cur,
39+ rlim.rlim_max
40+ );
41+ rlim.rlim_cur = rlim.rlim_max;
42+
43+ if unsafe { setrlimit(RLIMIT_NOFILE, &rlim) } != 0 {
44+ let err = io::Error::last_os_error();
45+ warn!("error setting nofile rlimit: {err:?}");
46+ }
47+}
48+
49+/// Restores the original nofile rlimit.
50+pub fn restore_nofile_rlimit() {
51+ let rlim_cur = ORIGINAL_NOFILE_RLIMIT_CUR.load(Ordering::SeqCst);
52+ let rlim_max = ORIGINAL_NOFILE_RLIMIT_MAX.load(Ordering::SeqCst);
53+
54+ if rlim_cur == 0 {
55+ return;
56+ }
57+
58+ let rlim = rlimit { rlim_cur, rlim_max };
59+ unsafe { setrlimit(RLIMIT_NOFILE, &rlim) };
60+}
61+
17 /// Spawns the command to run independently of the compositor.62 /// Spawns the command to run independently of the compositor.
18 pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {63 pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {
19 let _span = tracy_client::span!();64 let _span = tracy_client::span!();
@@ -103,6 +148,8 @@ fn do_spawn(command: &OsStr, mut process: Command) -> Option<Child> {
103 _ => libc::_exit(0),148 _ => libc::_exit(0),
104 }149 }
105 150
151+ restore_nofile_rlimit();
152+
106 Ok(())153 Ok(())
107 });154 });
108 }155 }
@@ -214,6 +261,8 @@ mod systemd {
214 }261 }
215 }262 }
216 263
264+ restore_nofile_rlimit();
265+
217 Ok(())266 Ok(())
218 });267 });
219 }268 }