MCP: a panicking command no longer wedges the headless session (#16)

headless_op ran every tool under one Mutex<Headless> 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 <[email protected]>
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-10-04 01:01:16 -04:00
1 parent b6a95b6c8d
commit 8a2f31c2c4
1 file changed
+25 -4
+25 -4
View File
@@ -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<Vec<u8>, 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:?}");
}
}