From 90d1fd9d7d69f6124cdceb3b7b825acaf149f360 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Sun, 27 Oct 2019 23:23:54 +0100 Subject: [PATCH] console: use Lwt_unix.openfile (RDONLY; NONBLOCK) to open the fifo, followed by Lwt_unix.wait_read (in read_console, called asynchronously in a fresh task) and only then convert to a Lwt_io.t (of_fd ~mode:Input) -- thanks to @cfcs This allows albatross to be run on Linux, where the former code lead to: - open FIFO - read <- EOF (although no writer was connected yet) this works nicely around ocsigen/lwt#741 --- daemon/albatross_console.ml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/daemon/albatross_console.ml b/daemon/albatross_console.ml index 9ac154e..29993d1 100644 --- a/daemon/albatross_console.ml +++ b/daemon/albatross_console.ml @@ -20,8 +20,10 @@ let pp_unix_error ppf e = Fmt.string ppf (Unix.error_message e) let active = ref String.Map.empty -let read_console id name ring channel = +let read_console id name ring fd = Lwt.catch (fun () -> + Lwt_unix.wait_read fd >>= fun () -> + let channel = Lwt_io.of_fd ~mode:Lwt_io.Input fd in let rec loop () = Lwt_io.read_line channel >>= fun line -> Logs.debug (fun m -> m "read %s" line) ; @@ -48,14 +50,14 @@ let read_console id name ring channel = | exn -> Logs.err (fun m -> m "%s error while reading %s" name (Printexc.to_string exn)) end ; - Lwt_io.close channel) + Vmm_lwt.safe_close fd) let open_fifo name = let fifo = Vmm_core.Name.fifo_file name in Lwt.catch (fun () -> Logs.debug (fun m -> m "opening %a for reading" Fpath.pp fifo) ; - Lwt_io.open_file ~mode:Lwt_io.Input (Fpath.to_string fifo) >>= fun channel -> - Lwt.return (Some channel)) + Lwt_unix.openfile (Fpath.to_string fifo) [Lwt_unix.O_RDONLY; Lwt_unix.O_NONBLOCK] 0 >>= fun fd -> + Lwt.return (Some fd)) (function | Unix.Unix_error (e, f, _) -> Logs.err (fun m -> m "%a error in %s: %a" Fpath.pp fifo f pp_unix_error e) ;