Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent-tools): raise Linux process cap (#8785)
Raghav Bhardwaj887e0d0eed78
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rsindex f152647d2..3ead54237 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/proc.rs@@ -238,7 +238,11 @@ const TEMP_ENV_KEYS: &[&str] = &["TMPDIR", "TMP", "TEMP"];/// than returning an error most tools report clearly.#[cfg(unix)]const CHILD_LIMITS: &[(RlimitResource, u64)] = &[- (nix::libc::RLIMIT_NPROC, 4096),+ // Linux counts NPROC across the entire UID, not one Jan child tree. Keep a+ // finite fork-bomb guard without making ordinary workstation activity starve+ // agent-tool shells. macOS inherits its host-managed per-UID ceiling instead.+ #[cfg(target_os = "linux")]+ (nix::libc::RLIMIT_NPROC, 8192),(nix::libc::RLIMIT_NOFILE, 65536),(nix::libc::RLIMIT_FSIZE, 16 * 1024 * 1024 * 1024),];@@ -254,13 +258,12 @@ type RlimitResource = nix::libc::c_int;/// Bound the resource exhaustion a sandboxed command could otherwise trigger on/// the host. `bwrap` 0.6.1 (and older) has no `--rlimit`, so instead we clamp the/// child's soft limits here, before exec, from the one choke point every backend-/// funnels through. A fork-bomb is capped by `NPROC`, descriptor exhaustion by-/// `NOFILE`, and disk fill through the unbounded workspace bind by `FSIZE`. The-/// hard limit is left at the host's value so a command that genuinely needs more-/// can raise its own soft limit back up. The bwrap wrapper execs `bwrap` itself,-/// which sets up the namespace and then execs the real shell, so the limits carry-/// over to every descendant. Linux only; the Windows AppContainer child is-/// limited by its token.+/// funnels through. Linux `NPROC` still constrains fork bombs, while descriptor+/// exhaustion and disk fill are capped by `NOFILE` and `FSIZE`. The hard limit is+/// left at the host's value so a command that genuinely needs more can raise its+/// own soft limit back up. The bwrap wrapper execs `bwrap` itself, which sets up+/// the namespace and then execs the real shell, so the limits carry over to every+/// descendant. Linux only; the Windows AppContainer child is limited by its token.#[cfg(unix)]fn confine_limits(cmd: &mut Command) {// `tokio::process::Command::pre_exec` (unix) is the std `pre_exec`; the call@@ -853,9 +856,8 @@ mod tests {#[cfg(unix)]#[tokio::test]- async fn confine_limits_caps_the_child_process_count() {- // The rlimit mounting must actually reach the spawned child: with NPROC- // clamped we still run up to the cap, but a fork-bomb past it fails.+ async fn confine_limits_caps_child_resource_limits() {+ // The rlimit mounting must reach the spawned child before its shell runs.let child = spawn(shell(), "exit 0", &tmp(), None, ShellEnv::default(), None).await.unwrap();let pid = child.id().unwrap();child.wait_with_output().await.unwrap();@@ -863,7 +865,7 @@ mod tests {// Spawn a shell that reports its own soft limits; confine_limits sets// each to the target, bounded by whatever hard limit the host allows.- // `ulimit -f` reports FSIZE in 1024-byte blocks, `-n` a raw count.+ // `ulimit -f` reports FSIZE in 1024-byte blocks; `-n` and `-u` are raw.for (name, flag, resource, target, unit) in [("NOFILE", "-n", nix::libc::RLIMIT_NOFILE, 65536_u64, 1_u64),(@@ -873,6 +875,8 @@ mod tests {16 * 1024 * 1024 * 1024,1024,),+ #[cfg(target_os = "linux")]+ ("NPROC", "-u", nix::libc::RLIMIT_NPROC, 8192_u64, 1_u64),] {let cmd = format!("ulimit {flag}");let child = spawn(shell(), &cmd, &tmp(), None, ShellEnv::default(), None).await.unwrap();