Skip to content

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

fix(storage): keep a test-installed slot to its own test under cfg(test)

M3gA-Mindde984a7c370c
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index f106741cdb..0250680620 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -232,6 +232,46 @@ impl Slot {
static BACKEND: LazyLock<Slot> = LazyLock::new(Slot::default);
+/// The libtest thread (named after its test) that installed the slot, under
+/// `cfg(test)`. Unit tests share one process and run in parallel; without this,
+/// a backend one test installs becomes every concurrent test's storage
+/// ([`local::backend_for`] prefers the slot), and their rows mix and vanish
+/// when the installer restores it (MAIN-57).
+#[cfg(test)]
+static SLOT_OWNER: Mutex<Option<String>> = Mutex::new(None);
+
+#[cfg(test)]
+fn set_slot_owner(owner: Option<String>) {
+ *SLOT_OWNER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = owner;
+}
+
+/// Whether the installed slot belongs to the calling test. A slot a test
+/// installed is hidden from the other tests' threads; one installed off a test
+/// thread (a runtime worker, the storage bridge) stays visible to all, as in
+/// production.
+#[cfg(test)]
+fn slot_visible_here() -> bool {
+ let owner = SLOT_OWNER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .clone();
+ let Some(owner) = owner.filter(|owner| is_test_thread(owner)) else {
+ return true;
+ };
+ match std::thread::current().name() {
+ Some(name) if is_test_thread(name) => name == owner,
+ _ => true,
+ }
+}
+
+/// libtest names each test's thread after its path (`module::tests::name`).
+#[cfg(test)]
+fn is_test_thread(name: &str) -> bool {
+ name.contains("::")
+}
+
/// The URL the installed backend was opened from, when the host installed it
/// by URL ([`install_for_url`]). [`rebind`] compares against it; a backend
/// installed without one (a test's, an embedder's own) is never rebound.
@@ -246,6 +286,10 @@ fn set_installed_url(url: Option<String>) {
/// The URL the installed backend was opened from, if the host installed it by
/// URL.
pub fn installed_url() -> Option<String> {
+ #[cfg(test)]
+ if !slot_visible_here() {
+ return None;
+ }
INSTALLED_URL
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
@@ -350,6 +394,8 @@ where
/// Makes `backend` the process's storage backend; returns the previous one.
pub fn install(backend: Arc<dyn StorageBackend>) -> Option<Arc<dyn StorageBackend>> {
+ #[cfg(test)]
+ set_slot_owner(std::thread::current().name().map(str::to_owned));
set_installed_url(None);
let previous = BACKEND.install(backend);
// What was recorded described the previous backend.
@@ -372,11 +418,17 @@ pub fn install_for_url(
/// The installed backend, when the host configured one.
pub fn installed() -> Option<Arc<dyn StorageBackend>> {
+ #[cfg(test)]
+ if !slot_visible_here() {
+ return None;
+ }
BACKEND.installed()
}
/// Removes the installed backend; returns whether there was one.
pub fn clear() -> bool {
+ #[cfg(test)]
+ set_slot_owner(None);
set_installed_url(None);
agents::reset_recorded();
BACKEND.clear()
diff --git a/crates/openhuman-core/src/storage/mod_tests.rs b/crates/openhuman-core/src/storage/mod_tests.rs
index f83fd952f3..4194dbcc20 100644
--- a/crates/openhuman-core/src/storage/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/mod_tests.rs
@@ -400,3 +400,40 @@ mod storage_runtime {
}
}
}
+
+/// A backend one test installs is not another concurrent test's storage: the
+/// slot is visible to its installer's thread only (MAIN-57).
+#[test]
+fn a_test_installed_slot_is_hidden_from_other_test_threads() {
+ let ready = std::sync::Arc::new(std::sync::Barrier::new(2));
+ let done = std::sync::Arc::new(std::sync::Barrier::new(2));
+ let installer = {
+ let (ready, done) = (ready.clone(), done.clone());
+ std::thread::Builder::new()
+ .name("storage::slot_probe::installer".into())
+ .spawn(move || {
+ let _guard = crate::storage::test_support::install_backend_for_test(
+ crate::storage::test_support::memory_backend(),
+ );
+ let mine = crate::storage::installed().is_some();
+ ready.wait();
+ done.wait();
+ assert!(mine, "the installer sees its own slot");
+ })
+ .unwrap()
+ };
+ let other = std::thread::Builder::new()
+ .name("storage::slot_probe::other".into())
+ .spawn(move || {
+ ready.wait();
+ let seen = installed_url().is_some() || crate::storage::installed().is_some();
+ done.wait();
+ seen
+ })
+ .unwrap();
+ installer.join().unwrap();
+ assert!(
+ !other.join().unwrap(),
+ "another test's thread must not see the installer's backend"
+ );
+}