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

Rustls buffered handshake eof failed #98

Merged
merged 7 commits into from
Mar 19, 2022
Merged
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
2 changes: 1 addition & 1 deletion tokio-rustls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tokio-rustls"
version = "0.23.2"
version = "0.23.3"
authors = ["quininer kel <quininer@live.com>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/tokio-rs/tls"
Expand Down
4 changes: 1 addition & 3 deletions tokio-rustls/src/common/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ where
try_poll!(tls_stream.handshake(cx));
}

while tls_stream.session.wants_write() {
try_poll!(tls_stream.write_io(cx));
}
try_poll!(Pin::new(&mut tls_stream).poll_flush(cx));
}

Poll::Ready(Ok(stream))
Expand Down
14 changes: 13 additions & 1 deletion tokio-rustls/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@ where
loop {
let mut write_would_block = false;
let mut read_would_block = false;
let mut need_flush = false;

while self.session.wants_write() {
match self.write_io(cx) {
Poll::Ready(Ok(n)) => wrlen += n,
Poll::Ready(Ok(n)) => {
wrlen += n;
need_flush = true;
}
Poll::Pending => {
write_would_block = true;
break;
Expand All @@ -178,6 +182,14 @@ where
}
}

if need_flush {
match Pin::new(&mut self.io).poll_flush(cx) {
Poll::Ready(Ok(())) => (),
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Pending => write_would_block = true,
}
}

while !self.eof && self.session.wants_read() {
match self.read_io(cx) {
Poll::Ready(Ok(0)) => self.eof = true,
Expand Down
24 changes: 24 additions & 0 deletions tokio-rustls/src/common/test_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ async fn stream_handshake() -> io::Result<()> {
Ok(()) as io::Result<()>
}

#[tokio::test]
async fn stream_buffered_handshake() -> io::Result<()> {
use tokio::io::BufWriter;

let (server, mut client) = make_pair();
let mut server = Connection::from(server);

{
let mut good = BufWriter::new(Good(&mut server));
let mut stream = Stream::new(&mut good, &mut client);
let (r, w) = poll_fn(|cx| stream.handshake(cx)).await?;

assert!(r > 0);
assert!(w > 0);

poll_fn(|cx| stream.handshake(cx)).await?; // finish server handshake
}

assert!(!server.is_handshaking());
assert!(!client.is_handshaking());

Ok(()) as io::Result<()>
}

#[tokio::test]
async fn stream_handshake_eof() -> io::Result<()> {
let (_, mut client) = make_pair();
Expand Down