Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

native-tls: fix buffered io hang #106

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions tokio-native-tls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,45 @@ impl<S: AsyncRead + AsyncWrite + Unpin> Future for MidHandshake<S> {

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut_self = self.get_mut();
let mut s = mut_self.0.take().expect("future polled after completion");

s.get_mut().context = cx as *mut _ as *mut ();
match s.handshake() {
Ok(mut s) => {
s.get_mut().context = null_mut();
Poll::Ready(Ok(TlsStream(s)))
let mut already_flushed = false;

loop {
let mut s = mut_self.0.take().expect("future polled after completion");

s.get_mut().context = cx as *mut _ as *mut ();

match s.handshake() {
Ok(mut s) => {
s.get_mut().context = null_mut();
return Poll::Ready(Ok(TlsStream(s)));
}
Err(HandshakeError::WouldBlock(mut s)) => {
s.get_mut().context = null_mut();

match Pin::new(&mut s.get_mut().get_mut()).poll_flush(cx) {
Poll::Ready(Ok(())) => {
mut_self.0 = Some(s);

if !already_flushed {
already_flushed = true;
continue;
} else {
return Poll::Pending;
}
}
Poll::Ready(Err(_e)) => {
mut_self.0 = Some(s);
todo!("figure out how to bubble up the io error")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfackler do you know how we can bubble up an io error through native_tls::Error or should we create a new error type here that wraps both?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to make a wrapper type (or just use box<error>.

}
Poll::Pending => {}
}

mut_self.0 = Some(s);

return Poll::Pending;
}
Err(HandshakeError::Failure(e)) => return Poll::Ready(Err(e)),
}
Err(HandshakeError::WouldBlock(mut s)) => {
s.get_mut().context = null_mut();
mut_self.0 = Some(s);
Poll::Pending
}
Err(HandshakeError::Failure(e)) => Poll::Ready(Err(e)),
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions tokio-native-tls/tests/buffered.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use native_tls::TlsConnector;
use tokio::{io::BufWriter, net::TcpStream};

#[tokio::test]
async fn connect_using_bufwriter() {
drop(env_logger::try_init());

let socket = BufWriter::new(
TcpStream::connect(("example.com", 443))
.await
.expect("connect socket"),
);

let connector = TlsConnector::builder()
.build()
.expect("build TLS connector");

tokio_native_tls::TlsConnector::from(connector)
.connect("example.com", BufWriter::new(socket))
.await
.expect("connect TLS");
}