From 439a5493b7daec82a66d4df2f87244215b3be646 Mon Sep 17 00:00:00 2001
From: Anthony Ramine <nox@nox.paris>
Date: Mon, 31 Jan 2022 11:06:06 +0100
Subject: [PATCH] Don't return shutdown errors during shutdown

---
 boring/src/ssl/mod.rs | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/boring/src/ssl/mod.rs b/boring/src/ssl/mod.rs
index c382375d..6efa4909 100644
--- a/boring/src/ssl/mod.rs
+++ b/boring/src/ssl/mod.rs
@@ -3054,7 +3054,24 @@ impl<S: Read + Write> SslStream<S> {
         match unsafe { ffi::SSL_shutdown(self.ssl.as_ptr()) } {
             0 => Ok(ShutdownResult::Sent),
             1 => Ok(ShutdownResult::Received),
-            n => Err(self.make_error(n)),
+            n => {
+                let err = self.make_error(n);
+
+                // If boring returns PROTOCOL_IS_SHUTDOWN then the connection
+                // has already been shutdown and we can just return Ok(()), as
+                // this was exactly what we wanted to do anyway.
+                if e.code() == ErrorCode::SSL {
+                    if let Some(stack) = e.ssl_error() {
+                        if let Some(first) = stack.errors.first() {
+                            if first.code() as i32 == boring_sys::SSL_R_PROTOCOL_IS_SHUTDOWN {
+                                return Poll::Ready(Ok(ShutdownResult::Received));
+                            }
+                        }
+                    }
+                }
+
+                Err(err)
+            }
         }
     }