on master help sign in
Handle language server shutdown with timeout
commitc2aad859b12e01c7724f16ddf957e6db2b3c8a60 authorwojciechkepka <wojtek.kepka@protonmail.com> dateSat 19 Jun 2021, 04:56 (5 years ago) parent03d1ca7
3 files changed+31−1
helix-lsp/src/client.rs+15 helix-lsp/src/lib.rs+4 helix-term/src/application.rs+12−1
helix-lsp/src/client.rs+15
@@ -272,6 +272,21 @@ impl Client {
272272 self.notify::<lsp::notification::Exit>(())
273273 }
274274
275+ /// Tries to shut down the language server but returns
276+ /// early if server responds with an error.
277+ pub async fn shutdown_and_exit(&self) -> Result<()> {
278+ self.shutdown().await?;
279+ self.exit().await
280+ }
281+
282+ /// Forcefully shuts down the language server ignoring any errors.
283+ pub async fn force_shutdown(&self) -> Result<()> {
284+ if let Err(e) = self.shutdown().await {
285+ log::warn!("language server failed to terminate gracefully - {}", e);
286+ }
287+ self.exit().await
288+ }
289+
275290 // -------------------------------------------------------------------------------------------
276291 // Text document
277292 // -------------------------------------------------------------------------------------------
helix-lsp/src/lib.rs+4
@@ -310,6 +310,10 @@ impl Registry {
310310 Err(Error::LspNotDefined)
311311 }
312312 }
313+
314+ pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> {
315+ self.inner.values().map(|(_, client)| client)
316+ }
313317}
314318
315319#[derive(Debug)]
helix-term/src/application.rs+12−1
@@ -23,7 +23,7 @@ use crossterm::{
2323
2424use tui::layout::Rect;
2525
26−use futures_util::stream::FuturesUnordered;
26+use futures_util::{future, stream::FuturesUnordered};
2727
2828type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
2929pub type LspCallback =
@@ -406,6 +406,17 @@ impl Application {
406406
407407 self.event_loop().await;
408408
409+ tokio::time::timeout(
410+ Duration::from_millis(500),
411+ future::join_all(
412+ self.editor
413+ .language_servers
414+ .iter_clients()
415+ .map(|client| client.force_shutdown()),
416+ ),
417+ )
418+ .await;
419+
409420 // reset cursor shape
410421 write!(stdout, "\x1B[2 q");
411422