From 8a2f31c2c4720a0bd32d97bf6df273a14220aed9 Mon Sep 17 00:00:00 2001 From: storybored379 Date: Sat, 3 Oct 2026 22:01:16 -0700 Subject: [PATCH] MCP: a panicking command no longer wedges the headless session (#16) headless_op ran every tool under one Mutex and mapped a poisoned lock to "session lock poisoned". So after any command panicked (e.g. a bug in a filter), every later tool call (doc_inspect, doc_new, doc_save...) failed for the rest of the MCP session, losing unsaved work. Edits run on a copy of the document (Session::edit), so the session is still consistent after a panic: recover the guard with PoisonError::into_inner and keep serving. The panicking call itself still returns an error. 1 test (automation server). Co-authored-by: storybored379 Co-authored-by: Claude Opus 5.5 (1M context) --- crates/automation/src/server.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs index be42e89d41..45ab3e2035 100644 --- a/crates/automation/src/server.rs +++ b/crates/automation/src/server.rs @@ -2,7 +2,7 @@ //! are not valid in every client's tool-name grammar). use std::path::PathBuf; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, PoisonError}; use base64::Engine as _; use rmcp::handler::server::router::tool::ToolRouter; @@ -228,9 +228,10 @@ impl PhotocraftMcp { let h = h.clone(); Some( tokio::task::spawn_blocking(move || { - let mut g = h - .lock() - .map_err(|_| AutomationError::Other("session lock poisoned".into()))?; + // A panicking command poisons the lock, but edits run on a copy + // of the document (`Session::edit`), so the session is still + // consistent: keep serving instead of failing every later call. + let mut g = h.lock().unwrap_or_else(PoisonError::into_inner); f(&mut g) }) .await @@ -672,3 +673,23 @@ pub fn render_document_png( ) -> Result, AutomationError> { files::render_png(doc, max_side) } + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test(flavor = "multi_thread")] + async fn a_panicking_command_does_not_wedge_the_session() { + let mcp = PhotocraftMcp::headless(); + let r = mcp + .headless_op(|_| -> Result<(), AutomationError> { panic!("boom") }) + .await + .unwrap(); + assert!(r.is_err()); + let r = mcp + .headless_op(|h| h.command_run("file.new", json!({"width": 8, "height": 8}))) + .await + .unwrap(); + assert!(r.is_ok(), "session still usable after a panic: {r:?}"); + } +}