From 2a43eeaa7f36163c6e12f0faeba3242951ba06c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Sat, 26 Nov 2016 11:47:50 +0100 Subject: [PATCH] P2P: properly handle `End_of_file`. --- src/node/net/p2p.ml | 4 ++-- src/utils/lwt_utils.ml | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) 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