Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(engine): acquire switch lock before config load in engine_set

Steven Enamakel2463f0821cea
diff --git a/crates/openhuman-core/src/memory/ops/engine.rs b/crates/openhuman-core/src/memory/ops/engine.rs
index c1d61c5d3a..b3be71cc0d 100644
--- a/crates/openhuman-core/src/memory/ops/engine.rs
+++ b/crates/openhuman-core/src/memory/ops/engine.rs
@@ -520,8 +520,15 @@ pub async fn memory_engine_get() -> Result<RpcOutcome<EngineState>, String> {
pub async fn memory_engine_set(
params: EngineTargetParams,
) -> Result<RpcOutcome<EngineState>, String> {
- let config = load_config().await?;
let prepared = prepare_target(params)?;
+ // One switch at a time, and never while a migration owns the switch.
+ let _switch = SWITCH_LOCK.lock().await;
+ if super::engine_migrate::migration_running() {
+ return Err(
+ "a memory migration is running; wait for it to finish or cancel it before switching"
+ .to_string(),
+ );
+ }
log::debug!(
"{LOG_PREFIX} engine_set engine='{}' has_key={} has_endpoint={}",
prepared.id,
@@ -531,15 +538,9 @@ pub async fn memory_engine_set(
if prepared.id != MODULE_ID {
// Fail before touching the keychain or config when the engine cannot
// even be built (no transport, missing endpoint/key, bad deployment).
+ let config = load_config().await?;
build_target_provider(&config, &prepared)?;
}
- let _switch = SWITCH_LOCK.lock().await;
- if super::engine_migrate::migration_running() {
- return Err(
- "a memory migration is running; wait for it to finish or cancel it before switching"
- .to_string(),
- );
- }
let state = commit_engine_locked(&prepared).await?;
Ok(RpcOutcome::new(state, vec![]))
}