@@ -47,9 +47,9 @@ module Network.Wai.Handler.WarpTLS (
4747import Control.Applicative ((<$>))
4848#endif
4949import Control.Applicative ((<|>))
50−import Control.Exception (Exception, throwIO, bracket, finally, handle, fromException, try, IOException, onException, SomeException(..))
50+import Control.Exception (Exception, throwIO, bracket, finally, handle, fromException, try, IOException, onException, SomeException(..), handleJust)
5151import qualified Control.Exception as E
52−import Control.Monad (void)
52+import Control.Monad (void, guard)
5353import qualified Data.ByteString as S
5454import qualified Data.ByteString.Lazy as L
5555import Data.Default.Class (def)
@@ -348,9 +348,17 @@ httpOverTls TLSSettings{..} s bs0 params = do
348348 sendfile fid offset len hook headers =
349349 readSendFile writeBuf bufferSize sendall fid offset len hook headers
350350
351− close' = void (tryIO $ TLS.bye ctx) `finally`
351+ close' = void (tryIO sendBye) `finally`
352352 TLS.contextClose ctx
353353
354+ sendBye =
355+ -- It's fine if the connection was closed by the other side before
356+ -- receiving close_notify, see RFC 5246 section 7.2.1.
357+ handleJust
358+ (\e -> guard (e == ConnectionClosedByPeer) >> return e)
359+ (const (return ()))
360+ (TLS.bye ctx)
361+
354362 -- TLS version of recv with a cache for leftover input data.
355363 -- The cache is shared with recvBuf.
356364 recv cref = do