Skip to content

Commit 95ce856

Browse files
committed
Do not use tokio::select! for race2
tokio::select! requires the futures to be fused, see for example tokio-rs/tokio#3812
1 parent c47fef2 commit 95ce856

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/server.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,25 @@ impl<T> Future for UnwrapToPending<T> {
464464
}
465465

466466
pub(crate) async fn race2<T, A: Future<Output = T>, B: Future<Output = T>>(f1: A, f2: B) -> T {
467-
tokio::select! {
468-
x = f1 => x,
469-
x = f2 => x,
470-
}
467+
// Pin the futures on the stack for polling
468+
let mut f1 = std::pin::pin!(f1);
469+
let mut f2 = std::pin::pin!(f2);
470+
471+
// Create a future that resolves when either f1 or f2 completes
472+
std::future::poll_fn(|cx| {
473+
// Poll both futures
474+
match f1.as_mut().poll(cx) {
475+
Poll::Ready(result) => return Poll::Ready(result),
476+
Poll::Pending => {}
477+
}
478+
match f2.as_mut().poll(cx) {
479+
Poll::Ready(result) => return Poll::Ready(result),
480+
Poll::Pending => {}
481+
}
482+
// If neither is ready, yield back to the executor
483+
Poll::Pending
484+
})
485+
.await
471486
}
472487

473488
/// Run a server loop, invoking a handler callback for each request.

0 commit comments

Comments
 (0)