From 87a6b95e8c7ca2c3527cc2a71beae3e7fa719542 Mon Sep 17 00:00:00 2001 From: Robur Date: Mon, 18 Oct 2021 13:45:53 +0000 Subject: [PATCH] Model.read_file: avoid file descriptor leak --- lib/model.ml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/model.ml b/lib/model.ml index 9cc8303..b16dcb6 100644 --- a/lib/model.ml +++ b/lib/model.ml @@ -24,7 +24,11 @@ let read_file datadir filepath = let filepath = Fpath.(datadir // filepath) in Lwt.try_bind (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 | Unix.Unix_error (e, _, _) -> Logs.warn (fun m -> m "Error reading local file %a: %s"