From a665996e0bb7d057ab02120a0a842d4517657a70 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:10:09 +0000 Subject: [PATCH] v1.17: exits send_datagram_task if the connection is closed (backport of #33836) (#34327) exits send_datagram_task if the connection is closed (#33836) Waiting on receiver.recv() can unnecessarily block while the connection is already closed. The commit exits send_datagram_task if the connection is closed. (cherry picked from commit 03fbe083b8d88474ca7a9295f0cf13809b474303) Co-authored-by: behzad nouri --- turbine/src/quic_endpoint.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/turbine/src/quic_endpoint.rs b/turbine/src/quic_endpoint.rs index e8a316420b42d8..a947f212296fb7 100644 --- a/turbine/src/quic_endpoint.rs +++ b/turbine/src/quic_endpoint.rs @@ -435,10 +435,21 @@ async fn send_datagram_task( connection: Connection, mut receiver: AsyncReceiver, ) -> Result<(), Error> { - while let Some(bytes) = receiver.recv().await { - connection.send_datagram(bytes)?; + tokio::pin! { + let connection_closed = connection.closed(); + } + loop { + tokio::select! { + biased; + bytes = receiver.recv() => { + match bytes { + None => return Ok(()), + Some(bytes) => connection.send_datagram(bytes)?, + } + } + err = &mut connection_closed => return Err(Error::from(err)), + } } - Ok(()) } async fn make_connection_task(