Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent): run tool children in their own session so they cannot stop the TUI
James Dam069520d01e6c
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rsindex 3c5d7c771..ea10d1533 100644--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/tools/handlers.rs@@ -1653,7 +1653,12 @@ pub async fn render_html_png(c.args(&chrome_args);c};+ // Chrome gets no stdin and no controlling terminal: left alone it would sit+ // in Jan's foreground process group holding the TUI's terminal.+ #[cfg(unix)]+ proc::detach_from_terminal(&mut launcher);let mut child = match launcher+ .stdin(std::process::Stdio::null()).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::piped()).spawn()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 9496bd1c8..1601dd4fe 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@@ -712,9 +712,7 @@ pub async fn spawn_with_stdin(#[cfg(unix)]fn set_process_group(cmd: &mut Command) {- // pgid 0 => the child becomes leader of a new group whose id equals its pid,- // so `kill_tree(pid)` can signal the whole group.- cmd.process_group(0);+ detach_from_terminal(cmd);}#[cfg(windows)]@@ -723,10 +721,44 @@ fn set_process_group(cmd: &mut Command) {cmd.creation_flags(CREATE_NEW_PROCESS_GROUP);}+/// Start the child in a new session (`setsid`), so it leads a new process group+/// (pgid == pid, which [`kill_tree`] relies on) **and** has no controlling+/// terminal.+///+/// A new process group alone (`process_group(0)`) is not enough when Jan runs+/// in a terminal: the child stays in Jan's session, so anything it starts can+/// open `/dev/tty` and make itself the terminal's foreground group with+/// `tcsetpgrp` (a job-control program blocking SIGTTOU does exactly that).+/// Once it exits, the foreground points at a dead group and the TUI's next+/// read of the terminal stops Jan with SIGTTIN -- the shell then reports+/// `suspended (tty input)` and the session looks dead. Without a controlling+/// terminal, opening `/dev/tty` fails (ENXIO), so a child can never take Jan's+/// terminal; a tool that insists on prompting there fails fast instead of+/// stopping or hanging.+///+/// This replaces `process_group(0)` rather than adding to it: std applies the+/// process group before the `pre_exec` hooks, and `setsid` refuses (EPERM) in a+/// process that already leads a group.+#[cfg(unix)]+pub fn detach_from_terminal(cmd: &mut Command) {+ // # Safety: runs in the forked child before exec; `setsid` is+ // async-signal-safe. It can only fail if the child already leads a process+ // group, which a freshly forked child never does; failing the spawn then is+ // better than silently sharing Jan's terminal.+ unsafe {+ cmd.pre_exec(|| {+ if nix::libc::setsid() == -1 {+ return Err(std::io::Error::last_os_error());+ }+ Ok(())+ });+ }+}+/// Kill the process `pid` and every descendant it spawned.///-/// Every child we register is spawned with `process_group(0)` (see-/// [`set_process_group`]), so its pgid equals its pid and `killpg` reaps the+/// Every child we register is spawned in its own session (see+/// [`detach_from_terminal`]), so its pgid equals its pid and `killpg` reaps the/// whole tree. There is deliberately no bare-`kill(pid)` fallback: `killpg`/// fails only when the group is already gone (ESRCH) or we lack permission/// (EPERM, which a single `kill` would hit too), so the fallback could never@@ -1271,6 +1303,47 @@ mod tests {assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), expected);}+ /// The child leads its own session and process group: `kill_tree` still+ /// reaches the whole tree, and it is outside Jan's session, so it cannot+ /// take Jan's terminal.+ #[tokio::test]+ async fn spawned_child_leads_its_own_session() {+ use nix::unistd::{getpgid, getsid, Pid};+ let mut child = spawn(shell(), "sleep 30", &tmp(), None, ShellEnv::default(), None)+ .await+ .unwrap();+ let pid = Pid::from_raw(child.id().unwrap() as i32);+ let (sid, pgid) = (getsid(Some(pid)), getpgid(Some(pid)));+ kill_tree(pid.as_raw() as u32);+ let _ = child.wait().await;+ unregister(None, pid.as_raw() as u32);+ assert_eq!(sid.unwrap(), pid, "child must lead a new session");+ assert_eq!(pgid.unwrap(), pid, "child must lead its process group");+ assert_ne!(sid.unwrap(), getsid(None).unwrap());+ }++ /// The SIGTTIN stop behind a "dead" TUI session: a tool child that could+ /// open `/dev/tty` could also make itself the terminal's foreground group+ /// and leave Jan reading from the background. Without a controlling+ /// terminal the open fails, whether or not Jan itself has one.+ #[tokio::test]+ async fn spawned_child_cannot_open_the_terminal() {+ let child = spawn(+ shell(),+ "if (: </dev/tty) 2>/dev/null; then echo opened; else echo denied; fi",+ &tmp(),+ None,+ ShellEnv::default(),+ None,+ )+ .await+ .unwrap();+ let pid = child.id().unwrap();+ let out = child.wait_with_output().await.unwrap();+ unregister(None, pid);+ assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "denied");+ }+#[tokio::test]async fn kill_tree_reaps_backgrounded_grandchild() {// The shell backgrounds a long sleeper, prints its pid, then waits ondiff --git a/src-tauri/src/core/cli/mcp.rs b/src-tauri/src/core/cli/mcp.rsindex 46e5c817d..098ad6086 100644--- a/src-tauri/src/core/cli/mcp.rs+++ b/src-tauri/src/core/cli/mcp.rs@@ -526,10 +526,10 @@ fn stdio_command(params: &McpServerConfig, scrub: &[String]) -> Command {use std::os::windows::process::CommandExt;cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW}+ // Its own session: pgid == pid for `kill_process_tree`, and no controlling+ // terminal, so a server cannot take the TUI's terminal away from Jan.#[cfg(unix)]- {- cmd.process_group(0);- }+ tauri_plugin_agent_tools::tools::proc::detach_from_terminal(&mut cmd);cmd.kill_on_drop(true);for arg in params.args.iter().filter_map(Value::as_str) {cmd.arg(arg);diff --git a/src-tauri/src/core/mcp/helpers.rs b/src-tauri/src/core/mcp/helpers.rsindex ab1c3d03a..9ad59bfab 100644--- a/src-tauri/src/core/mcp/helpers.rs+++ b/src-tauri/src/core/mcp/helpers.rs@@ -546,11 +546,10 @@ async fn schedule_mcp_start_task<R: Runtime>(}// Make the child a process-group leader (pgid == pid) so teardown can// signal the whole tree; MCP bridges fork grandchildren that hold the- // port and outlive a single-PID kill.+ // port and outlive a single-PID kill. A new session, not just a new+ // group, so the server can never take over a terminal Jan runs in.#[cfg(unix)]- {- cmd.process_group(0);- }+ tauri_plugin_agent_tools::tools::proc::detach_from_terminal(&mut cmd);cmd.kill_on_drop(true);config_params.args