Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ocaml-lsp-server crash in Emacs #1115

Closed
mariusdkm opened this issue Jun 3, 2023 · 5 comments · Fixed by #1132
Closed

ocaml-lsp-server crash in Emacs #1115

mariusdkm opened this issue Jun 3, 2023 · 5 comments · Fixed by #1132
Labels
bug Something isn't working
Milestone

Comments

@mariusdkm
Copy link

mariusdkm commented Jun 3, 2023

I use ocaml-lsp-server installed through opam in my Emacs(GNU Emacs 28.2 running on a M1 Mac)

For some reason if I start the lsp through M-x lsp it just crashes with the error message:
(Side note I have dune build --watch running in the project, but this also happens without running it)

Process ocaml-lsp-server stderr finished
/-----------------------------------------------------------------------
| Internal error: Uncaught exception.
| Failure("Fiber.Ivar.fill")
| Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
| Called from Fiber__Scheduler.exec in file "src/fiber/scheduler.ml", line 87, characters 32-61
| Called from Fiber__Scheduler.advance.(fun) in file "src/fiber/scheduler.ml", line 195, characters 2-58
| Called from Fiber.run.loop in file "src/fiber/fiber.ml", line 15, characters 30-61
| Called from Fiber.run.(fun) in file "src/fiber/fiber.ml" (inlined), line 17, characters 17-47
| Called from Lev_fiber.run in file "submodules/lev/lev-fiber/src/lev_fiber.ml", line 1264, characters 10-59
| Re-raised at Stdune__Exn.raise_with_backtrace in file "otherlibs/stdune/exn.ml" (inlined), line 36, characters 27-56
| Called from Stdune__Exn_with_backtrace.reraise in file "otherlibs/stdune/exn_with_backtrace.ml", line 18, characters 33-71
| Called from Stdune__Exn_with_backtrace.try_with in file "otherlibs/stdune/exn_with_backtrace.ml", line 9, characters 8-12
\-----------------------------------------------------------------------

And this is the lsp-log

Command "ocaml-language-server --stdio" is not present on the path.
Command "ocamllsp" is present on the path.
Command "ocaml-language-server --stdio" is not present on the path.
Command "ocamllsp" is present on the path.
Found the following clients for /Users/_myuser_/Documents/uni/fpv/_school_project_/src/assignment.ml: (server-id ocaml-lsp-server, priority 0)
The following clients were selected based on priority: (server-id ocaml-lsp-server, priority 0)
Connected to dune /Users/_myuser_/Documents/uni/fpv/_school_project_ (/Users/_myuser_/Documents/uni/fpv/_school_project_/_build/.rpc/dune)
client 0: connecting...
client 0: connected to dune at unix:///Users/_myuser_/Documents/uni/fpv/_school_project_/_build/.rpc/dune

My installed ocaml-lsp-server version:

~ opam info ocaml-lsp-server

<><> ocaml-lsp-server: information on all versions ><><><><><><><><><><><><>  🐫 
name                   ocaml-lsp-server
all-installed-versions 1.15.1-5.0 [5.0.0]
My emacs config for ocaml

(add-to-list 'exec-path "/Users/mariusdekuthymeurers/.opam/5.0.0/bin")

;; Major mode for OCaml programming
(use-package tuareg
  :ensure t
  :mode (("\\.ocamlinit\\'" . tuareg-mode)))

;; Major mode for editing Dune project files
(use-package dune
  :ensure t)

(use-package utop
  :ensure t)

;; Merlin provides advanced IDE features
(use-package merlin
  :ensure t
  :config
  (add-hook 'tuareg-mode-hook #'merlin-mode)
  (add-hook 'merlin-mode-hook #'company-mode)
  ;; we're using flycheck instead
  (setq merlin-error-after-save nil))

(use-package merlin-eldoc
  :ensure t
  :hook ((tuareg-mode) . merlin-eldoc-setup))

;; This uses Merlin internally
(use-package flycheck-ocaml
  :ensure t
  :config
  (flycheck-ocaml-setup))

@rgrinberg
Copy link
Member

Can you apply the following patch to fiber and then try reproducing again:

diff --git a/otherlibs/fiber/src/core.ml b/otherlibs/fiber/src/core.ml
index a571e69e4..f005839a4 100644
--- a/otherlibs/fiber/src/core.ml
+++ b/otherlibs/fiber/src/core.ml
@@ -32,7 +32,10 @@ and eff =
   | Toplevel_exception : Exn_with_backtrace.t -> eff
   | Done of value
 
-and 'a ivar = { mutable state : ('a, [ `Full | `Empty ]) ivar_state }
+and 'a ivar =
+  { mutable state : ('a, [ `Full | `Empty ]) ivar_state
+  ; source : Printexc.raw_backtrace
+  }
 
 and ('a, _) ivar_state =
   | Full : 'a -> ('a, [> `Full ]) ivar_state
@@ -210,7 +213,9 @@ let reraise_all l _k =
 module Ivar = struct
   type 'a t = 'a ivar
 
-  let create () = { state = Empty }
+  let create () =
+    let source = Printexc.get_callstack 20 in
+    { state = Empty; source }
 
   let read t k = Read_ivar (t, k)
 
diff --git a/otherlibs/fiber/src/scheduler.ml b/otherlibs/fiber/src/scheduler.ml
index f3c2ac679..d279614f1 100644
--- a/otherlibs/fiber/src/scheduler.ml
+++ b/otherlibs/fiber/src/scheduler.ml
@@ -22,7 +22,10 @@ module Jobs = struct
 
   let fill_ivar ivar x jobs =
     match ivar.state with
-    | Full _ -> failwith "Fiber.Ivar.fill"
+    | Full _ ->
+      let exn = Failure "Ivar.fill" in
+      let backtrace = ivar.source in
+      Exn_with_backtrace.reraise { Exn_with_backtrace.backtrace; exn }
     | Empty ->
       ivar.state <- Full x;
       jobs

@mariusdkm
Copy link
Author

How do I apply these patches?
I found the scheduler.ml and core.ml files in my opam lib directoy (~./.opam/5.0.0/lib/fiber/)
But if I apply the patches there nothing happens, and if I do opam reinstall it redownloads them and overrides my patches.

@rgrinberg
Copy link
Member

You would have to clone the repe of fiber, checkout the appropriate version, apply the patch, and then pin the repo (see opam pin --help).

@mariusdkm
Copy link
Author

Ok, this took way longer to do then I expected to install the fiber package with the specific patches.
But here is the new stack trace:

/-----------------------------------------------------------------------
| Internal error: Uncaught exception.
| Failure("Ivar.fill")
| Raised by primitive operation at Fiber__Core.Ivar.create in file "fiber/src/core.ml", line 217, characters 17-42
| Called from Ocaml_lsp_server__Ocamlformat_rpc.create_state in file "ocaml-lsp-server/src/ocamlformat_rpc.ml", line 188, characters 17-37
| Called from Ocaml_lsp_server__Ocamlformat_rpc.create in file "ocaml-lsp-server/src/ocamlformat_rpc.ml" (inlined), line 190, characters 53-68
| Called from Ocaml_lsp_server.start in file "ocaml-lsp-server/src/ocaml_lsp_server.ml", line 753, characters 24-49
| Called from Fiber__Scheduler.exec in file "fiber/src/scheduler.ml", line 76, characters 8-11
| Called from Fiber__Scheduler.start in file "fiber/src/scheduler.ml", line 219, characters 2-42
| Called from Fiber.run.(fun) in file "fiber/src/fiber.ml" (inlined), line 17, characters 28-47
| Called from Lev_fiber.run in file "submodules/lev/lev-fiber/src/lev_fiber.ml", line 1264, characters 10-59
| Called from Ocaml_lsp_server.run in file "ocaml-lsp-server/src/ocaml_lsp_server.ml", line 868, characters 2-151
| Called from Stdune__Exn_with_backtrace.try_with in file "otherlibs/stdune/exn_with_backtrace.ml", line 9, characters 8-12
| Called from Dune__exe__Main in file "ocaml-lsp-server/bin/main.ml", line 40, characters 6-106
| Re-raised at Stdune__Exn.raise_with_backtrace in file "otherlibs/stdune/exn.ml" (inlined), line 36, characters 27-56
| Called from Stdune__Exn_with_backtrace.reraise in file "otherlibs/stdune/exn_with_backtrace.ml", line 18, characters 33-71
| Called from Fiber__Scheduler.exec in file "fiber/src/scheduler.ml", line 90, characters 32-61
| Called from Fiber__Scheduler.advance.(fun) in file "fiber/src/scheduler.ml", line 198, characters 2-58
| Called from Fiber.run.loop in file "fiber/src/fiber.ml", line 15, characters 30-61
| Called from Fiber.run.(fun) in file "fiber/src/fiber.ml" (inlined), line 17, characters 17-47
| Called from Lev_fiber.run in file "submodules/lev/lev-fiber/src/lev_fiber.ml", line 1264, characters 10-59
| Re-raised at Stdune__Exn.raise_with_backtrace in file "otherlibs/stdune/exn.ml" (inlined), line 36, characters 27-56
| Called from Stdune__Exn_with_backtrace.reraise in file "otherlibs/stdune/exn_with_backtrace.ml", line 18, characters 33-71
| Called from Stdune__Exn_with_backtrace.try_with in file "otherlibs/stdune/exn_with_backtrace.ml", line 9, characters 8-12
\-----------------------------------------------------------------------


Process ocaml-lsp-server stderr finished

@rgrinberg rgrinberg added the bug Something isn't working label Jun 9, 2023
rgrinberg added a commit that referenced this issue Jun 10, 2023
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 -->
@rgrinberg
Copy link
Member

Can you see #1132 fixes the issue for you?

@rgrinberg rgrinberg added this to the 1.16.0 milestone Jun 10, 2023
rgrinberg added a commit that referenced this issue Jun 11, 2023
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 -->
rgrinberg added a commit that referenced this issue Jun 11, 2023
if [get_process] is alled concurrently, the ivar might be filled twice.

Fixes #1115

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants