From bc1e93aac8944196b8e351b6e6af10b91c8e20a0 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sat, 10 Jun 2023 13:13:00 +0100 Subject: [PATCH] fix: ocamlformat-rpc initialization if [get_process] is alled concurrently, the ivar might be filled twice. Fixes #1115 Signed-off-by: Rudi Grinberg --- CHANGES.md | 3 +++ ocaml-lsp-server/src/ocamlformat_rpc.ml | 14 +++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 9cd61307e..a13b451f8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,9 @@ ## Fixes +- Fix initilization of `ocamlformat-rpc` in some edge cases when ocamlformat is + initialized concurrently (#1132) + - Kill unnecessary `$ dune ocaml-merlin` with SIGTERM rather than SIGKILL (#1124) diff --git a/ocaml-lsp-server/src/ocamlformat_rpc.ml b/ocaml-lsp-server/src/ocamlformat_rpc.ml index ab36ac995..9c77a0e17 100644 --- a/ocaml-lsp-server/src/ocamlformat_rpc.ml +++ b/ocaml-lsp-server/src/ocamlformat_rpc.ml @@ -136,12 +136,18 @@ type state = type t = state ref +let maybe_fill ivar x = + let* v = Fiber.Ivar.peek ivar in + match v with + | Some _ -> Fiber.return () + | None -> Fiber.Ivar.fill ivar x + let get_process t = match !t with | Running p -> Fiber.return @@ Ok p | Disabled | Stopped -> Fiber.return @@ Error `No_process | Waiting_for_init { ask_init; wait_init } -> ( - let* () = Fiber.Ivar.fill ask_init () in + let* () = maybe_fill ask_init () in let+ () = Fiber.Ivar.read wait_init in match !t with | Running p -> Ok p @@ -189,12 +195,6 @@ let create_state () = let create () = ref (if Sys.win32 then Disabled else create_state ()) -let maybe_fill ivar x = - let* v = Fiber.Ivar.peek ivar in - match v with - | Some _ -> Fiber.return () - | None -> Fiber.Ivar.fill ivar x - let stop t = match !t with | Disabled | Stopped -> Fiber.return ()