diff --git a/src/lwt_ssl.ml b/src/lwt_ssl.ml index b178b94..2c50469 100644 --- a/src/lwt_ssl.ml +++ b/src/lwt_ssl.ml @@ -166,11 +166,23 @@ let ssl_shutdown (fd, s) = Plain -> Lwt.return_unit | SSL s -> repeat_call fd (fun () -> Ssl.shutdown s) -let shutdown (fd, _) cmd = Lwt_unix.shutdown fd cmd +let shutdown (fd, _) cmd = + Lwt.finalize + (fun () -> + Lwt.catch + (fun () -> + Lwt_unix.shutdown fd cmd; + Lwt.return_unit) + (function + (* Occurs if the peer closes the connection first. *) + | Unix.Unix_error (Unix.ENOTCONN, _, _) -> Lwt.return_unit + | exn -> Lwt.reraise exn)[@ocaml.warning "-4"]) + (fun () -> + Lwt_unix.close fd) let close_notify = function | (_, Plain) as s -> - shutdown s Unix.SHUTDOWN_SEND; + shutdown s Unix.SHUTDOWN_SEND >>= fun () -> Lwt.return_true | (fd, SSL s) -> repeat_call fd (fun () -> Ssl.close_notify s) @@ -181,7 +193,7 @@ let abort (fd, _) = Lwt_unix.abort fd let shutdown_and_close s = ssl_shutdown s >>= fun () -> - Lwt.wrap2 shutdown s Unix.SHUTDOWN_ALL >>= fun () -> + shutdown s Unix.SHUTDOWN_ALL >>= fun () -> close s let out_channel_of_descr ?buffer s = diff --git a/src/lwt_ssl.mli b/src/lwt_ssl.mli index ac91ee0..0b7f8e1 100644 --- a/src/lwt_ssl.mli +++ b/src/lwt_ssl.mli @@ -68,7 +68,7 @@ val write_bytes : socket -> Lwt_bytes.t -> int -> int -> int Lwt.t val wait_read : socket -> unit Lwt.t val wait_write : socket -> unit Lwt.t -val shutdown : socket -> Unix.shutdown_command -> unit +val shutdown : socket -> Unix.shutdown_command -> unit Lwt.t val close : socket -> unit Lwt.t val in_channel_of_descr : ?buffer:Lwt_bytes.t -> socket -> Lwt_io.input_channel