Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lsp): fix possible race condition with tests expecting 3 publishDiagnostics messages, but getting 2 or 1 #12868

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions cli/lsp/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,9 @@ async fn generate_deps_diagnostics(
/// Publishes diagnostics to the client.
async fn publish_diagnostics(
client: &lspower::Client,
collection: Arc<Mutex<DiagnosticCollection>>,
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<lsp::Diagnostic> =
Expand Down Expand Up @@ -646,13 +645,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);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition was right here where the lock is released and then the lock was acquired in publish_diagnostics.


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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kitsonk would it be better to pass the diagnostics directly to this function? It seems like they get set in the collection and then they're taken from there. Maybe they shouldn't be put in the collection if lint isn't enabled or the specifier isn't enabled?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The considerations is that if you toggle a project as enabled or disabled (or for linting) the only way to clear diagnostics is to publish an empty set for each file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm not going to bother with this for now. I'll just fix this one issue in this PR.

snapshot.performance.measure(mark);
};

Expand All @@ -668,13 +666,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);
};

Expand All @@ -690,13 +686,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);
};

Expand Down