Skip to content

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

fix(runtime): stop background tasks from blocking on shutdown

Steven Enamakel6ac322aa2ca0
diff --git a/crates/openhuman-core/src/core/runtime/saas.rs b/crates/openhuman-core/src/core/runtime/saas.rs
index 467f42ee43..6592665367 100644
--- a/crates/openhuman-core/src/core/runtime/saas.rs
+++ b/crates/openhuman-core/src/core/runtime/saas.rs
@@ -529,7 +529,10 @@ pub async fn build_with_profile_capabilities(
);
crate::profiles::host::install(Arc::clone(&host));
crate::profiles::lease::heartbeat(Arc::clone(&host), runtime.context().clone());
- crate::profiles::background::spawn(host);
+ crate::profiles::background::spawn(
+ &crate::core::runtime::DispatchCtx::new(runtime.context().clone()),
+ host,
+ );
Ok(runtime)
}
diff --git a/crates/openhuman-core/src/core/runtime/services.rs b/crates/openhuman-core/src/core/runtime/services.rs
index ab28f44448..52a2f9234d 100644
--- a/crates/openhuman-core/src/core/runtime/services.rs
+++ b/crates/openhuman-core/src/core/runtime/services.rs
@@ -127,6 +127,7 @@ pub(crate) async fn start_selected_services(
tasks.track(
"blob_sweep",
crate::storage::blob_sweep::spawn(
+ &crate::core::runtime::DispatchCtx::new(std::sync::Arc::clone(ctx)),
crate::storage::blob_sweep::FIRST_SWEEP_DELAY,
crate::storage::blob_sweep::SWEEP_INTERVAL,
),
diff --git a/crates/openhuman-core/src/profiles/background.rs b/crates/openhuman-core/src/profiles/background.rs
index 18aaec1725..d67e6455d8 100644
--- a/crates/openhuman-core/src/profiles/background.rs
+++ b/crates/openhuman-core/src/profiles/background.rs
@@ -40,8 +40,11 @@ pub struct TickReport {
}
/// Start the loop. It runs until the process exits.
-pub fn spawn(host: Arc<ProfileHost>) -> tokio::task::JoinHandle<()> {
- crate::core::runtime::DispatchCtx::host_default().spawn(async move {
+pub fn spawn(
+ ctx: &crate::core::runtime::DispatchCtx,
+ host: Arc<ProfileHost>,
+) -> tokio::task::JoinHandle<()> {
+ ctx.spawn(async move {
log::info!(
"[profiles][background] started (every {}s)",
TICK_INTERVAL.as_secs()
diff --git a/crates/openhuman-core/src/storage/blob_sweep.rs b/crates/openhuman-core/src/storage/blob_sweep.rs
index 95890d5a38..145a3c2791 100644
--- a/crates/openhuman-core/src/storage/blob_sweep.rs
+++ b/crates/openhuman-core/src/storage/blob_sweep.rs
@@ -173,8 +173,12 @@ pub async fn sweep_installed() -> u64 {
/// Spawns the sweeper: after `first_delay`, then every `interval`, until the
/// task is aborted. Missed ticks are skipped, not replayed.
-pub fn spawn(first_delay: Duration, interval: Duration) -> tokio::task::JoinHandle<()> {
- crate::core::runtime::DispatchCtx::host_default().spawn(async move {
+pub fn spawn(
+ ctx: &crate::core::runtime::DispatchCtx,
+ first_delay: Duration,
+ interval: Duration,
+) -> tokio::task::JoinHandle<()> {
+ ctx.spawn(async move {
tracing::debug!(
first_delay_secs = first_delay.as_secs(),
interval_secs = interval.as_secs(),
diff --git a/crates/openhuman-core/src/storage/blob_sweep_tests.rs b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
index 2623258c39..d1da109ea5 100644
--- a/crates/openhuman-core/src/storage/blob_sweep_tests.rs
+++ b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
@@ -55,7 +55,7 @@ async fn sweep_all_visits_the_local_scope() {
#[tokio::test(start_paused = true)]
async fn spawned_sweeper_waits_for_its_first_delay_then_keeps_ticking() {
- let handle = spawn(Duration::from_secs(10), Duration::from_secs(20));
+ let handle = spawn(&crate::core::runtime::DispatchCtx::ambient(), Duration::from_secs(10), Duration::from_secs(20));
tokio::time::sleep(Duration::from_secs(65)).await;
assert!(!handle.is_finished(), "the sweeper keeps running");
handle.abort();