Skip to content

Commit

Permalink
Handle language server shutdown with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
vv9k authored and archseer committed Jun 19, 2021
1 parent 03d1ca7 commit c2aad85
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
15 changes: 15 additions & 0 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ impl Client {
self.notify::<lsp::notification::Exit>(())
}

/// Tries to shut down the language server but returns
/// early if server responds with an error.
pub async fn shutdown_and_exit(&self) -> Result<()> {
self.shutdown().await?;
self.exit().await
}

/// Forcefully shuts down the language server ignoring any errors.
pub async fn force_shutdown(&self) -> Result<()> {
if let Err(e) = self.shutdown().await {
log::warn!("language server failed to terminate gracefully - {}", e);
}
self.exit().await
}

// -------------------------------------------------------------------------------------------
// Text document
// -------------------------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions helix-lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ impl Registry {
Err(Error::LspNotDefined)
}
}

pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> {
self.inner.values().map(|(_, client)| client)
}
}

#[derive(Debug)]
Expand Down
13 changes: 12 additions & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crossterm::{

use tui::layout::Rect;

use futures_util::stream::FuturesUnordered;
use futures_util::{future, stream::FuturesUnordered};

type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
pub type LspCallback =
Expand Down Expand Up @@ -406,6 +406,17 @@ impl Application {

self.event_loop().await;

tokio::time::timeout(
Duration::from_millis(500),
future::join_all(
self.editor
.language_servers
.iter_clients()
.map(|client| client.force_shutdown()),
),
)
.await;

// reset cursor shape
write!(stdout, "\x1B[2 q");

Expand Down

0 comments on commit c2aad85

Please sign in to comment.