Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions crates/openhuman-core/src/integrations/composio/module_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,27 @@ 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, ()>,
}

/// Take the module lock for the rest of the test.
///
/// 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down
6 changes: 5 additions & 1 deletion crates/openhuman-core/src/modules/storage_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<Binding>, 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) {
Expand Down Expand Up @@ -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 {
Expand Down
75 changes: 75 additions & 0 deletions crates/openhuman-core/src/modules/storage_host_backend.rs
Original file line number Diff line number Diff line change
@@ -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<Arc<dyn StorageBackend>> {
#[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<Option<Arc<dyn StorageBackend>>> =
std::sync::Mutex::new(None);

#[cfg(test)]
fn test_module_storage() -> Option<Arc<dyn StorageBackend>> {
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<dyn StorageBackend>) -> 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<Arc<dyn StorageBackend>>,
}

#[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();
}
}
10 changes: 10 additions & 0 deletions scripts/ci/process-statics.json
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<Arc<dyn StorageBackend>>>",
"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": "",
Expand Down
Loading