mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-12 05:15:00 +00:00
Eliminate RwLock around LanguageServer's outbound message channel
We observed a deadlock when quitting zed. The main thread was attempting to acquire a write lock to this outbound message sender. We weren't able to understand exactly how this occurred, but we removed the use of a lock there, so this shouldn't happen anymore. Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
parent
368301fcec
commit
9a97588f79
1 changed files with 30 additions and 37 deletions
|
@ -33,7 +33,7 @@ type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
|
||||||
|
|
||||||
pub struct LanguageServer {
|
pub struct LanguageServer {
|
||||||
next_id: AtomicUsize,
|
next_id: AtomicUsize,
|
||||||
outbound_tx: RwLock<Option<channel::Sender<Vec<u8>>>>,
|
outbound_tx: channel::Sender<Vec<u8>>,
|
||||||
capabilities: watch::Receiver<Option<ServerCapabilities>>,
|
capabilities: watch::Receiver<Option<ServerCapabilities>>,
|
||||||
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
|
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
|
||||||
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
|
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
|
||||||
|
@ -213,7 +213,7 @@ impl LanguageServer {
|
||||||
response_handlers,
|
response_handlers,
|
||||||
capabilities: capabilities_rx,
|
capabilities: capabilities_rx,
|
||||||
next_id: Default::default(),
|
next_id: Default::default(),
|
||||||
outbound_tx: RwLock::new(Some(outbound_tx)),
|
outbound_tx,
|
||||||
executor: executor.clone(),
|
executor: executor.clone(),
|
||||||
io_tasks: Mutex::new(Some((input_task, output_task))),
|
io_tasks: Mutex::new(Some((input_task, output_task))),
|
||||||
initialized: initialized_rx,
|
initialized: initialized_rx,
|
||||||
|
@ -296,37 +296,41 @@ impl LanguageServer {
|
||||||
let request = Self::request_internal::<request::Initialize>(
|
let request = Self::request_internal::<request::Initialize>(
|
||||||
&this.next_id,
|
&this.next_id,
|
||||||
&this.response_handlers,
|
&this.response_handlers,
|
||||||
this.outbound_tx.read().as_ref(),
|
&this.outbound_tx,
|
||||||
params,
|
params,
|
||||||
);
|
);
|
||||||
let response = request.await?;
|
let response = request.await?;
|
||||||
Self::notify_internal::<notification::Initialized>(
|
Self::notify_internal::<notification::Initialized>(
|
||||||
this.outbound_tx.read().as_ref(),
|
&this.outbound_tx,
|
||||||
InitializedParams {},
|
InitializedParams {},
|
||||||
)?;
|
)?;
|
||||||
Ok(response.capabilities)
|
Ok(response.capabilities)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Result<()>>> {
|
pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Option<()>>> {
|
||||||
if let Some(tasks) = self.io_tasks.lock().take() {
|
if let Some(tasks) = self.io_tasks.lock().take() {
|
||||||
let response_handlers = self.response_handlers.clone();
|
let response_handlers = self.response_handlers.clone();
|
||||||
let outbound_tx = self.outbound_tx.write().take();
|
|
||||||
let next_id = AtomicUsize::new(self.next_id.load(SeqCst));
|
let next_id = AtomicUsize::new(self.next_id.load(SeqCst));
|
||||||
|
let outbound_tx = self.outbound_tx.clone();
|
||||||
let mut output_done = self.output_done_rx.lock().take().unwrap();
|
let mut output_done = self.output_done_rx.lock().take().unwrap();
|
||||||
Some(async move {
|
let shutdown_request = Self::request_internal::<request::Shutdown>(
|
||||||
Self::request_internal::<request::Shutdown>(
|
&next_id,
|
||||||
&next_id,
|
&response_handlers,
|
||||||
&response_handlers,
|
&outbound_tx,
|
||||||
outbound_tx.as_ref(),
|
(),
|
||||||
(),
|
);
|
||||||
)
|
let exit = Self::notify_internal::<notification::Exit>(&outbound_tx, ());
|
||||||
.await?;
|
outbound_tx.close();
|
||||||
Self::notify_internal::<notification::Exit>(outbound_tx.as_ref(), ())?;
|
Some(
|
||||||
drop(outbound_tx);
|
async move {
|
||||||
output_done.recv().await;
|
shutdown_request.await?;
|
||||||
drop(tasks);
|
exit?;
|
||||||
Ok(())
|
output_done.recv().await;
|
||||||
})
|
drop(tasks);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
.log_err(),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -375,7 +379,7 @@ impl LanguageServer {
|
||||||
Self::request_internal::<T>(
|
Self::request_internal::<T>(
|
||||||
&this.next_id,
|
&this.next_id,
|
||||||
&this.response_handlers,
|
&this.response_handlers,
|
||||||
this.outbound_tx.read().as_ref(),
|
&this.outbound_tx,
|
||||||
params,
|
params,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
@ -385,7 +389,7 @@ impl LanguageServer {
|
||||||
fn request_internal<T: request::Request>(
|
fn request_internal<T: request::Request>(
|
||||||
next_id: &AtomicUsize,
|
next_id: &AtomicUsize,
|
||||||
response_handlers: &Mutex<HashMap<usize, ResponseHandler>>,
|
response_handlers: &Mutex<HashMap<usize, ResponseHandler>>,
|
||||||
outbound_tx: Option<&channel::Sender<Vec<u8>>>,
|
outbound_tx: &channel::Sender<Vec<u8>>,
|
||||||
params: T::Params,
|
params: T::Params,
|
||||||
) -> impl 'static + Future<Output = Result<T::Result>>
|
) -> impl 'static + Future<Output = Result<T::Result>>
|
||||||
where
|
where
|
||||||
|
@ -415,16 +419,8 @@ impl LanguageServer {
|
||||||
);
|
);
|
||||||
|
|
||||||
let send = outbound_tx
|
let send = outbound_tx
|
||||||
.as_ref()
|
.try_send(message)
|
||||||
.ok_or_else(|| {
|
.context("failed to write to language server's stdin");
|
||||||
anyhow!("tried to send a request to a language server that has been shut down")
|
|
||||||
})
|
|
||||||
.and_then(|outbound_tx| {
|
|
||||||
outbound_tx
|
|
||||||
.try_send(message)
|
|
||||||
.context("failed to write to language server's stdin")?;
|
|
||||||
Ok(())
|
|
||||||
});
|
|
||||||
async move {
|
async move {
|
||||||
send?;
|
send?;
|
||||||
rx.recv().await.unwrap()
|
rx.recv().await.unwrap()
|
||||||
|
@ -438,13 +434,13 @@ impl LanguageServer {
|
||||||
let this = self.clone();
|
let this = self.clone();
|
||||||
async move {
|
async move {
|
||||||
this.initialized.clone().recv().await;
|
this.initialized.clone().recv().await;
|
||||||
Self::notify_internal::<T>(this.outbound_tx.read().as_ref(), params)?;
|
Self::notify_internal::<T>(&this.outbound_tx, params)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn notify_internal<T: notification::Notification>(
|
fn notify_internal<T: notification::Notification>(
|
||||||
outbound_tx: Option<&channel::Sender<Vec<u8>>>,
|
outbound_tx: &channel::Sender<Vec<u8>>,
|
||||||
params: T::Params,
|
params: T::Params,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let message = serde_json::to_vec(&Notification {
|
let message = serde_json::to_vec(&Notification {
|
||||||
|
@ -453,9 +449,6 @@ impl LanguageServer {
|
||||||
params,
|
params,
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let outbound_tx = outbound_tx
|
|
||||||
.as_ref()
|
|
||||||
.ok_or_else(|| anyhow!("tried to notify a language server that has been shut down"))?;
|
|
||||||
outbound_tx.try_send(message)?;
|
outbound_tx.try_send(message)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue