on master help sign in
LSP: Forcefully shutdown uninitialized servers (#7449)

The LSP spec has this to say about initialize: > Until the server has responded to the `initialize` request with an > `InitializeResult`, the client must not send any additional requests > or notifications to the server. (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize) The spec is not really explicit about how to handle this scenario. Before a client sends the 'initialize' request we are allowed to send an 'exit' notification, but after 'initialize' we can't send any requests (like shutdown) or notifications (like exit). So my intepretation is that we should forcefully close the server in this state. This matches the behavior of Neovim's built-in LSP client: https://github.com/neovim/neovim/blob/5ceb2238d3685255cd517ca87fd7edae9ed88811/runtime/lua/vim/lsp.lua#L1610-L1628

commite0bb032f0e6dcc9b84e571c345928fba4054855e authorMichael Davis <mcarsondavis@gmail.com> dateThu 29 Jun 2023, 10:25 (3 years ago) parentb745fb2
1 file changed+9−1
helix-lsp/src/transport.rs+9−1
helix-lsp/src/transport.rs+9−1
@@ -353,6 +353,11 @@ impl Transport {
353353 }
354354 }
355355
356+ fn is_shutdown(payload: &Payload) -> bool {
357+ use lsp_types::request::{Request, Shutdown};
358+ matches!(payload, Payload::Request { value: jsonrpc::MethodCall { method, .. }, .. } if method == Shutdown::METHOD)
359+ }
360+
356361 // TODO: events that use capabilities need to do the right thing
357362
358363 loop {
@@ -391,7 +396,10 @@ impl Transport {
391396 }
392397 msg = client_rx.recv() => {
393398 if let Some(msg) = msg {
394− if is_pending && !is_initialize(&msg) {
399+ if is_pending && is_shutdown(&msg) {
400+ log::info!("Language server not initialized, shutting down");
401+ break;
402+ } else if is_pending && !is_initialize(&msg) {
395403 // ignore notifications
396404 if let Payload::Notification(_) = msg {
397405 continue;