From d9e2fa14e8a2d3becf673855b451fa8ea64390d9 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Thu, 16 Jul 2026 17:44:14 +0200 Subject: [PATCH] fix: avoid local inference Metal teardown crash (#10508) Co-authored-by: Douwe M Osinga --- crates/goose-local-inference/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/goose-local-inference/src/lib.rs b/crates/goose-local-inference/src/lib.rs index e36ee9bed..7d97ea656 100644 --- a/crates/goose-local-inference/src/lib.rs +++ b/crates/goose-local-inference/src/lib.rs @@ -39,7 +39,7 @@ use rmcp::model::Tool; use serde_json::{json, Value}; use std::collections::{HashMap, HashSet}; use std::path::PathBuf; -use std::sync::{Arc, Mutex as StdMutex}; +use std::sync::{Arc, Mutex as StdMutex, Weak}; use tokio::sync::{Mutex, Notify}; use uuid::Uuid; @@ -96,17 +96,17 @@ pub fn builtin_chat_template_names() -> Vec { llamacpp::builtin_chat_template_names() } -static RUNTIME: StdMutex>> = StdMutex::new(None); +static RUNTIME: StdMutex> = StdMutex::new(Weak::new()); fn current_runtime() -> Option> { - RUNTIME.lock().expect("runtime lock poisoned").clone() + RUNTIME.lock().expect("runtime lock poisoned").upgrade() } impl InferenceRuntime { pub fn get_or_init() -> Result> { let mut guard = RUNTIME.lock().expect("runtime lock poisoned"); - if let Some(runtime) = guard.as_ref() { - return Ok(runtime.clone()); + if let Some(runtime) = guard.upgrade() { + return Ok(runtime); } let llamacpp_backend: Arc = Arc::new(LlamaCppBackend::new()?); let mlx_backend: Arc = Arc::new(MlxBackend::new()); @@ -118,7 +118,7 @@ impl InferenceRuntime { cold_load_lock: Mutex::new(()), backends, }); - *guard = Some(runtime.clone()); + *guard = Arc::downgrade(&runtime); Ok(runtime) }