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,6 +17,7 @@ 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; let mut last_scan = context.last_full_folder_scan.lock().await;
if let Some(last_scan) = *last_scan { if let Some(last_scan) = *last_scan {
let elapsed_secs = time_elapsed(&last_scan).as_secs(); let elapsed_secs = time_elapsed(&last_scan).as_secs();
@@ -28,6 +29,13 @@ impl Imap {
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");
let folders = session.list_folders().await?; let folders = session.list_folders().await?;
@@ -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)
} }
} }