Skip to content

Commit 3525494

Browse files
committed
Don't unconditionally shut down the SslStream
OpenSSL doesn't like applications to call SSL_Shutdown on a connection which either hasn't finished the handshake or has fatal errors. This commit only addresses the first case. When using tokio_openssl::SslStream in hyper, and (a)waiting on the conn returned by for instance hyper::client::conn::http1::handshake(stream) it returns "error shutting down connection" with the nested cause "shutdown while in init" errors if the SSL handshake fails. This error might not fatal, but it is confusing, and makes it harder find the actual error one is looking for.
1 parent 70edf38 commit 3525494

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,17 @@ where
269269
}
270270

271271
fn poll_shutdown(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<io::Result<()>> {
272-
match self.as_mut().with_context(ctx, |s| s.shutdown()) {
272+
let maybe_shutdown = |s: &mut openssl::ssl::SslStream<_>| {
273+
if s.ssl().is_init_finished() {
274+
s.shutdown()
275+
} else {
276+
// I would really like to return an error with ErrorCode::ZERO_RETURN here,
277+
// but there aren't any public methods to create openssl::error::Error
278+
Ok(ShutdownResult::Received)
279+
}
280+
};
281+
282+
match self.as_mut().with_context(ctx, maybe_shutdown) {
273283
Ok(ShutdownResult::Sent) | Ok(ShutdownResult::Received) => {}
274284
Err(ref e) if e.code() == ErrorCode::ZERO_RETURN => {}
275285
Err(ref e) if e.code() == ErrorCode::WANT_READ || e.code() == ErrorCode::WANT_WRITE => {

0 commit comments

Comments
 (0)