From 42e1d72fb287e9e4a173d141230a577198f49721 Mon Sep 17 00:00:00 2001 From: Jack Zhou Date: Tue, 18 Jul 2017 14:23:56 -0700 Subject: [PATCH] Fixed TlsStream closing the connection abruptly on fatal errors. (#12) Instead, flush queued TLS messages when an error occurs before closing the connection. --- src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4e3a47f..d4d1aa5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,8 +172,16 @@ impl TlsStream continue }, Ok(_) => { - self.session.process_new_packets() - .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; + if let Err(err) = self.session.process_new_packets() { + // flush queued messages before returning an Err in + // order to send alerts instead of abruptly closing + // the socket + if self.session.wants_write() { + // ignore result to avoid masking original error + let _ = self.session.write_tls(&mut self.io); + } + return Err(io::Error::new(io::ErrorKind::Other, err)); + } continue }, Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => true,