diff --git a/Cargo.lock b/Cargo.lock index b9558dc8e..b4d4d487c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -798,6 +798,7 @@ dependencies = [ "log", "luahelper", "mlua", + "nix 0.26.2", "notify", "ordered-float", "portable-pty", diff --git a/config/Cargo.toml b/config/Cargo.toml index 0c7c1608b..24dcae13d 100644 --- a/config/Cargo.toml +++ b/config/Cargo.toml @@ -45,5 +45,8 @@ wezterm-bidi = { path = "../bidi" } wezterm-input-types = { path = "../wezterm-input-types" } wezterm-term = { path = "../term", features=["use_serde"] } +[target."cfg(unix)".dependencies] +nix = "0.26" + [target."cfg(windows)".dependencies] winapi = { version = "0.3", features = ["winuser"]} diff --git a/config/src/config.rs b/config/src/config.rs index 55b11ec4d..0063abb6e 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -766,6 +766,12 @@ pub struct Config { #[dynamic(default = "default_one")] pub palette_max_key_assigments_for_action: usize, + + #[dynamic(default = "default_ulimit_nofile")] + pub ulimit_nofile: u64, + + #[dynamic(default = "default_ulimit_nproc")] + pub ulimit_nproc: u64, } impl_lua_conversion_dynamic!(Config); @@ -773,6 +779,14 @@ fn default_one() -> usize { 1 } +fn default_ulimit_nofile() -> u64 { + 2048 +} + +fn default_ulimit_nproc() -> u64 { + 2048 +} + impl Default for Config { fn default() -> Self { // Ask FromDynamic to provide the defaults based on the attributes @@ -791,6 +805,47 @@ impl Config { Self::load_with_overrides(&wezterm_dynamic::Value::default()) } + pub fn update_ulimit(&self) -> anyhow::Result<()> { + #[cfg(unix)] + { + use nix::sys::resource::{getrlimit, setrlimit, Resource}; + + let (no_file_soft, no_file_hard) = getrlimit(Resource::RLIMIT_NOFILE)?; + + if no_file_soft < self.ulimit_nofile { + setrlimit( + Resource::RLIMIT_NOFILE, + self.ulimit_nofile.min(no_file_hard), + no_file_hard, + ) + .with_context(|| { + format!( + "raise RLIMIT_NOFILE from {no_file_soft} to ulimit_nofile {}", + self.ulimit_nofile + ) + })?; + } + + let (nproc_soft, nproc_hard) = getrlimit(Resource::RLIMIT_NPROC)?; + + if nproc_soft < self.ulimit_nproc { + setrlimit( + Resource::RLIMIT_NPROC, + self.ulimit_nproc.min(nproc_hard), + nproc_hard, + ) + .with_context(|| { + format!( + "raise RLIMIT_NPROC from {nproc_soft} to ulimit_nproc {}", + self.ulimit_nproc + ) + })?; + } + } + + Ok(()) + } + pub fn load_with_overrides(overrides: &wezterm_dynamic::Value) -> LoadedConfig { // Note that the directories crate has methods for locating project // specific config directories, but only returns one of them, not diff --git a/docs/changelog.md b/docs/changelog.md index 0b2eb7d71..1f874e321 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -23,6 +23,7 @@ As features stabilize some brief notes about them will accumulate here. #### New * [pane:activate()](config/lua/pane/activate.md) and [tab:activate()](config/lua/MuxTab/activate.md). #3217 +* [ulimit_nofile](config/lua/config/ulimit_nofile.md) and [ulimint_nproc](config/lua/config/ulimit_nproc.md) options. ?3353 #### Fixed * mux: Stale remote window mapping could prevent spawning new tabs in remote domain. #2759 diff --git a/docs/config/lua/config/ulimit_nofile.md b/docs/config/lua/config/ulimit_nofile.md new file mode 100644 index 000000000..0f769e489 --- /dev/null +++ b/docs/config/lua/config/ulimit_nofile.md @@ -0,0 +1,14 @@ +# `ulimit_nofile = 2048` + +{{since('nightly')}} + +On Unix systems, specifies the minimum desirable value for the `RLIMIT_NOFILE` +*soft limit*. + +That system parameter controls the maximum number of file descriptors that a +given process is permitted to open. + +On startup, wezterm will inspect the soft and hard limits, and if the soft +limit is *below* the value of the `ulimit_nofile` option, wezterm will attempt to +raise it to `min(ulimit_nofile, hard_limit)`. + diff --git a/docs/config/lua/config/ulimit_nproc.md b/docs/config/lua/config/ulimit_nproc.md new file mode 100644 index 000000000..d75e564fa --- /dev/null +++ b/docs/config/lua/config/ulimit_nproc.md @@ -0,0 +1,15 @@ +# `ulimit_nproc = 2048` + +{{since('nightly')}} + +On Unix systems, specifies the minimum desirable value for the `RLIMIT_NPROC` +*soft limit*. + +That system parameter controls the maximum number of simultaneous processes +that a given user is permitted to spawn. + +On startup, wezterm will inspect the soft and hard limits, and if the soft +limit is *below* the value of the `ulimit_nproc` option, wezterm will attempt to +raise it to `min(ulimit_nproc, hard_limit)`. + + diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index 3515dc978..f73b9e8a5 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -291,6 +291,7 @@ async fn spawn_tab_in_domain_if_mux_is_empty( } let config = config::configuration(); + config.update_ulimit()?; let _config_subscription = config::subscribe_to_config_reload(move || { promise::spawn::spawn_into_main_thread(async move { diff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rs index 721dee290..fccb486a7 100644 --- a/wezterm-mux-server/src/main.rs +++ b/wezterm-mux-server/src/main.rs @@ -82,6 +82,9 @@ fn run() -> anyhow::Result<()> { )?; let config = config::configuration(); + + config.update_ulimit()?; + #[cfg(unix)] { if opts.daemonize { diff --git a/wezterm/src/main.rs b/wezterm/src/main.rs index 90dad6f4f..1725045a8 100644 --- a/wezterm/src/main.rs +++ b/wezterm/src/main.rs @@ -635,6 +635,7 @@ fn run() -> anyhow::Result<()> { opts.skip_config, )?; let config = config::configuration(); + config.update_ulimit()?; match opts .cmd