diff --git a/unix/tcpv4_socket.ml b/unix/tcpv4_socket.ml index d2cbc22c2..32f14183c 100644 --- a/unix/tcpv4_socket.ml +++ b/unix/tcpv4_socket.ml @@ -87,11 +87,16 @@ let read fd = (fun exn -> return (`Error (`Unknown (Printexc.to_string exn)))) let rec write fd buf = - Lwt_cstruct.write fd buf - >>= function - | n when n = Cstruct.len buf -> return (`Ok ()) - | 0 -> return `Eof - | n -> write fd (Cstruct.sub buf n (Cstruct.len buf - n)) + Lwt.catch + (fun () -> + Lwt_cstruct.write fd buf + >>= function + | n when n = Cstruct.len buf -> return (`Ok ()) + | 0 -> return `Eof + | n -> write fd (Cstruct.sub buf n (Cstruct.len buf - n)) + ) (function + | Unix.Unix_error(Unix.EPIPE, _, _) -> return `Eof + | e -> Lwt.fail e) let writev fd bufs = Lwt_list.fold_left_s diff --git a/unix/tcpv6_socket.ml b/unix/tcpv6_socket.ml index 76821cef4..8d93aef92 100644 --- a/unix/tcpv6_socket.ml +++ b/unix/tcpv6_socket.ml @@ -88,11 +88,16 @@ let read fd = (fun exn -> return (`Error (`Unknown (Printexc.to_string exn)))) let rec write fd buf = - Lwt_cstruct.write fd buf - >>= function - | n when n = Cstruct.len buf -> return (`Ok ()) - | 0 -> return `Eof - | n -> write fd (Cstruct.sub buf n (Cstruct.len buf - n)) + Lwt.catch + (fun () -> + Lwt_cstruct.write fd buf + >>= function + | n when n = Cstruct.len buf -> return (`Ok ()) + | 0 -> return `Eof + | n -> write fd (Cstruct.sub buf n (Cstruct.len buf - n)) + ) (function + | Unix.Unix_error(Unix.EPIPE, _, _) -> return `Eof + | e -> Lwt.fail e) let writev fd bufs = Lwt_list.fold_left_s