util: Raise open file soft limit at startup on Unix (#64034)

# Objective

Fixes #46470
Relates to #53901, #62486, #20806

Processes launched via GUI (such as `launchd`, Finder, Spotlight, or
Dock on macOS) inherit a default soft `RLIMIT_NOFILE` of only 256 open
files (lazily raised to 2,560 by Apple frameworks on first use of
certain APIs).

In active workspaces—especially those running multiple language servers,
MCP context servers, active filesystem watchers (`FSEventStream`), and
agent tool calls—baseline file descriptor consumption sits near 200–250.
During bursts of file activity or shell tool spawning, Zed quickly
exhausts this soft limit and fails with `EMFILE (os error 24: Too many
open files)`.

Similar desktop runtimes and editors (Chromium, VS Code, Node.js, Go,
JVM) raise their soft descriptor limit at startup for this exact reason.

## Solution

- Added `util::increase_open_file_limit()` called early in `main()`
right after tracing initialization.
- On macOS, query `getrlimit(RLIMIT_NOFILE)` and raise `rlim_cur` to
`limit.rlim_max.min(OPEN_MAX)`, where `OPEN_MAX = 10,240` (from
`<sys/syslimits.h>`). Note that macOS `setrlimit(2)` returns `EINVAL` if
`rlim_cur > 10240` even when `rlim_max` is `RLIM_INFINITY`.
- On other Unix platforms (Linux), raise `rlim_cur` to
`limit.rlim_max.min(65536)`.
- If the current limit is already at or above the target, it remains
untouched.
- Added a unit test in `crates/util/src/util.rs` verifying limit
querying and retention.

## Testing

- Added `test_increase_open_file_limit` in `crates/util/src/util.rs`.
- Ran `cargo test -p util` (134 passed, 0 failed).
- Ran `cargo check -p zed` (passed cleanly).

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

---

Release Notes:

- Raised the default open file descriptor soft limit at startup on
Unix/macOS to prevent `EMFILE` (`Too many open files`) errors in large
workspaces.

---------

Co-authored-by: Ben Kunkle <ben@zed.dev>
This commit is contained in:
Anton WieheandBen Kunkle authored and GitHub committed 2026-09-11 15:39:49 +00:00
1 parent 7ff8e1c258
commit d12e456be7
3 files changed
+37 -1

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ dirs.workspace = true
[target.'cfg(unix)'.dependencies]
command-fds = "0.3.1"
libc.workspace = true
nix = { workspace = true, features = ["user"] }
nix = { workspace = true, features = ["resource", "user"] }
[target.'cfg(target_os = "macos")'.dependencies]
mach2.workspace = true
+33
View File
@@ -244,6 +244,39 @@ Error: Running Zed as root or via sudo is unsupported.
}
}
/// Raises the soft limit on open file descriptors without changing the hard limit.
///
/// Call during startup, before spawning children that will inherit the limit.
#[cfg(unix)]
pub fn increase_open_file_limit() -> Result<()> {
use anyhow::Context as _;
use nix::sys::resource::{Resource::RLIMIT_NOFILE, getrlimit, setrlimit};
let (soft_limit, hard_limit) = getrlimit(RLIMIT_NOFILE).context("getrlimit(RLIMIT_NOFILE)")?;
// These are startup targets, not OS ceilings. Preserve higher inherited limits.
let target = if cfg!(target_os = "macos") {
10_240
} else {
65_536
};
let mut requested_limit = hard_limit.min(target);
while requested_limit > soft_limit {
let Err(error) = setrlimit(RLIMIT_NOFILE, requested_limit, hard_limit) else {
log::info!("raised open file soft limit from {soft_limit} to {requested_limit}");
return Ok(());
};
// Some systems enforce a ceiling below the reported hard limit.
if error != nix::errno::Errno::EINVAL || requested_limit == soft_limit + 1 {
return Err(error).context("setrlimit(RLIMIT_NOFILE)");
}
requested_limit = soft_limit + (requested_limit - soft_limit) / 2;
}
Ok(())
}
#[cfg(unix)]
fn load_shell_from_passwd() -> Result<()> {
let buflen = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
+3
View File
@@ -299,6 +299,9 @@ fn main() {
}
ztracing::init();
#[cfg(unix)]
util::increase_open_file_limit().log_err();
let version = option_env!("ZED_BUILD_ID");
let app_commit_sha =
option_env!("ZED_COMMIT_SHA").map(|commit_sha| AppCommitSha::new(commit_sha.to_string()));