raise ulimit nofile and nproc to a reasonable min value on unix systems

It's a tremendous PITA for the user to do this at the system level on a
mac, where it is sorely needed.  This commit allows raising to a desired
minimum level, but won't decrease from an already larger soft limit.

refs: https://github.com/wez/wezterm/discussions/3353
This commit is contained in:
Wez Furlong committed 2023-03-28 07:29:47 -07:00
1 parent cb5252a246
commit a7fde7935b
9 files changed
+94

No files matched your search

Generated
+1
View File
@@ -798,6 +798,7 @@ dependencies = [
"log",
"luahelper",
"mlua",
"nix 0.26.2",
"notify",
"ordered-float",
"portable-pty",
+3
View File
@@ -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"]}
+55
View File
@@ -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
+1
View File
@@ -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
+14
View File
@@ -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)`.
+15
View File
@@ -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)`.
+1
View File
@@ -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 {
+3
View File
@@ -82,6 +82,9 @@ fn run() -> anyhow::Result<()> {
)?;
let config = config::configuration();
config.update_ulimit()?;
#[cfg(unix)]
{
if opts.daemonize {
+1
View File
@@ -635,6 +635,7 @@ fn run() -> anyhow::Result<()> {
opts.skip_config,
)?;
let config = config::configuration();
config.update_ulimit()?;
match opts
.cmd