Raise the managed app-server file descriptor limit on Unix (#51470)

## What changed

For managed app-server daemons using Unix sockets, raise the soft
`RLIMIT_NOFILE` limit to 4096 at startup, capped by the inherited hard limit.
Preserve existing higher soft limits and log a warning if the adjustment fails.

GitOrigin-RevId: 2ef27370e55cd86a7a62cbc3d47041933b89ac21
This commit is contained in:
Eric Traut
2026-10-06 19:51:15 +00:00
committed by copyberry
parent 5679e675f4
commit 8519cde1ba
4 changed files with 40 additions and 2 deletions
+1
View File
@@ -2152,6 +2152,7 @@ dependencies = [
"hmac 0.12.1",
"http 1.4.0",
"keyring",
"libc",
"opentelemetry",
"opentelemetry_sdk",
"pretty_assertions",
+3
View File
@@ -123,6 +123,9 @@ uuid = { workspace = true, features = ["serde", "v7"] }
[target.'cfg(all(target_os = "linux", target_env = "musl", any(target_arch = "x86_64", target_arch = "aarch64")))'.dependencies]
tikv-jemallocator = { workspace = true }
[target.'cfg(unix)'.dependencies]
libc = { workspace = true }
[target.'cfg(windows)'.dependencies]
codex-windows-sandbox = { workspace = true }
+10 -2
View File
@@ -122,6 +122,8 @@ mod gateway_oauth_notifications;
mod image_url;
pub mod in_process;
mod log_write_warning;
#[cfg(unix)]
mod managed_daemon;
mod mcp_refresh;
mod message_processor;
mod model_catalog;
@@ -504,6 +506,10 @@ pub async fn run_main_with_transport_options(
) -> IoResult<AppServerExit> {
#[cfg(target_os = "windows")]
let _registered_core = codex_windows_sandbox::registered_core_requested();
let managed_daemon = matches!(&transport, AppServerTransport::UnixSocket { .. })
&& runtime_options.managed_daemon;
#[cfg(unix)]
let nofile_limit_result = managed_daemon.then(managed_daemon::raise_nofile_limit);
let loader_overrides = loader_overrides_with_test_user_config_file(
loader_overrides,
test_user_config_file_from_env(),
@@ -747,6 +753,10 @@ pub async fn run_main_with_transport_options(
.with(log_db_layer)
.with(otel_layers)
.try_init();
#[cfg(unix)]
if let Some(Err(err)) = nofile_limit_result {
warn!(%err, "failed to raise managed app-server file descriptor limit");
}
for warning in &config_warnings {
match &warning.details {
Some(details) => error!("{} {}", warning.summary, details),
@@ -784,8 +794,6 @@ pub async fn run_main_with_transport_options(
let single_client_mode = matches!(&transport, AppServerTransport::Stdio);
let graceful_signal_restart_enabled =
runtime_options.install_shutdown_signal_handler && !single_client_mode;
let managed_daemon = matches!(&transport, AppServerTransport::UnixSocket { .. })
&& runtime_options.managed_daemon;
let mut app_server_client_name_rx = None;
match &transport {
+26
View File
@@ -0,0 +1,26 @@
//! Unix process setup specific to a managed app-server daemon.
use std::io;
const NOFILE_LIMIT: libc::rlim_t = 4096;
pub(crate) fn raise_nofile_limit() -> io::Result<()> {
let mut limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
// SAFETY: getrlimit initializes the stack-owned resource-limit structure.
if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &raw mut limit) } == -1 {
return Err(io::Error::last_os_error());
}
let target = limit.rlim_cur.max(limit.rlim_max.min(NOFILE_LIMIT));
if target == limit.rlim_cur {
return Ok(());
}
limit.rlim_cur = target;
// SAFETY: the requested soft limit does not exceed the inherited hard limit.
if unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limit) } == -1 {
return Err(io::Error::last_os_error());
}
Ok(())
}