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

fix(lsp): avoid calling client while holding lock #18197

Merged
merged 16 commits into from
Mar 15, 2023
Prev Previous commit
Next Next commit
Passing tests
  • Loading branch information
dsherret committed Mar 14, 2023
commit 52476b858369be91c74de66d081eec519e1b2a1b
29 changes: 29 additions & 0 deletions cli/lsp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ pub enum TestingNotification {
Progress(testing_lsp_custom::TestRunProgressParams),
}

/// A client that provides methods that don't need to be awaited.
///
/// This struct should be used in places where you want to ensure the
/// code doesn't accidentally call into the client while holding an
/// LSP lock, which could then call into the server causing a deadlock.
#[derive(Clone)]
pub struct NotificationOnlyClient(Client);

impl NotificationOnlyClient {
pub fn send_registry_state_notification(
&self,
params: lsp_custom::RegistryStateNotificationParams,
) {
let client = self.0.clone();
tokio::task::spawn(async move {
client.send_registry_state_notification(params).await;
});
}
}

#[derive(Clone)]
pub struct Client(Arc<dyn ClientTrait>);

Expand All @@ -45,6 +65,10 @@ impl Client {
Self(Arc::new(ReplClient))
}

pub fn as_notification_only(&self) -> NotificationOnlyClient {
NotificationOnlyClient(self.clone())
}

pub async fn publish_diagnostics(
&self,
uri: lsp::Url,
Expand Down Expand Up @@ -207,6 +231,10 @@ impl ClientTrait for TowerClient {
uris: Vec<lsp::Url>,
) -> AsyncReturn<Result<Vec<Result<SpecifierSettings, AnyError>>, AnyError>>
{
eprintln!(
"URIS: {:?}",
uris.iter().map(|i| i.as_str()).collect::<Vec<_>>()
);
let client = self.0.clone();
Box::pin(async move {
let config_response = client
Expand Down Expand Up @@ -235,6 +263,7 @@ impl ClientTrait for TowerClient {
}

fn workspace_configuration(&self) -> AsyncReturn<Result<Value, AnyError>> {
eprintln!("WORKSPACE CONFIG");
let client = self.0.clone();
Box::pin(async move {
let config_response = client
Expand Down
20 changes: 9 additions & 11 deletions cli/lsp/completions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use super::client::Client;
use super::client::NotificationOnlyClient;
use super::config::ConfigSnapshot;
use super::documents::Documents;
use super::lsp_custom;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub struct CompletionItemData {
async fn check_auto_config_registry(
url_str: &str,
config: &ConfigSnapshot,
client: Client,
client: &NotificationOnlyClient,
module_registries: &ModuleRegistry,
) {
// check to see if auto discovery is enabled
Expand Down Expand Up @@ -78,14 +78,12 @@ async fn check_auto_config_registry(
// incompatible.
// TODO(@kitsonk) clean up protocol when doing v2 of suggestions
if suggestions {
client
.send_registry_state_notification(
lsp_custom::RegistryStateNotificationParams {
origin,
suggestions,
},
)
.await;
client.send_registry_state_notification(
lsp_custom::RegistryStateNotificationParams {
origin,
suggestions,
},
);
}
}
}
Expand Down Expand Up @@ -139,7 +137,7 @@ pub async fn get_import_completions(
specifier: &ModuleSpecifier,
position: &lsp::Position,
config: &ConfigSnapshot,
client: Client,
client: &NotificationOnlyClient,
module_registries: &ModuleRegistry,
documents: &Documents,
maybe_import_map: Option<Arc<ImportMap>>,
Expand Down
2 changes: 0 additions & 2 deletions cli/lsp/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use super::logging::lsp_log;
use crate::util::path::ensure_directory_specifier;
use crate::util::path::specifier_to_file_path;
use deno_core::error::AnyError;
use deno_core::serde::Deserialize;
Expand Down Expand Up @@ -575,7 +574,6 @@ impl Config {
workspace: ModuleSpecifier,
enabled_paths: Vec<String>,
) -> bool {
let workspace = ensure_directory_specifier(workspace);
let mut touched = false;
if !enabled_paths.is_empty() {
if let Ok(workspace_path) = specifier_to_file_path(&workspace) {
Expand Down
Loading