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.
This commit is contained in:
link2xt
2025-05-20 18:12:14 +00:00
parent 0e45c2246f
commit 89df9536e9

View File

@@ -17,16 +17,24 @@ impl Imap {
session: &mut Session, session: &mut Session,
) -> Result<bool> { ) -> Result<bool> {
// First of all, debounce to once per minute: // 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 mut last_scan = context.last_full_folder_scan.lock().await;
let elapsed_secs = time_elapsed(&last_scan).as_secs(); if let Some(last_scan) = *last_scan {
let debounce_secs = context let elapsed_secs = time_elapsed(&last_scan).as_secs();
.get_config_u64(Config::ScanAllFoldersDebounceSecs) let debounce_secs = context
.await?; .get_config_u64(Config::ScanAllFoldersDebounceSecs)
.await?;
if elapsed_secs < debounce_secs { if elapsed_secs < debounce_secs {
return Ok(false); 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"); info!(context, "Starting full folder scan");
@@ -94,7 +102,6 @@ impl Imap {
} }
info!(context, "Found folders: {folder_names:?}."); info!(context, "Found folders: {folder_names:?}.");
last_scan.replace(tools::Time::now());
Ok(true) Ok(true)
} }
} }