Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Cancel language server requests if request is dropped

Thorsten Ball1a6cd7333429
diff --git a/crates/lsp/src/lsp.rs b/crates/lsp/src/lsp.rs
index a70422008cc..31b85b180ba 100644
--- a/crates/lsp/src/lsp.rs
+++ b/crates/lsp/src/lsp.rs
@@ -863,17 +863,31 @@ impl LanguageServer {
.try_send(message)
.context("failed to write to language server's stdin");
+ let outbound_tx = outbound_tx.downgrade();
let mut timeout = executor.timer(LSP_REQUEST_TIMEOUT).fuse();
let started = Instant::now();
async move {
handle_response?;
send?;
+ let cancel_on_drop = util::defer(move || {
+ if let Some(outbound_tx) = outbound_tx.upgrade() {
+ Self::notify_internal::<notification::Cancel>(
+ &outbound_tx,
+ CancelParams {
+ id: NumberOrString::Number(id as i32),
+ },
+ )
+ .log_err();
+ }
+ });
+
let method = T::METHOD;
futures::select! {
response = rx.fuse() => {
let elapsed = started.elapsed();
log::trace!("Took {elapsed:?} to receive response to {method:?} id {id}");
+ cancel_on_drop.abort();
response?
}
diff --git a/crates/util/src/util.rs b/crates/util/src/util.rs
index a2f8b87feea..ccf612e16a2 100644
--- a/crates/util/src/util.rs
+++ b/crates/util/src/util.rs
@@ -299,7 +299,7 @@ pub struct Deferred<F: FnOnce()>(Option<F>);
impl<F: FnOnce()> Deferred<F> {
/// Drop without running the deferred function.
- pub fn cancel(mut self) {
+ pub fn abort(mut self) {
self.0.take();
}
}