From 0f90b4665a691e1c7393e5bd116c7f6a0819692e Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 23 Nov 2021 20:04:27 -0500 Subject: [PATCH] chore(lsp): fix possible race condition with tests expecting 3 publishDiagnostics messages (#12868) The tests expect 3 publish notifications. It was possible for less than 3 to occur if two or more tasks set the diagnostics in the collection, exited the lock at the same time, then called `publish_diagnostics` --- cli/lsp/diagnostics.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/cli/lsp/diagnostics.rs b/cli/lsp/diagnostics.rs index 9bf45c5ae8..8a0d50ef1f 100644 --- a/cli/lsp/diagnostics.rs +++ b/cli/lsp/diagnostics.rs @@ -568,10 +568,9 @@ async fn generate_deps_diagnostics( /// Publishes diagnostics to the client. async fn publish_diagnostics( client: &lspower::Client, - collection: Arc>, + collection: &mut DiagnosticCollection, snapshot: &language_server::StateSnapshot, ) { - let mut collection = collection.lock().await; if let Some(changes) = collection.take_changes() { for specifier in changes { let mut diagnostics: Vec = @@ -625,13 +624,12 @@ async fn update_diagnostics( error!("Error generating lint diagnostics: {}", err); }) .unwrap_or_default(); - { - let mut collection = collection.lock().await; - for diagnostic_record in diagnostics { - collection.set(DiagnosticSource::DenoLint, diagnostic_record); - } + + let mut collection = collection.lock().await; + for diagnostic_record in diagnostics { + collection.set(DiagnosticSource::DenoLint, diagnostic_record); } - publish_diagnostics(client, collection, &snapshot).await; + publish_diagnostics(client, &mut collection, &snapshot).await; snapshot.performance.measure(mark); }; @@ -647,13 +645,11 @@ async fn update_diagnostics( error!("Error generating TypeScript diagnostics: {}", err); }) .unwrap_or_default(); - { - let mut collection = collection.lock().await; - for diagnostic_record in diagnostics { - collection.set(DiagnosticSource::TypeScript, diagnostic_record); - } + let mut collection = collection.lock().await; + for diagnostic_record in diagnostics { + collection.set(DiagnosticSource::TypeScript, diagnostic_record); } - publish_diagnostics(client, collection, &snapshot).await; + publish_diagnostics(client, &mut collection, &snapshot).await; snapshot.performance.measure(mark); }; @@ -669,13 +665,11 @@ async fn update_diagnostics( error!("Error generating Deno diagnostics: {}", err); }) .unwrap_or_default(); - { - let mut collection = collection.lock().await; - for diagnostic_record in diagnostics { - collection.set(DiagnosticSource::Deno, diagnostic_record); - } + let mut collection = collection.lock().await; + for diagnostic_record in diagnostics { + collection.set(DiagnosticSource::Deno, diagnostic_record); } - publish_diagnostics(client, collection, &snapshot).await; + publish_diagnostics(client, &mut collection, &snapshot).await; snapshot.performance.measure(mark); };