From 70a35e7b99b68162184c4a37e10486b22515306c Mon Sep 17 00:00:00 2001 From: Jamie Tanna Date: Thu, 1 Oct 2026 14:46:08 +0100 Subject: [PATCH] fix: exit shortly after `shutdown` so clients can stop the server I've noticed recently that when calling `:lsp stop vale_ls` in Neovim, the `vale-ls` process remained alive. With some investigation from Claude Sonnet 5.5, it highlighted that `tower-lsp` 0.19's `Server::serve` doesn't return on `exit`. It only marks the service as exited, and the final `join!` waits on channels whose senders live outside the read loop, so it only completes once stdin reaches EOF. This also means anything placed after `serve()` in `main` is never reached. As clients like Neovim send `shutdown` then `exit`, and keep stdin open until the process exits, neither LSP client nor LSP server can proceed. In particular for Neovim, this means that `on_exit` and `LspDetach` never fire. As `exit` is the only valid message after `shutdown`, we can instead exit the process from the `shutdown` handler, after a short delay so the reply has been written. Now, we exit with exit code 0 ~0.2s after `shutdown` with stdin still open, with or without a following `exit`. We use 200ms as a guesstimate of a reasonable time to wait for a response ot be flushed. A client that was extremely slow to read could see the process exit before the reply, which should be treated as a normal shutdown. Co-Authored-By: Claude Sonnet 5.5 --- src/server.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/server.rs b/src/server.rs index aa1ca02..68dce48 100644 --- a/src/server.rs +++ b/src/server.rs @@ -112,6 +112,16 @@ impl LanguageServer for Backend { } async fn shutdown(&self) -> Result<()> { + // tower-lsp's `serve` doesn't return on `exit`: it only finishes once + // stdin closes. Clients like Neovim send `exit` but keep stdin open + // until the process quits, so they'd wait forever. `exit` is the only + // valid message after `shutdown`, so leave once the reply has been + // written. + tokio::spawn(async { + tokio::time::sleep(Duration::from_millis(200)).await; + std::process::exit(0); + }); + Ok(()) }