Skip to content

Commit

Permalink
fix: ocamlformat-rpc initialization
Browse files Browse the repository at this point in the history
if [get_process] is alled concurrently, the ivar might be filled twice.

Fixes #1115

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

<!-- ps-id: d5c77719-29a5-4b46-92e8-c09f6453ba56 -->
  • Loading branch information
rgrinberg committed Jun 11, 2023
1 parent f83459b commit bc1e93a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -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)

Expand Down
14 changes: 7 additions & 7 deletions ocaml-lsp-server/src/ocamlformat_rpc.ml
Expand Up @@ -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
Expand Down Expand Up @@ -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 ()
Expand Down

0 comments on commit bc1e93a

Please sign in to comment.