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: concurrency deadlock #1980

Merged
merged 2 commits into from
Mar 6, 2024
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
27 changes: 17 additions & 10 deletions crates/biome_service/src/workspace/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl WorkspaceServer {
self.documents
.get(path)
.map(|doc| doc.file_source_index)
.and_then(|index| self.file_sources.read().unwrap().get_index(index).copied())
.and_then(|index| self.get_source(index))
.unwrap_or(DocumentFileSource::from_path(path))
}

Expand Down Expand Up @@ -168,6 +168,17 @@ impl WorkspaceServer {
}
}

fn get_source(&self, index: usize) -> Option<DocumentFileSource> {
let file_sources = self.file_sources.read().unwrap();
file_sources.get_index(index).copied()
}

fn set_source(&self, document_file_source: DocumentFileSource) -> usize {
let mut file_sources = self.file_sources.write().unwrap();
let (index, _) = file_sources.insert_full(document_file_source);
index
}

/// Get the parser result for a given file
///
/// Returns and error if no file exists in the workspace with this path or
Expand Down Expand Up @@ -207,13 +218,12 @@ impl WorkspaceServer {
}

let settings = self.settings();
let mut file_sources = self.file_sources.write().unwrap();
let Some(file_source) = file_sources.get_index(document.file_source_index) else {
let Some(file_source) = self.get_source(document.file_source_index) else {
return Err(WorkspaceError::not_found());
};
let parsed = parse(
biome_path,
*file_source,
file_source,
document.content.as_str(),
settings,
&mut document.node_cache,
Expand All @@ -223,8 +233,7 @@ impl WorkspaceServer {
any_parse,
} = parsed;
if let Some(language) = language {
let (index, _) = file_sources.insert_full(language);
document.file_source_index = index;
document.file_source_index = self.set_source(language);
}
Ok(entry.insert(any_parse).clone())
}
Expand Down Expand Up @@ -376,8 +385,7 @@ impl Workspace for WorkspaceServer {

/// Add a new file to the workspace
fn open_file(&self, params: OpenFileParams) -> Result<(), WorkspaceError> {
let mut file_sources = self.file_sources.write().unwrap();
let (index, _) = file_sources.insert_full(
let index = self.set_source(
params
.document_file_source
.unwrap_or(DocumentFileSource::from_path(&params.path)),
Expand All @@ -396,8 +404,7 @@ impl Workspace for WorkspaceServer {
}

fn open_project(&self, params: OpenProjectParams) -> Result<(), WorkspaceError> {
let mut file_sources = self.file_sources.write().unwrap();
let (index, _) = file_sources.insert_full(JsonFileSource::json().into());
let index = self.set_source(JsonFileSource::json().into());
self.syntax.remove(&params.path);
self.documents.insert(
params.path,
Expand Down
Loading