Commits

Anonymous committed 8c3112e

.

  • Participants
  • Parent commits a8d3e3a

Comments (0)

Files changed (1)

 
 value write_fd_from = Lwt_unix.write;
 
+value return_unit = return ()
+;
+
 value rec write_fd_loop fd str ofs left =
   if left = 0
   then
-    return ()
+    return_unit
   else
     Lwt.bind
       (Lwt_unix.write fd str ofs left)
   let bufsz = 4096 in
   let buf = String.make bufsz '\x00' in
   fun fd ->
-    shutdown fd Unix.SHUTDOWN_SEND >>= fun () ->
-    loop ()
-    where rec loop () =
-      read_fd_into fd buf 0 bufsz >>= fun was_read ->
-      if was_read = 0
-      then return ()
-      else loop ()
+    catch
+      (fun () ->
+         shutdown fd Unix.SHUTDOWN_SEND >>= loop
+         where rec loop () =
+           read_fd_into fd buf 0 bufsz >>= fun was_read ->
+           if was_read = 0
+           then return_unit
+           else loop ()
+    )
+    (fun
+     [ (Unix.Unix_error err _ _) as e ->
+         if err = Unix.ENOTCONN || err = Unix.EINVAL
+         then return_unit
+         else error e
+     | e -> error e
+     ]
+    )
 ;
 
 value close_socket fd =
     (match sockaddr with
       [ Unix.ADDR_UNIX path when path <> "" && path.[0] <> '\x00' ->
           ( Unix.unlink path
-          ; return ()
+          ; return_unit
           )
       | Unix.ADDR_UNIX _ | Unix.ADDR_INET _ ->
-          return ()
+          return_unit
       ]
     ) >>= fun () ->
     let () = sigpipe_finally () in
              )
           ) >>= fun () ->
         let () = if server_debug then dbg "after 'catch loop'" else () in
-        return ()
+        return_unit
       )
   ; { shutdown = lazy (Lwt.wakeup abort_wakener `Shutdown)
     ; wait_server =
         finish_waiter >>= fun res ->
         match res with
-        [ `Ok () -> return ()
+        [ `Ok () -> return_unit
         | `Error e -> error e
         ]
     }
       let () = if server_debug then dbg "close2" else () in
       Lwt_unix.close fd >>= fun () ->
       let () = if server_debug then dbg "close3" else () in
-      return ()
+      return_unit
     end in
     let in_ch = Lwt_io.of_fd ?buffer_size ~mode:Lwt_io.input
       ~close:(fun () -> Lazy.force close) fd in
         let res_opt = Array.make len None in
         let writer i m_a =
           m_a >>= fun a ->
-          (res_opt.(i) := Some a; return ())
+          (res_opt.(i) := Some a; return_unit)
         in
         let writers = inner [] 0
           where rec inner acc i =