diff --git a/crates/openhuman-core/src/integrations/composio/module_client.rs b/crates/openhuman-core/src/integrations/composio/module_client.rs index f7f22220b7f..e1190f34700 100644 --- a/crates/openhuman-core/src/integrations/composio/module_client.rs +++ b/crates/openhuman-core/src/integrations/composio/module_client.rs @@ -314,9 +314,11 @@ pub(crate) static MODULE_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::cons /// The module lock, and the storage the module keeps its preferences in. #[cfg(test)] pub(crate) struct ModuleGuard { - // Fields drop in order: the previous storage slot comes back before the - // next test may take the module. - _storage: crate::storage::test_support::InstalledBackendGuard, + // Fields drop in order: the previous module storage comes back before the + // next test may take the module. Without the module loader there are no + // module calls to serve. + #[cfg(feature = "modules")] + _storage: crate::modules::storage_host::backend_choice::TestModuleStorage, _lock: tokio::sync::MutexGuard<'static, ()>, } @@ -324,13 +326,15 @@ pub(crate) struct ModuleGuard { /// /// The module refuses a preference call when the host serves no storage, and /// a module call carries no workspace for the host to fall back to, so the -/// guard installs storage as a booted host has it (memory, the one driver -/// every build has), restored on drop. +/// guard serves module calls a fresh memory backend, restored on drop. It is +/// not installed in the process slot: that would make it every concurrent +/// test's storage (MAIN-57). #[cfg(test)] pub(crate) async fn module_guard() -> ModuleGuard { let lock = module_lock_guard().await; ModuleGuard { - _storage: crate::storage::test_support::install_backend_for_test( + #[cfg(feature = "modules")] + _storage: crate::modules::storage_host::backend_choice::serve_test_module_storage( crate::storage::test_support::memory_backend(), ), _lock: lock, diff --git a/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs index 0d2a9248bc1..c79514d2823 100644 --- a/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs +++ b/crates/openhuman-core/src/integrations/composio/tools/scope_gate_tests.rs @@ -222,13 +222,12 @@ async fn a_preference_still_in_the_legacy_file_gates_execution() { /// read a stored preference, every gate that consults it denies. /// /// The module caches its link to the host's storage on first use. Saving a -/// preference while a backend is installed connects that link; removing the +/// preference while a backend is served connects that link; withdrawing the /// backend then makes the module's next preference read fail (`Unavailable`), /// as a storage outage would. /// -/// `module_guard` already holds the storage slot and installs a memory backend -/// (restored on drop), so the test must not lock the slot a second time: the -/// slot mutex is not reentrant and doing so deadlocks. +/// `module_guard` serves module calls a memory backend (restored on drop); +/// the test withdraws it rather than touching the process storage slot. #[tokio::test] async fn an_unreadable_preference_denies_at_every_gate() { use crate::integrations::composio::module_client::module_guard; @@ -246,7 +245,10 @@ async fn an_unreadable_preference_denies_at_every_gate() { only(true, false, false), ) .await - .expect("the module stores the pref while storage is installed"); + .expect("the module stores the pref while storage is served"); + #[cfg(feature = "modules")] + crate::modules::storage_host::backend_choice::withdraw_test_module_storage(); + #[cfg(not(feature = "modules"))] crate::storage::clear(); let read = diff --git a/crates/openhuman-core/src/modules/storage_host.rs b/crates/openhuman-core/src/modules/storage_host.rs index 7e00e71021e..5883da4ccbf 100644 --- a/crates/openhuman-core/src/modules/storage_host.rs +++ b/crates/openhuman-core/src/modules/storage_host.rs @@ -388,7 +388,8 @@ impl StorageHost { /// The module's binding for `scope`, built on first use and again when the /// installed backend changed. The host runs the declared schema here. async fn bind(&self, module: &str, scope: Scope) -> Result, BindError> { - let backend = self.environment.backend().ok_or(BindError::NoBackend)?; + let backend = + backend_choice::for_module(module, &*self.environment).ok_or(BindError::NoBackend)?; let key = (module.to_string(), scope.as_str().to_string()); let mut bindings = self.bindings.lock().await; if let Some(existing) = bindings.get(&key) { @@ -576,6 +577,9 @@ pub(crate) fn current_module_scope( module_scope(tenant.profile.as_deref(), module_id) } +#[path = "storage_host_backend.rs"] +pub(crate) mod backend_choice; + /// Production wiring: the broker's name owners, the installed backend, the /// host's scope rule and the compiled grants. struct Live { diff --git a/crates/openhuman-core/src/modules/storage_host_backend.rs b/crates/openhuman-core/src/modules/storage_host_backend.rs new file mode 100644 index 00000000000..141e4ab9414 --- /dev/null +++ b/crates/openhuman-core/src/modules/storage_host_backend.rs @@ -0,0 +1,75 @@ +//! Which backend a module's storage binds to. Under `cfg(test)` a connector +//! module test serves its own storage here while it holds the module lock +//! (`module_guard`), kept out of the process storage slot so it is never +//! another concurrent test's storage (MAIN-57). + +use std::sync::Arc; + +use crate::storage::StorageBackend; + +/// The backend `module`'s storage binds to: the host's, or under `cfg(test)` +/// the storage a connector-module test is serving. +pub(super) fn for_module( + module: &str, + environment: &dyn super::Environment, +) -> Option> { + #[cfg(test)] + if module == super::super::connectors::MODULE_ID { + if let Some(backend) = test_module_storage() { + return Some(backend); + } + } + let _ = module; + environment.backend() +} + +#[cfg(test)] +/// Storage for module calls in unit tests that hold a module lock (the +/// connector module's `module_guard`). A module call carries no workspace, so +/// such a test needs a backend; this one is read only here, so it is never +/// another test's storage the way an installed process slot is (MAIN-57). +static TEST_MODULE_STORAGE: std::sync::Mutex>> = + std::sync::Mutex::new(None); + +#[cfg(test)] +fn test_module_storage() -> Option> { + TEST_MODULE_STORAGE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .clone() +} + +#[cfg(test)] +/// Serve `backend` to module calls until the returned guard drops. Hold the +/// module's test lock for as long as it lives. +pub(crate) fn serve_test_module_storage(backend: Arc) -> TestModuleStorage { + let previous = TEST_MODULE_STORAGE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .replace(backend); + TestModuleStorage { previous } +} + +#[cfg(test)] +/// Withdraw the backend module calls are served, as a storage outage would, +/// while the guard from [`serve_test_module_storage`] is still held. +pub(crate) fn withdraw_test_module_storage() { + *TEST_MODULE_STORAGE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = None; +} + +#[cfg(test)] +/// Restores the previous test module storage on drop. +pub(crate) struct TestModuleStorage { + previous: Option>, +} + +#[cfg(test)] +impl Drop for TestModuleStorage { + fn drop(&mut self) { + *TEST_MODULE_STORAGE + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = self.previous.take(); + } +} diff --git a/scripts/ci/process-statics.json b/scripts/ci/process-statics.json index ed07402fb93..f223f01b7ac 100644 --- a/scripts/ci/process-statics.json +++ b/scripts/ci/process-statics.json @@ -399,6 +399,16 @@ "reason": "The single tinybus module bus (broker, loader, connection) for the process; tinybus never unloads a module, so there is exactly one. Carries no tenant data: tenant scope is resolved per call by the host callbacks it serves.", "verification": "modules/runtime_tests.rs: the_module_bus_is_a_singleton_and_serves_proxies" }, + { + "file": "crates/openhuman-core/src/modules/storage_host_backend.rs", + "scope": "", + "name": "TEST_MODULE_STORAGE", + "type": "std::sync::Mutex>>", + "count": 1, + "category": "A", + "reason": "Test-only (cfg(test)): the backend module calls are served while a unit test holds a module lock (composio module_guard), so that test's module storage is never installed in the process slot, where it would be every concurrent test's storage (MAIN-57). Set and restored only under the module's test lock; no tenant state, not compiled into any shipped build.", + "verification": "integrations/composio/ops/scope_prefs_tests.rs: a_saved_pref_survives_a_deferred_legacy_migration" + }, { "file": "crates/openhuman-core/src/modules/wallet_backend.rs", "scope": "",