Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(ls): avoid config/doc_state lock-order deadlock (#4711)
M. Willis Monroecb3681b178be
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rsindex a23ce6ace..622340f7e 100644--- a/harper-ls/src/backend.rs+++ b/harper-ls/src/backend.rs@@ -152,7 +152,7 @@ impl Backend {async fn load_user_dictionary(&self) -> MutableDictionary {let config = self.config.read().await;- load_dict(&config.user_dict_path, self.config.read().await.dialect)+ load_dict(&config.user_dict_path, config.dialect).await.map_err(|err| info!("{err}")).unwrap_or(MutableDictionary::new())@@ -168,13 +168,10 @@ impl Backend {async fn load_workspace_dictionary(&self) -> MutableDictionary {let config = self.config.read().await;- load_dict(- &config.workspace_dict_path,- self.config.read().await.dialect,- )- .await- .map_err(|err| info!("{err}"))- .unwrap_or(MutableDictionary::new())+ load_dict(&config.workspace_dict_path, config.dialect)+ .await+ .map_err(|err| info!("{err}"))+ .unwrap_or(MutableDictionary::new())}async fn save_workspace_dictionary(&self, dict: impl Dictionary) -> Result<()> {@@ -437,12 +434,17 @@ impl Backend {uri: &Uri,range: Range,) -> JsonResult<Vec<CodeActionOrCommand>> {- let (config, mut doc_states) = tokio::join!(self.config.read(), self.doc_state.lock());+ // Copy necessary configuration to avoid holding lock.+ // `update_document` takes `doc_state` before `config`, so holding a `config` read guard+ // while waiting for `doc_state` deadlocks once a `config` writer is queued.+ let code_action_config = self.config.read().await.code_action_config.clone();++ let mut doc_states = self.doc_state.lock().await;let Some(doc_state) = doc_states.get_mut(uri) else {return Ok(Vec::new());};- Ok(doc_state.generate_code_actions(range, &config.code_action_config))+ Ok(doc_state.generate_code_actions(range, &code_action_config))}async fn generate_diagnostics(&self, uri: &Uri) -> Vec<Diagnostic> {