Skip to content

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

refactor(channel_runtime): extract cleanup join into a helper

Steven Enamakel7e3c03340c8c
diff --git a/crates/openhuman-core/src/modules/channel_runtime.rs b/crates/openhuman-core/src/modules/channel_runtime.rs
index 5920c511dc..e27b448b6c 100644
--- a/crates/openhuman-core/src/modules/channel_runtime.rs
+++ b/crates/openhuman-core/src/modules/channel_runtime.rs
@@ -178,19 +178,25 @@ impl ChannelRuntimeLease {
if self.0.cleanup.closed.load(Ordering::Acquire) {
return Ok(());
}
- let mut closing = self.0.closing.lock().await;
- if closing.is_none() {
+ join_cleanup(&self.0.closing, || {
let cleanup = self.0.cleanup.clone();
- *closing = Some(self.0.runtime.spawn(async move { cleanup.run().await }));
- }
- let result = tokio::time::timeout(CLOSE_BUDGET, closing.as_mut().expect("cleanup worker"))
- .await
- .map_err(|_| ModuleCallError::TransportFailed)?;
- closing.take();
- result.map_err(|_| ModuleCallError::TransportFailed)?
+ self.0.runtime.spawn(async move { cleanup.run().await })
+ }).await
}
}
+async fn join_cleanup(
+ closing: &Mutex<Option<tokio::task::JoinHandle<Result<(), ModuleCallError>>>>,
+ start: impl FnOnce() -> tokio::task::JoinHandle<Result<(), ModuleCallError>>,
+) -> Result<(), ModuleCallError> {
+ let mut closing = closing.lock().await;
+ if closing.is_none() { *closing = Some(start()); }
+ let result = tokio::time::timeout(CLOSE_BUDGET, closing.as_mut().expect("cleanup worker"))
+ .await.map_err(|_| ModuleCallError::TransportFailed)?;
+ closing.take();
+ result.map_err(|_| ModuleCallError::TransportFailed)?
+}
+
fn validate_reservation(reservation: &ChannelRuntimeReservation) -> Result<(), ModuleCallError> {
if reservation.handle.len() != 32
|| !reservation
diff --git a/crates/openhuman-core/src/modules/channel_runtime_tests.rs b/crates/openhuman-core/src/modules/channel_runtime_tests.rs
index 6a02ef2a3b..e5a6bc9645 100644
--- a/crates/openhuman-core/src/modules/channel_runtime_tests.rs
+++ b/crates/openhuman-core/src/modules/channel_runtime_tests.rs
@@ -47,3 +47,44 @@ fn malformed_handles_cannot_replace_another_callback() {
);
}
}
+
+#[tokio::test]
+async fn cancelled_close_wait_retains_the_original_cleanup_worker() {
+ let closing = Arc::new(Mutex::new(None));
+ let started = Arc::new(tokio::sync::Notify::new());
+ let release = Arc::new(tokio::sync::Notify::new());
+ let acknowledged = Arc::new(AtomicBool::new(false));
+ let waiter = tokio::spawn({
+ let closing = closing.clone();
+ let started = started.clone();
+ let release = release.clone();
+ let acknowledged = acknowledged.clone();
+ async move {
+ join_cleanup(&closing, || tokio::spawn(async move {
+ started.notify_one();
+ release.notified().await;
+ acknowledged.store(true, Ordering::Release);
+ Ok(())
+ })).await
+ }
+ });
+ started.notified().await;
+ waiter.abort();
+ assert!(waiter.await.unwrap_err().is_cancelled());
+ assert!(!acknowledged.load(Ordering::Acquire));
+ assert!(closing.lock().await.is_some());
+ release.notify_one();
+ join_cleanup(&closing, || panic!("must reuse the retained worker")).await.unwrap();
+ assert!(acknowledged.load(Ordering::Acquire));
+ assert!(closing.lock().await.is_none());
+}
+
+#[tokio::test]
+async fn a_failed_cleanup_can_be_retried_without_aborting_pending_work() {
+ let closing = Mutex::new(None);
+ assert_eq!(join_cleanup(&closing, || tokio::spawn(async {
+ Err(ModuleCallError::TransportFailed)
+ })).await, Err(ModuleCallError::TransportFailed));
+ assert!(closing.lock().await.is_none());
+ join_cleanup(&closing, || tokio::spawn(async { Ok(()) })).await.unwrap();
+}