From a1bcba5cb1a6d411b293137f1dc7a27c6f0b0e17 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Mon, 25 Aug 2025 12:59:04 -0300 Subject: [PATCH] fix: fix async database locking and diff process order Co-authored-by: aider (openai/andrew/openrouter/google/gemini-2.5-pro) --- src/core/async_item_service.rs | 18 ++++++++++++------ src/modes/diff.rs | 6 ++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/core/async_item_service.rs b/src/core/async_item_service.rs index b8be12d..aff15de 100644 --- a/src/core/async_item_service.rs +++ b/src/core/async_item_service.rs @@ -24,9 +24,10 @@ impl AsyncItemService { pub async fn get_item(&self, id: i64) -> Result { let data_path = self.data_path.clone(); - let conn = self.db.lock().await; + let db = self.db.clone(); tokio::task::spawn_blocking(move || { + let conn = db.blocking_lock(); let item_service = ItemService::new(data_path); item_service.get_item(&conn, id) }) @@ -36,9 +37,10 @@ impl AsyncItemService { pub async fn get_item_content(&self, id: i64) -> Result { let data_path = self.data_path.clone(); - let conn = self.db.lock().await; + let db = self.db.clone(); tokio::task::spawn_blocking(move || { + let conn = db.blocking_lock(); let item_service = ItemService::new(data_path); item_service.get_item_content(&conn, id) }) @@ -53,9 +55,10 @@ impl AsyncItemService { meta: HashMap, ) -> Result { let data_path = self.data_path.clone(); - let conn = self.db.lock().await; + let db = self.db.clone(); tokio::task::spawn_blocking(move || { + let conn = db.blocking_lock(); let item_service = ItemService::new(data_path); item_service.find_item(&conn, &ids, &tags, &meta) }) @@ -69,9 +72,10 @@ impl AsyncItemService { meta: HashMap, ) -> Result, CoreError> { let data_path = self.data_path.clone(); - let conn = self.db.lock().await; + let db = self.db.clone(); tokio::task::spawn_blocking(move || { + let conn = db.blocking_lock(); let item_service = ItemService::new(data_path); item_service.list_items(&conn, &tags, &meta) }) @@ -81,9 +85,10 @@ impl AsyncItemService { pub async fn delete_item(&self, id: i64) -> Result<(), CoreError> { let data_path = self.data_path.clone(); - let mut conn = self.db.lock().await; + let db = self.db.clone(); tokio::task::spawn_blocking(move || { + let mut conn = db.blocking_lock(); let item_service = ItemService::new(data_path); item_service.delete_item(&mut conn, id) }) @@ -98,9 +103,10 @@ impl AsyncItemService { metadata: HashMap, ) -> Result { let data_path = self.data_path.clone(); - let mut conn = self.db.lock().await; + let db = self.db.clone(); tokio::task::spawn_blocking(move || { + let mut conn = db.blocking_lock(); let item_service = ItemService::new(data_path); item_service.save_item_from_mcp(&content, &tags, &metadata, &mut conn) }) diff --git a/src/modes/diff.rs b/src/modes/diff.rs index 3022c9b..d7bd803 100644 --- a/src/modes/diff.rs +++ b/src/modes/diff.rs @@ -383,7 +383,10 @@ pub fn mode_diff( log::debug!("MAIN: Done waiting on input-writer threads."); - // Now that all input has been sent and input pipes will be closed by threads exiting, + // Now that all input has been sent, the diff process will run to completion. + // We can read its output. This will block until the process is finished. + let (stdout_capture_result, stderr_capture_result) = execute_diff_command(&mut child_process)?; + // wait for the diff child process to terminate. log::debug!("MAIN: Waiting for diff child process to finish..."); let diff_status = child_process @@ -394,7 +397,6 @@ pub fn mode_diff( diff_status ); - let (stdout_capture_result, stderr_capture_result) = execute_diff_command(&mut child_process)?; handle_diff_output(diff_status, stdout_capture_result, stderr_capture_result)?; Ok(())