diff --git a/src/node/net/p2p.ml b/src/node/net/p2p.ml index 0ff445895..71480b985 100644 --- a/src/node/net/p2p.ml +++ b/src/node/net/p2p.ml @@ -203,7 +203,7 @@ module Make (P: PARAMS) = struct | Some msg -> Lwt.return msg) (function - | Unix.Unix_error _ -> return Disconnect + | Unix.Unix_error _ | End_of_file -> return Disconnect | e -> fail e) (* send a message over a TCP socket *) @@ -236,7 +236,7 @@ module Make (P: PARAMS) = struct return true end) (function - | Unix.Unix_error _ -> return_false + | Unix.Unix_error _ | End_of_file -> return_false | e -> fail e) (* A peer handle, as a record-encoded object, abstract from the diff --git a/src/utils/lwt_utils.ml b/src/utils/lwt_utils.ml index 2fcfeaa49..3a67511ec 100644 --- a/src/utils/lwt_utils.ml +++ b/src/utils/lwt_utils.ml @@ -231,8 +231,9 @@ let rec read_bytes ?(pos = 0) ?len fd buf = if len = 0 then Lwt.return_unit else - Lwt_unix.read fd buf pos len >>= fun nb_read -> - inner (pos + nb_read) (len - nb_read) + Lwt_unix.read fd buf pos len >>= function + | 0 -> Lwt.fail End_of_file (* other endpoint cleanly closed its connection *) + | nb_read -> inner (pos + nb_read) (len - nb_read) in inner pos len @@ -254,7 +255,8 @@ let write_mbytes ?(pos=0) ?len descr buf = if len = 0 then Lwt.return_unit else - Lwt_bytes.write descr buf pos len >>= fun nb_written -> - inner (pos + nb_written) (len - nb_written) in + Lwt_bytes.write descr buf pos len >>= function + | 0 -> Lwt.fail End_of_file (* other endpoint cleanly closed its connection *) + | nb_written -> inner (pos + nb_written) (len - nb_written) in inner pos len