Skip to content

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

fix(channels): propagate channel service errors and wait for session resume

Steven Enamakel52ee225979f9
diff --git a/crates/openhuman-core/src/channels/runtime/session.rs b/crates/openhuman-core/src/channels/runtime/session.rs
index 04d97a26cd..289b2fb442 100644
--- a/crates/openhuman-core/src/channels/runtime/session.rs
+++ b/crates/openhuman-core/src/channels/runtime/session.rs
@@ -107,12 +107,21 @@ fn spawn_channel_service_with<F, Fut>(
) -> tokio::task::JoinHandle<()>
where
F: FnMut(CancellationToken) -> Fut + Send + 'static,
- Fut: Future<Output = ()> + Send + 'static,
+ Fut: Future<Output = anyhow::Result<()>> + Send + 'static,
{
tokio::spawn(async move {
- let mut session = controller.current();
+ // A logout can race the service task's first poll. Do not let a
+ // service created during that window start with the replacement
+ // token before a credential has been installed again.
+ let mut session = controller.wait_for_resume().await;
loop {
- run(session.clone()).await;
+ if let Err(error) = run(session.clone()).await {
+ tracing::error!(domain = "channels", operation = "service", error = %error, "channel service generation stopped");
+ // In particular, a failed close must retain ownership and
+ // stop here instead of admitting a second listener against
+ // the still-live generation.
+ return;
+ }
if !session.is_cancelled() {
return;
}
@@ -124,7 +133,7 @@ where
pub(crate) fn spawn_current_channel_service_with<F, Fut>(run: F) -> tokio::task::JoinHandle<()>
where
F: FnMut(CancellationToken) -> Fut + Send + 'static,
- Fut: Future<Output = ()> + Send + 'static,
+ Fut: Future<Output = anyhow::Result<()>> + Send + 'static,
{
spawn_channel_service_with(SESSION.clone(), run)
}
diff --git a/crates/openhuman-core/src/core/runtime/services.rs b/crates/openhuman-core/src/core/runtime/services.rs
index b385664a64..85268ffb41 100644
--- a/crates/openhuman-core/src/core/runtime/services.rs
+++ b/crates/openhuman-core/src/core/runtime/services.rs
@@ -298,24 +298,19 @@ pub fn spawn_channels_service() -> Option<tokio::task::JoinHandle<()>> {
let config = match crate::config::Config::load_or_init().await {
Ok(c) => c,
Err(e) => {
- log::warn!("[channels] could not load config for listeners: {e}");
- return;
+ return Err(e.into());
}
};
if !crate::channels::has_listener_configuration(&config) {
log::debug!(
"[channels] no channel integrations configured; not spawning listeners"
);
- return;
+ return Ok(());
}
log::info!(
"[channels] spawning in-process realtime listeners (Telegram, Discord, …)"
);
- if let Err(e) =
- crate::channels::start_channels_with_session(config, channel_session).await
- {
- log::error!("[channels] start_channels ended with error: {e}");
- }
+ crate::channels::start_channels_with_session(config, channel_session).await
},
),
);