From a0f310a4921a57545d5eb6f9f0e86420c1066007 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 11 Oct 2024 08:57:05 +0000 Subject: [PATCH] Make into_future on Serve, WithGracefulShutdown independent of tokio (#2975) --- axum/src/serve.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/axum/src/serve.rs b/axum/src/serve.rs index 27e4912a..87b103ee 100644 --- a/axum/src/serve.rs +++ b/axum/src/serve.rs @@ -312,17 +312,17 @@ where _marker: _, } = self; - let (signal_tx, signal_rx) = watch::channel(()); - let signal_tx = Arc::new(signal_tx); - tokio::spawn(async move { - signal.await; - trace!("received graceful shutdown signal. Telling tasks to shutdown"); - drop(signal_rx); - }); - - let (close_tx, close_rx) = watch::channel(()); - private::ServeFuture(Box::pin(async move { + let (signal_tx, signal_rx) = watch::channel(()); + let signal_tx = Arc::new(signal_tx); + tokio::spawn(async move { + signal.await; + trace!("received graceful shutdown signal. Telling tasks to shutdown"); + drop(signal_rx); + }); + + let (close_tx, close_rx) = watch::channel(()); + loop { let (tcp_stream, remote_addr) = tokio::select! { conn = tcp_accept(&tcp_listener) => { @@ -597,4 +597,20 @@ mod tests { assert_eq!(address.ip(), IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))); assert_ne!(address.port(), 0); } + + #[test] + fn into_future_outside_tokio() { + let router: Router = Router::new(); + let addr = "0.0.0.0:0"; + + let rt = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap(); + + let listener = rt.block_on(tokio::net::TcpListener::bind(addr)).unwrap(); + + // Call Serve::into_future outside of a tokio context. This used to panic. + _ = serve(listener, router).into_future(); + } }