Skip to content

Commit

Permalink
Move loading of system files to before server declares initialized
Browse files Browse the repository at this point in the history
Loading of system files will sent mutating requests to self which can
deadlock with requests by users. Finish this work before accepting user
requests or notifications.
  • Loading branch information
bbannier committed Apr 19, 2022
1 parent beb882a commit 2b785f3
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ struct Backend {
}

impl Backend {
async fn info_message<M>(&self, message: M)
where
M: std::fmt::Display,
{
if let Some(client) = &self.client {
// Send these to the log by default.
client.log_message(MessageType::INFO, message).await;
}
}

async fn warn_message<M>(&self, message: M)
where
M: std::fmt::Display,
Expand Down Expand Up @@ -272,6 +262,29 @@ impl LanguageServer for Backend {
state.set_workspace_folders(Arc::new(workspace_folders));
}

{
// Load system scripts.
match zeek::prefixes().await {
Ok(prefixes) => {
if let Ok(mut state) = self.state_mut() {
state.set_prefixes(Arc::new(prefixes));
}
}
Err(e) => error!("{e}"),
}

// Load all visible files.
if let Ok(files) = self.visible_files().await {
self.did_change_watched_files(DidChangeWatchedFilesParams {
changes: files
.into_iter()
.map(|f| FileEvent::new(f, FileChangeType::CREATED))
.collect(),
})
.await;
}
}

Ok(InitializeResult {
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
Expand Down Expand Up @@ -303,29 +316,7 @@ impl LanguageServer for Backend {
}

#[instrument]
async fn initialized(&self, _: InitializedParams) {
match zeek::prefixes().await {
Ok(prefixes) => {
if let Ok(mut state) = self.state_mut() {
state.set_prefixes(Arc::new(prefixes));
}
}
Err(e) => error!("{e}"),
}

// Load all visible files.
if let Ok(files) = self.visible_files().await {
self.did_change_watched_files(DidChangeWatchedFilesParams {
changes: files
.into_iter()
.map(|f| FileEvent::new(f, FileChangeType::CREATED))
.collect(),
})
.await;
}

self.info_message("server initialized!").await;
}
async fn initialized(&self, _: InitializedParams) {}

#[instrument]
async fn shutdown(&self) -> Result<()> {
Expand Down

0 comments on commit 2b785f3

Please sign in to comment.