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