From 68c6db0d298a853d6af487667f3f11c132f99f36 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Thu, 5 Nov 2026 03:32:24 +0530 Subject: [PATCH] fix(runtime): don't relock the config snapshot while replacing it The desktop's secret preflight replaces the embedder config snapshot once it has opened the sealed config. replace_latest_embedder_config held the snapshot's mutex and then called bind_context_snapshot, which locks the same mutex again; std's Mutex is not reentrant, so the preflight thread deadlocked and the desktop core kept serving only its approval methods. Bind the snapshot under the guard already held. --- .../src/core/runtime/context_config.rs | 4 ++- .../src/core/runtime/context_tests.rs | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/crates/openhuman-core/src/core/runtime/context_config.rs b/crates/openhuman-core/src/core/runtime/context_config.rs index 93f0a1431a7..46369694b04 100644 --- a/crates/openhuman-core/src/core/runtime/context_config.rs +++ b/crates/openhuman-core/src/core/runtime/context_config.rs @@ -43,7 +43,9 @@ impl CoreContext { if let Some(snapshot) = &self.latest_embedder_config { if let Ok(mut current) = snapshot.lock() { *current = config; - crate::config::Config::bind_context_snapshot(snapshot); + // Bound under this guard: relocking the snapshot here would + // deadlock the thread. + current.runtime_state.context_snapshot = Some(Arc::downgrade(snapshot)); } } } diff --git a/crates/openhuman-core/src/core/runtime/context_tests.rs b/crates/openhuman-core/src/core/runtime/context_tests.rs index fdc23ee4b86..1d24f70ab23 100644 --- a/crates/openhuman-core/src/core/runtime/context_tests.rs +++ b/crates/openhuman-core/src/core/runtime/context_tests.rs @@ -65,6 +65,38 @@ fn an_embedder_config_is_readable_from_the_context() { assert_eq!(read.default_model.as_deref(), Some("embedder-model")); } +/// The desktop's secret preflight replaces the snapshot once it has opened +/// the sealed config; the replacement must return and stay bound. +#[test] +fn replacing_the_embedder_config_returns_and_keeps_it_bound() { + let ctx = ctx_with_config(crate::config::Config::default()); + let mut replacement = crate::config::Config::default(); + replacement.default_model = Some("after-preflight".into()); + + let (done, finished) = std::sync::mpsc::channel(); + let replacing = Arc::clone(&ctx); + std::thread::spawn(move || { + replacing.replace_latest_embedder_config(replacement); + let _ = done.send(()); + }); + finished + .recv_timeout(std::time::Duration::from_secs(5)) + .expect("replacing the embedder config returns"); + + let read = ctx.latest_embedder_config().expect("snapshot is bound"); + assert_eq!(read.default_model.as_deref(), Some("after-preflight")); + let snapshot = ctx.latest_embedder_config.as_ref().expect("snapshot"); + let bound = snapshot + .lock() + .unwrap() + .runtime_state + .context_snapshot + .clone(); + assert!(bound + .and_then(|weak| weak.upgrade()) + .is_some_and(|bound| Arc::ptr_eq(&bound, snapshot))); +} + #[tokio::test] async fn the_current_dispatch_sees_the_scoped_embedder_config() { // This is the read path `load_config_with_timeout` uses. If it resolved