Model.read_file: avoid file descriptor leak

This commit is contained in:
Robur 2021-10-18 13:45:53 +00:00
parent fa1cf92702
commit 87a6b95e8c

View file

@ -24,7 +24,11 @@ let read_file datadir filepath =
let filepath = Fpath.(datadir // filepath) in let filepath = Fpath.(datadir // filepath) in
Lwt.try_bind Lwt.try_bind
(fun () -> Lwt_io.open_file ~mode:Lwt_io.Input (Fpath.to_string filepath)) (fun () -> Lwt_io.open_file ~mode:Lwt_io.Input (Fpath.to_string filepath))
(fun ic -> Lwt_result.ok (Lwt_io.read ic)) (fun ic ->
let open Lwt.Infix in
Lwt_io.read ic >>= fun data ->
Lwt_io.close ic >>= fun () ->
Lwt_result.return data)
(function (function
| Unix.Unix_error (e, _, _) -> | Unix.Unix_error (e, _, _) ->
Logs.warn (fun m -> m "Error reading local file %a: %s" Logs.warn (fun m -> m "Error reading local file %a: %s"