From ecb38ee69ac199979c1dd783e7527facc5913c6d Mon Sep 17 00:00:00 2001 From: Fantix King Date: Fri, 20 Feb 2026 20:46:44 -0500 Subject: [PATCH 1/2] fix(tls,io): multiple native-tls issues * handshake: avoid unnecessary flushes * read: flush outgoing data if any * write: avoid starvation if incoming data is needed * shutdown: send close_notify before TCP FIN --- compio-io/src/compat/sync_stream.rs | 4 +- compio-tls/src/adapter.rs | 11 +++-- compio-tls/src/stream.rs | 73 +++++++++++++++++++---------- compio-tls/tests/connect.rs | 1 + 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/compio-io/src/compat/sync_stream.rs b/compio-io/src/compat/sync_stream.rs index 64dce66f6..3f37c487c 100644 --- a/compio-io/src/compat/sync_stream.rs +++ b/compio-io/src/compat/sync_stream.rs @@ -126,7 +126,9 @@ impl SyncStream { Ok(to_read) } - pub(crate) fn has_pending_write(&self) -> bool { + /// Returns `true` if there is pending data in the write buffer that needs + /// to be flushed. + pub fn has_pending_write(&self) -> bool { !self.write_buf.is_empty() } } diff --git a/compio-tls/src/adapter.rs b/compio-tls/src/adapter.rs index 0bfa6566f..e516fd952 100644 --- a/compio-tls/src/adapter.rs +++ b/compio-tls/src/adapter.rs @@ -163,14 +163,19 @@ async fn handshake_native_tls( loop { match res { Ok(mut s) => { - s.get_mut().flush_write_buf().await?; + if s.get_mut().has_pending_write() { + s.get_mut().flush_write_buf().await?; + } return Ok(TlsStream::from(s)); } Err(e) => match e { HandshakeError::Failure(e) => return Err(io::Error::other(e)), HandshakeError::WouldBlock(mut mid_stream) => { - if mid_stream.get_mut().flush_write_buf().await? == 0 { - mid_stream.get_mut().fill_read_buf().await?; + let s = mid_stream.get_mut(); + if s.has_pending_write() { + s.flush_write_buf().await?; + } else { + s.fill_read_buf().await?; } res = mid_stream.handshake(); } diff --git a/compio-tls/src/stream.rs b/compio-tls/src/stream.rs index 203ee4aec..c802283c0 100644 --- a/compio-tls/src/stream.rs +++ b/compio-tls/src/stream.rs @@ -75,6 +75,35 @@ impl From>> for TlsStream } } +#[cfg(feature = "native-tls")] +#[inline] +async fn drive(s: &mut native_tls::TlsStream>, mut f: F) -> io::Result +where + S: AsyncRead + AsyncWrite, + F: FnMut(&mut native_tls::TlsStream>) -> io::Result, +{ + loop { + match f(s) { + Ok(res) => { + let s = s.get_mut(); + if s.has_pending_write() { + s.flush_write_buf().await?; + } + break Ok(res); + } + Err(e) if e.kind() == io::ErrorKind::WouldBlock => { + let s = s.get_mut(); + if s.has_pending_write() { + s.flush_write_buf().await?; + } else { + s.fill_read_buf().await?; + } + } + Err(e) => break Err(e), + } + } +} + impl AsyncRead for TlsStream { async fn read(&mut self, mut buf: B) -> BufResult { let slice = buf.as_uninit(); @@ -84,20 +113,12 @@ impl AsyncRead for TlsStream { unsafe { std::slice::from_raw_parts_mut::(slice.as_mut_ptr().cast(), slice.len()) }; match &mut self.0 { #[cfg(feature = "native-tls")] - TlsStreamInner::NativeTls(s) => loop { - match io::Read::read(s, slice) { - Ok(res) => { - unsafe { buf.advance_to(res) }; - return BufResult(Ok(res), buf); - } - Err(e) if e.kind() == io::ErrorKind::WouldBlock => { - match s.get_mut().fill_read_buf().await { - Ok(_) => continue, - Err(e) => return BufResult(Err(e), buf), - } - } - res => return BufResult(res, buf), + TlsStreamInner::NativeTls(s) => match drive(s, |s| io::Read::read(s, slice)).await { + Ok(res) => { + unsafe { buf.advance_to(res) }; + BufResult(Ok(res), buf) } + res => BufResult(res, buf), }, #[cfg(feature = "rustls")] TlsStreamInner::Rustls(s) => { @@ -140,16 +161,10 @@ impl AsyncWrite for TlsStream { let slice = buf.as_init(); match &mut self.0 { #[cfg(feature = "native-tls")] - TlsStreamInner::NativeTls(s) => loop { - let res = io::Write::write(s, slice); - match res { - Err(e) if e.kind() == io::ErrorKind::WouldBlock => match flush_impl(s).await { - Ok(_) => continue, - Err(e) => return BufResult(Err(e), buf), - }, - _ => return BufResult(res, buf), - } - }, + TlsStreamInner::NativeTls(s) => { + let res = drive(s, |s| io::Write::write(s, slice)).await; + BufResult(res, buf) + } #[cfg(feature = "rustls")] TlsStreamInner::Rustls(s) => { let res = futures_util::AsyncWriteExt::write(s, slice).await; @@ -175,7 +190,17 @@ impl AsyncWrite for TlsStream { self.flush().await?; match &mut self.0 { #[cfg(feature = "native-tls")] - TlsStreamInner::NativeTls(s) => s.get_mut().get_mut().shutdown().await, + TlsStreamInner::NativeTls(s) => { + // Send close_notify alert, then shutdown the underlying stream. + // Note, this implementation is platform-specific relying on how + // native-tls handles shutdown. In general, it's consistent on + // first call (sending close_notify); but it may or may not block + // and wait for the peer to respond with close_notify on any + // subsequent calls. Here we just let such behavior propagate, + // and suggest the users to call shutdown() at most once. + drive(s, |s| s.shutdown()).await?; + s.get_mut().get_mut().shutdown().await + } #[cfg(feature = "rustls")] TlsStreamInner::Rustls(s) => futures_util::AsyncWriteExt::close(s).await, #[cfg(not(any(feature = "native-tls", feature = "rustls")))] diff --git a/compio-tls/tests/connect.rs b/compio-tls/tests/connect.rs index bedaf7508..54ffb1936 100644 --- a/compio-tls/tests/connect.rs +++ b/compio-tls/tests/connect.rs @@ -13,6 +13,7 @@ async fn connect(connector: TlsConnector) { stream.flush().await.unwrap(); let (_, res) = stream.read_to_end(vec![]).await.unwrap(); println!("{}", String::from_utf8_lossy(&res)); + stream.shutdown().await.unwrap(); } #[cfg(feature = "native-tls")] From 55ecea79595c9d907a4e8c40e831a7c0fa613c03 Mon Sep 17 00:00:00 2001 From: Fantix King Date: Fri, 20 Feb 2026 21:16:15 -0500 Subject: [PATCH 2/2] Apply CR suggestion Co-authored-by: Yuyi Wang --- compio-tls/src/adapter.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compio-tls/src/adapter.rs b/compio-tls/src/adapter.rs index e516fd952..dbec83f6e 100644 --- a/compio-tls/src/adapter.rs +++ b/compio-tls/src/adapter.rs @@ -163,8 +163,9 @@ async fn handshake_native_tls( loop { match res { Ok(mut s) => { - if s.get_mut().has_pending_write() { - s.get_mut().flush_write_buf().await?; + let inner = s.get_mut(); + if inner.has_pending_write() { + inner.flush_write_buf().await?; } return Ok(TlsStream::from(s)); }