From 89df9536e9eab08786a318b5be50afaa1a5fd190 Mon Sep 17 00:00:00 2001 From: link2xt Date: Tue, 20 May 2025 18:12:14 +0000 Subject: [PATCH] fix: reduce the scope of the last_full_folder_scan lock in scan_folders This makes it easier to ensure that holding this lock does not result in deadlocks. --- src/imap/scan_folders.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/imap/scan_folders.rs b/src/imap/scan_folders.rs index 58e9bbccf..0f1a253ab 100644 --- a/src/imap/scan_folders.rs +++ b/src/imap/scan_folders.rs @@ -17,16 +17,24 @@ impl Imap { session: &mut Session, ) -> Result { // First of all, debounce to once per minute: - let mut last_scan = context.last_full_folder_scan.lock().await; - if let Some(last_scan) = *last_scan { - let elapsed_secs = time_elapsed(&last_scan).as_secs(); - let debounce_secs = context - .get_config_u64(Config::ScanAllFoldersDebounceSecs) - .await?; + { + let mut last_scan = context.last_full_folder_scan.lock().await; + if let Some(last_scan) = *last_scan { + let elapsed_secs = time_elapsed(&last_scan).as_secs(); + let debounce_secs = context + .get_config_u64(Config::ScanAllFoldersDebounceSecs) + .await?; - if elapsed_secs < debounce_secs { - return Ok(false); + if elapsed_secs < debounce_secs { + return Ok(false); + } } + + // Update the timestamp before scanning the folders + // to avoid holding the lock for too long. + // This means next scan is delayed even if + // the current one fails. + last_scan.replace(tools::Time::now()); } info!(context, "Starting full folder scan"); @@ -94,7 +102,6 @@ impl Imap { } info!(context, "Found folders: {folder_names:?}."); - last_scan.replace(tools::Time::now()); Ok(true) } }