Skip to content

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

test(storage): reproduce the blocking-bridge deadlock on a capped host runtime

oxoxDev75e396212793
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index ea71c53a5d..0894929e00 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -176,7 +176,9 @@ pub async fn open(url: &str) -> Result<Arc<dyn StorageBackend>, StorageError> {
);
// Every opened backend checks the process's lease fences on write; with
// no fence registered (every single-user host) that is a pass-through.
- let backend = tinystoragedrivers::open(&parsed).await?;
+ let backend =
+ connect_on_storage_runtime(move || async move { tinystoragedrivers::open(&parsed).await })
+ .await??;
// Schema, scope move and migrations run on the raw backend, before any
// lease fence exists to refuse them.
let report = boot::prepare_and_migrate(&backend).await?;
@@ -190,6 +192,20 @@ pub async fn open(url: &str) -> Result<Arc<dyn StorageBackend>, StorageError> {
Ok(fenced_backend::wrap(backend))
}
+/// Runs `connect` to completion and returns what it built.
+///
+/// # Errors
+///
+/// When `connect` panics or cannot be run.
+pub(crate) async fn connect_on_storage_runtime<F, Fut, T>(connect: F) -> Result<T, StorageError>
+where
+ F: FnOnce() -> Fut + Send + 'static,
+ Fut: Future<Output = T> + Send + 'static,
+ T: Send + 'static,
+{
+ Ok(connect().await)
+}
+
/// Makes `backend` the process's storage backend; returns the previous one.
pub fn install(backend: Arc<dyn StorageBackend>) -> Option<Arc<dyn StorageBackend>> {
set_installed_url(None);
diff --git a/crates/openhuman-core/src/storage/mod_tests.rs b/crates/openhuman-core/src/storage/mod_tests.rs
index e3b803dc68..5b4bd75ced 100644
--- a/crates/openhuman-core/src/storage/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/mod_tests.rs
@@ -175,3 +175,108 @@ async fn the_driver_name_check_agrees_with_what_each_backend_claims() {
);
}
}
+
+mod storage_runtime {
+ use super::super::{block_on, connect_on_storage_runtime};
+ use super::StorageError;
+ use std::sync::mpsc as std_mpsc;
+ use std::time::Duration;
+ use tokio::sync::{mpsc, oneshot};
+
+ #[derive(Clone)]
+ struct FakePool {
+ requests: mpsc::UnboundedSender<oneshot::Sender<u32>>,
+ }
+
+ impl FakePool {
+ fn spawn() -> Self {
+ let (requests, mut receiver) = mpsc::unbounded_channel::<oneshot::Sender<u32>>();
+ tokio::spawn(async move {
+ while let Some(reply) = receiver.recv().await {
+ let _ = reply.send(7);
+ }
+ });
+ Self { requests }
+ }
+
+ async fn ping(&self) -> Result<u32, StorageError> {
+ let (reply, answer) = oneshot::channel();
+ self.requests
+ .send(reply)
+ .map_err(|_| StorageError::backend("fake pool stopped"))?;
+ answer
+ .await
+ .map_err(|_| StorageError::backend("fake pool dropped the request"))
+ }
+ }
+
+ #[tokio::test]
+ async fn returns_what_connect_built() {
+ let value = connect_on_storage_runtime(|| async { 41 + 1 })
+ .await
+ .expect("connect runs");
+ assert_eq!(value, 42);
+ }
+
+ #[test]
+ fn runs_from_inside_the_blocking_bridge() {
+ let value = block_on(async {
+ let pool = connect_on_storage_runtime(|| async { FakePool::spawn() }).await?;
+ pool.ping().await
+ })
+ .expect("bridged connect completes");
+ assert_eq!(value, 7);
+ }
+
+ #[tokio::test]
+ async fn a_panicking_connect_is_a_storage_error() {
+ let result = connect_on_storage_runtime(|| async {
+ if true {
+ panic!("connect blew up");
+ }
+ })
+ .await;
+ assert!(result.is_err(), "a panic surfaces as a StorageError");
+ }
+
+ #[test]
+ fn bridged_calls_do_not_starve_a_capped_host_runtime() {
+ let (done, outcome) = std_mpsc::channel();
+ let host = std::thread::spawn(move || {
+ let runtime = tokio::runtime::Builder::new_multi_thread()
+ .worker_threads(2)
+ .max_blocking_threads(1)
+ .enable_all()
+ .build()
+ .expect("host runtime");
+ let result = runtime.block_on(async {
+ let pool = connect_on_storage_runtime(|| async { FakePool::spawn() })
+ .await
+ .expect("connect");
+ let calls: Vec<_> = (0..8)
+ .map(|_| {
+ let pool = pool.clone();
+ tokio::spawn(async move { block_on(async move { pool.ping().await }) })
+ })
+ .collect();
+ let mut answers = Vec::new();
+ for call in calls {
+ answers.push(call.await.expect("call task").expect("ping"));
+ }
+ answers
+ });
+ let _ = done.send(result);
+ runtime
+ });
+ match outcome.recv_timeout(Duration::from_secs(20)) {
+ Ok(answers) => {
+ assert_eq!(answers, vec![7; 8]);
+ drop(host.join().expect("host thread"));
+ }
+ Err(_) => {
+ std::mem::forget(host);
+ panic!("storage bridge deadlocked");
+ }
+ }
+ }
+}