From 106ca5076fd8d485a9016fa202d618efb66e40dc Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 18 Sep 2024 16:43:59 -0700 Subject: [PATCH] Fix leak of LMDB connection in semantic index (#17992) Apparently, to close LMDB's file descriptors when using the `heed` library, you need to explicitly call `prepare_for_closing`. Release Notes: - N/A --------- Co-authored-by: Richard Feldman Co-authored-by: Jason --- crates/evals/src/eval.rs | 9 +++++++++ crates/semantic_index/src/semantic_index.rs | 12 +++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/evals/src/eval.rs b/crates/evals/src/eval.rs index 751dcd09aa..708cfa7511 100644 --- a/crates/evals/src/eval.rs +++ b/crates/evals/src/eval.rs @@ -446,6 +446,15 @@ async fn run_evaluation( println!("{}", serde_json::to_string(&query_results).unwrap()); } + + user_store + .update(cx, |_, _| { + drop(semantic_index); + drop(project); + drop(worktree); + drop(project_index); + }) + .unwrap(); } eprint!( diff --git a/crates/semantic_index/src/semantic_index.rs b/crates/semantic_index/src/semantic_index.rs index 3435d0a9ca..6c97ece024 100644 --- a/crates/semantic_index/src/semantic_index.rs +++ b/crates/semantic_index/src/semantic_index.rs @@ -25,7 +25,7 @@ pub use summary_index::FileSummary; pub struct SemanticDb { embedding_provider: Arc, - db_connection: heed::Env, + db_connection: Option, project_indices: HashMap, Model>, } @@ -70,7 +70,7 @@ impl SemanticDb { .ok(); Ok(SemanticDb { - db_connection, + db_connection: Some(db_connection), embedding_provider, project_indices: HashMap::default(), }) @@ -148,7 +148,7 @@ impl SemanticDb { let project_index = cx.new_model(|cx| { ProjectIndex::new( project.clone(), - self.db_connection.clone(), + self.db_connection.clone().unwrap(), self.embedding_provider.clone(), cx, ) @@ -171,6 +171,12 @@ impl SemanticDb { } } +impl Drop for SemanticDb { + fn drop(&mut self) { + self.db_connection.take().unwrap().prepare_for_closing(); + } +} + #[cfg(test)] mod tests { use super::*;