Skip to content

Commit

Permalink
Fix OpenSSL runtime shutdown (inhabitedtype#53)
Browse files Browse the repository at this point in the history
In some cases (especially when receiving a response with a close
delimited body), the peer closes the connection before we attempt to SSL
shutdown, leading to unnecessary reporting of errors such as
`Unix.Unix_error(Unix.EBADF)`
  • Loading branch information
anmonteiro committed May 11, 2020
1 parent 346a587 commit dc2c6c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
30 changes: 17 additions & 13 deletions lwt-unix/ssl_io_real.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ struct
type addr = Unix.sockaddr

let close ssl =
Lwt_ssl.ssl_shutdown ssl >>= fun () ->
let fd = Lwt_ssl.get_fd ssl in
match Lwt_unix.state fd with
| Closed ->
Lwt.return_unit
| _ ->
Lwt_ssl.ssl_shutdown ssl >>= fun () ->
Lwt.catch
(fun () -> Lwt.wrap2 Lwt_ssl.shutdown ssl Unix.SHUTDOWN_ALL)
(function
| Unix.Unix_error (Unix.ENOTCONN, _, _) ->
Lwt.return_unit
| exn -> Lwt.fail exn)
(fun () -> Lwt.wrap2 Lwt_ssl.shutdown ssl Unix.SHUTDOWN_ALL)
(function
| Unix.Unix_error (Unix.ENOTCONN, _, _) ->
Lwt.return_unit
| exn ->
Lwt.fail exn)
>>= fun () ->
let fd = Lwt_ssl.get_fd ssl in
match Lwt_unix.state fd with
| Lwt_unix.Closed -> Lwt.return_unit
| _ ->
Lwt.catch
(fun () -> Lwt_ssl.close ssl)
(fun _exn -> Lwt.return_unit)
match Lwt_unix.state fd with
| Lwt_unix.Closed ->
Lwt.return_unit
| _ ->
Lwt.catch (fun () -> Lwt_ssl.close ssl) (fun _exn -> Lwt.return_unit)

let read ssl bigstring ~off ~len =
Lwt.catch
Expand Down
2 changes: 0 additions & 2 deletions lwt-unix/tls_io_real.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ module Io :

let shutdown_receive _tls = ()

let close tls = Tls_lwt.Unix.close tls

let state tls =
match Tls_lwt.Unix.epoch tls with `Error -> `Error | `Ok _ -> `Open
end
Expand Down

0 comments on commit dc2c6c7

Please sign in to comment.