diff --git a/src/agent-client-protocol/src/util.rs b/src/agent-client-protocol/src/util.rs index 770dcb3..ec0cf37 100644 --- a/src/agent-client-protocol/src/util.rs +++ b/src/agent-client-protocol/src/util.rs @@ -107,24 +107,26 @@ pub async fn both( /// Returns the result of `foreground`. If `background` errors before /// `foreground` completes, the error is propagated. If `background` /// completes with `Ok(())`, we continue waiting for `foreground`. -pub async fn run_until( +pub fn run_until( background: impl Future>, foreground: impl Future>, -) -> Result { +) -> impl Future> { use futures::future::{Either, select}; use std::pin::pin; - match select(pin!(background), pin!(foreground)).await { - Either::Left((bg_result, fg_future)) => { - // Background finished first - bg_result?; // propagate error, or if Ok(()), keep waiting - fg_future.await - } - Either::Right((fg_result, _bg_future)) => { - // Foreground finished first, drop background - fg_result + Box::pin(async move { + match select(pin!(background), pin!(foreground)).await { + Either::Left((bg_result, fg_future)) => { + // Background finished first + bg_result?; // propagate error, or if Ok(()), keep waiting + fg_future.await + } + Either::Right((fg_result, _bg_future)) => { + // Foreground finished first, drop background + fg_result + } } - } + }) } /// Process items from a stream concurrently.