Skip to content

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

fix(storage): rebind tenant routers without deadlocking

Steven Enamakel400f0e303233
diff --git a/crates/openhuman-core/src/storage/rebind.rs b/crates/openhuman-core/src/storage/rebind.rs
index 36ea57fd9d..c162053d4b 100644
--- a/crates/openhuman-core/src/storage/rebind.rs
+++ b/crates/openhuman-core/src/storage/rebind.rs
@@ -7,7 +7,7 @@
//! differs from the one the host installed:
//!
//! 1. opens the new backend;
-//! 2. points the session store (`ReboundSessionStores`) and the process's
+//! 2. points the session-store tenant routers and the process's
//! storage slot at it, together, so no store reads one workspace's
//! sessions beside another's documents;
//! 3. runs the read-only legacy importers over the new workspace, since a
diff --git a/crates/openhuman-core/src/storage/sessions/tenant_router.rs b/crates/openhuman-core/src/storage/sessions/tenant_router.rs
index d99202759f..583ced0377 100644
--- a/crates/openhuman-core/src/storage/sessions/tenant_router.rs
+++ b/crates/openhuman-core/src/storage/sessions/tenant_router.rs
@@ -22,11 +22,27 @@ use tinystoragedrivers::{Scope, StorageBackend, StorageError};
#[derive(Default)]
struct TenantStoreRegistry {
providers: Arc<SessionStoreRegistry>,
- backends: Mutex<Vec<Weak<RwLock<Arc<dyn StorageBackend>>>>>,
+ state: Mutex<TenantStoreRegistryState>,
+}
+
+#[derive(Default)]
+struct TenantStoreRegistryState {
+ backends: Vec<Weak<RwLock<Arc<dyn StorageBackend>>>>,
+ current_backend: Option<Arc<dyn StorageBackend>>,
}
impl TenantStoreRegistry {
fn rebind(&self, backend: &Arc<dyn StorageBackend>) -> usize {
+ let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
+ let routers = state
+ .backends
+ .iter()
+ .filter_map(Weak::upgrade)
+ .collect::<Vec<_>>();
+ let mut bindings = routers
+ .iter()
+ .map(|router| router.write().unwrap_or_else(PoisonError::into_inner))
+ .collect::<Vec<_>>();
let rebound = match self.providers.rebind(Arc::clone(backend)) {
Ok(rebound) => rebound,
Err(error) => {
@@ -34,22 +50,21 @@ impl TenantStoreRegistry {
return 0;
}
};
- let mut live = self.backends.lock().unwrap_or_else(PoisonError::into_inner);
- live.retain(|weak| {
- if let Some(current) = weak.upgrade() {
- *current.write().unwrap_or_else(PoisonError::into_inner) = Arc::clone(backend);
- true
- } else {
- false
- }
- });
+ for current in &mut bindings {
+ **current = Arc::clone(backend);
+ }
+ state.backends.retain(|weak| weak.strong_count() > 0);
+ state.current_backend = Some(Arc::clone(backend));
rebound
}
fn register_backend(&self, backend: &Arc<RwLock<Arc<dyn StorageBackend>>>) {
- let mut live = self.backends.lock().unwrap_or_else(PoisonError::into_inner);
- live.retain(|weak| weak.strong_count() > 0);
- live.push(Arc::downgrade(backend));
+ let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
+ if let Some(current) = &state.current_backend {
+ *backend.write().unwrap_or_else(PoisonError::into_inner) = Arc::clone(current);
+ }
+ state.backends.retain(|weak| weak.strong_count() > 0);
+ state.backends.push(Arc::downgrade(backend));
}
}
@@ -219,9 +234,8 @@ impl SessionStoreProvider for TenantSessionStores {
backend,
self.recover_on_open,
admission,
- Arc::clone(&self.registry),
+ Arc::new(TenantStoreRegistry::default()),
);
- self.registry.register_backend(&actor.backend);
Ok(Some(Arc::new(actor)))
}
diff --git a/crates/openhuman-embed/src/host_internals.rs b/crates/openhuman-embed/src/host_internals.rs
index 1c0135973d..4d8b1cd1c8 100644
--- a/crates/openhuman-embed/src/host_internals.rs
+++ b/crates/openhuman-embed/src/host_internals.rs
@@ -187,8 +187,7 @@ pub mod agent {
}
pub mod session_store {
pub use openhuman_core::storage::sessions::{
- context_workspace_dir, install, installed, restore, ReboundSessionStores,
- WorkspaceSessionStores,
+ context_workspace_dir, install, installed, restore, WorkspaceSessionStores,
};
}
}