Skip to content

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

test(storage): add deadlock test for sync fallback

Steven Enamakel4f0ee1f6dcda
diff --git a/crates/openhuman-core/src/storage/local_tests.rs b/crates/openhuman-core/src/storage/local_tests.rs
index 42d9992fc7..f74ed4949a 100644
--- a/crates/openhuman-core/src/storage/local_tests.rs
+++ b/crates/openhuman-core/src/storage/local_tests.rs
@@ -87,3 +87,82 @@ async fn concurrent_sync_and_async_open_share_one_workspace_backend() {
"later sync opens must reuse the initialized backend"
);
}
+
+#[test]
+fn current_thread_sync_fallback_does_not_deadlock_on_async_backend_initialization() {
+ const CHILD: &str = "OPENHUMAN_STORAGE_LOCAL_ASYNC_INIT_CHILD";
+ const TEST: &str =
+ "current_thread_sync_fallback_does_not_deadlock_on_async_backend_initialization";
+ let test = format!("{}::{TEST}", module_path!().split_once("::").unwrap().1);
+
+ if std::env::var(CHILD).as_deref() != Ok(test.as_str()) {
+ let mut child = std::process::Command::new(std::env::current_exe().unwrap())
+ .args(["--exact", &test, "--nocapture", "--test-threads=1"])
+ .env(CHILD, &test)
+ .stdout(std::process::Stdio::piped())
+ .stderr(std::process::Stdio::piped())
+ .spawn()
+ .unwrap();
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(45);
+ loop {
+ if child.try_wait().unwrap().is_some() {
+ let output = child.wait_with_output().unwrap();
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ let stderr = String::from_utf8_lossy(&output.stderr);
+ assert!(
+ output.status.success() && stdout.contains("1 passed"),
+ "the bounded child must run and pass exactly one test; status={}\nstdout:\n{stdout}\nstderr:\n{stderr}",
+ output.status
+ );
+ return;
+ }
+ if std::time::Instant::now() >= deadline {
+ child.kill().unwrap();
+ let output = child.wait_with_output().unwrap();
+ panic!(
+ "current-thread storage initialization deadlocked; child exceeded 45s\nstdout:\n{}\nstderr:\n{}",
+ String::from_utf8_lossy(&output.stdout),
+ String::from_utf8_lossy(&output.stderr)
+ );
+ }
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ }
+
+ let runtime = tokio::runtime::Builder::new_current_thread()
+ .enable_all()
+ .build()
+ .unwrap();
+ runtime.block_on(async {
+ let dir = tempfile::tempdir().unwrap();
+ let workspace_for_async = dir.path().to_path_buf();
+ let (started_tx, started_rx) = tokio::sync::oneshot::channel();
+ let (release_tx, release_rx) = tokio::sync::oneshot::channel();
+ super::install_test_backend_open_gate(started_tx, release_rx);
+
+ let asynchronous =
+ tokio::spawn(async move { backend_for_async(&workspace_for_async).unwrap() });
+ started_rx
+ .await
+ .expect("async initialization claimed the workspace cell");
+
+ let (sync_entry_tx, sync_entry_rx) = std::sync::mpsc::channel();
+ super::arm_test_sync_backend_entry_signal(sync_entry_tx);
+ let releaser = std::thread::spawn(move || {
+ sync_entry_rx
+ .recv_timeout(std::time::Duration::from_secs(30))
+ .expect("the synchronous fallback reached backend_for");
+ release_tx
+ .send(())
+ .expect("the gated backend initializer is still waiting");
+ });
+
+ // This intentionally blocks the current-thread runtime. The async
+ // initializer must therefore be owned by the storage bridge instead
+ // of waiting for a task queued on this same runtime.
+ let synchronous = backend_for(dir.path()).unwrap();
+ let asynchronous = asynchronous.await.unwrap();
+ releaser.join().unwrap();
+ assert!(Arc::ptr_eq(&synchronous, &asynchronous));
+ });
+}