The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
fix(agent): the Python tool had the shell tool's deadlock too
purple86fa48fded0f
diff --git a/src-tauri/src/commands/agent.rs b/src-tauri/src/commands/agent.rsindex 4ff682a9..4997d72d 100644--- a/src-tauri/src/commands/agent.rs+++ b/src-tauri/src/commands/agent.rs@@ -1,5 +1,4 @@use std::fs;-use std::io::Read;use std::path::PathBuf;use std::process::{Command, Stdio};@@ -315,6 +314,13 @@ pub fn execute_code(let mut child = cmd.spawn().map_err(|e| format!("Spawn Python: {}", e))?;+ // Both pipes are drained on their own threads from here on. A script that+ // prints more than a pipe buffer would otherwise block on write and never+ // exit — the tool call ate the full timeout and returned nothing. Same+ // machinery as the shell tool (commands/shell.rs).+ let (out_buf, out_done) = super::shell::drain(child.stdout.take().expect("stdout is piped"));+ let (err_buf, err_done) = super::shell::drain(child.stderr.take().expect("stderr is piped"));+// Poll-based timeout since std::process::Child has no wait_timeoutlet start = std::time::Instant::now();let timeout_dur = std::time::Duration::from_millis(timeout_ms);@@ -322,30 +328,30 @@ pub fn execute_code(loop {match child.try_wait() {Ok(Some(status)) => {- let mut stdout_str = String::new();- let mut stderr_str = String::new();- if let Some(mut stdout) = child.stdout.take() {- let _ = stdout.read_to_string(&mut stdout_str);- }- if let Some(mut stderr) = child.stderr.take() {- let _ = stderr.read_to_string(&mut stderr_str);- }-+ super::shell::settle(&out_done, &err_done, std::time::Duration::from_millis(500));let _ = fs::remove_file(&script_path);return Ok(serde_json::json!({- "stdout": stdout_str,- "stderr": stderr_str,+ "stdout": super::shell::captured_text(&out_buf),+ "stderr": super::shell::captured_text(&err_buf),"exitCode": status.code().unwrap_or(-1),"timedOut": false,}));}Ok(None) => {if start.elapsed() > timeout_dur {+ super::shell::kill_tree(child.id());let _ = child.kill();+ let _ = child.wait();+ super::shell::settle(&out_done, &err_done, std::time::Duration::from_millis(200));let _ = fs::remove_file(&script_path);+ let mut stderr_str = super::shell::captured_text(&err_buf);+ if !stderr_str.is_empty() {+ stderr_str.push('\n');+ }+ stderr_str.push_str(&format!("Execution timed out after {}ms", timeout_ms));return Ok(serde_json::json!({- "stdout": "",- "stderr": format!("Execution timed out after {}ms", timeout_ms),+ "stdout": super::shell::captured_text(&out_buf),+ "stderr": stderr_str,"exitCode": -1,"timedOut": true,}));diff --git a/src-tauri/src/commands/shell.rs b/src-tauri/src/commands/shell.rsindex 0bcd68d3..80c3dda1 100644--- a/src-tauri/src/commands/shell.rs+++ b/src-tauri/src/commands/shell.rs@@ -35,7 +35,7 @@ fn workspace_cwd(chat_id: Option<&str>) -> PathBuf {const MAX_CAPTURE: usize = 256 * 1024;#[derive(Default)]-struct Captured {+pub(crate) struct Captured {kept: Vec<u8>,total: usize,}@@ -45,7 +45,7 @@ struct Captured {/// blocks on write forever. Reading only after `try_wait()` reports an exit/// therefore deadlocks on any command with real output — it hit the full timeout/// and returned nothing at all.-fn drain(mut pipe: impl Read + Send + 'static) -> (Arc<Mutex<Captured>>, Arc<AtomicBool>) {+pub(crate) fn drain(mut pipe: impl Read + Send + 'static) -> (Arc<Mutex<Captured>>, Arc<AtomicBool>) {let buf = Arc::new(Mutex::new(Captured::default()));let done = Arc::new(AtomicBool::new(false));let sink = Arc::clone(&buf);@@ -74,7 +74,7 @@ fn drain(mut pipe: impl Read + Send + 'static) -> (Arc<Mutex<Captured>>, Arc<Ato/// Decode captured bytes leniently. Build tools on a non-UTF-8 Windows codepage/// emit bytes `read_to_string` rejects outright — that used to throw the whole/// output away and hand the model an empty string next to a successful exit code.-fn captured_text(buf: &Arc<Mutex<Captured>>) -> String {+pub(crate) fn captured_text(buf: &Arc<Mutex<Captured>>) -> String {let c = match buf.lock() {Ok(c) => c,Err(poisoned) => poisoned.into_inner(),@@ -91,7 +91,7 @@ fn captured_text(buf: &Arc<Mutex<Captured>>) -> String {}/// Every process descending from `root`, deepest last.-fn descendants(root: u32, sys: &sysinfo::System) -> Vec<u32> {+pub(crate) fn descendants(root: u32, sys: &sysinfo::System) -> Vec<u32> {let mut children: std::collections::HashMap<u32, Vec<u32>> = std::collections::HashMap::new();for (pid, proc_) in sys.processes() {if let Some(parent) = proc_.parent() {@@ -119,7 +119,7 @@ fn descendants(root: u32, sys: &sysinfo::System) -> Vec<u32> {/// shell itself, so a timed-out `npm run dev`, build script or spawned server/// kept running after the tool call gave up — still holding its port and CPU,/// and still writing into a pipe nobody reads.-fn kill_tree(root: u32) {+pub(crate) fn kill_tree(root: u32) {use sysinfo::{Pid, ProcessesToUpdate, System};let mut sys = System::new();sys.refresh_processes(ProcessesToUpdate::All, true);@@ -137,7 +137,7 @@ fn kill_tree(root: u32) {/// Give the reader threads a moment to hit EOF after the child is gone. Never/// joins them: a grandchild can keep the pipe open (a spawned dev server), and/// joining would hang the command instead of returning what we already have.-fn settle(a: &Arc<AtomicBool>, b: &Arc<AtomicBool>, max: std::time::Duration) {+pub(crate) fn settle(a: &Arc<AtomicBool>, b: &Arc<AtomicBool>, max: std::time::Duration) {let deadline = std::time::Instant::now() + max;while std::time::Instant::now() < deadline {if a.load(Ordering::Acquire) && b.load(Ordering::Acquire) {