Skip to content

Commit 89df953

Browse files
committed
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.
1 parent 0e45c22 commit 89df953

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/imap/scan_folders.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ impl Imap {
1717
session: &mut Session,
1818
) -> Result<bool> {
1919
// First of all, debounce to once per minute:
20-
let mut last_scan = context.last_full_folder_scan.lock().await;
21-
if let Some(last_scan) = *last_scan {
22-
let elapsed_secs = time_elapsed(&last_scan).as_secs();
23-
let debounce_secs = context
24-
.get_config_u64(Config::ScanAllFoldersDebounceSecs)
25-
.await?;
20+
{
21+
let mut last_scan = context.last_full_folder_scan.lock().await;
22+
if let Some(last_scan) = *last_scan {
23+
let elapsed_secs = time_elapsed(&last_scan).as_secs();
24+
let debounce_secs = context
25+
.get_config_u64(Config::ScanAllFoldersDebounceSecs)
26+
.await?;
2627

27-
if elapsed_secs < debounce_secs {
28-
return Ok(false);
28+
if elapsed_secs < debounce_secs {
29+
return Ok(false);
30+
}
2931
}
32+
33+
// Update the timestamp before scanning the folders
34+
// to avoid holding the lock for too long.
35+
// This means next scan is delayed even if
36+
// the current one fails.
37+
last_scan.replace(tools::Time::now());
3038
}
3139
info!(context, "Starting full folder scan");
3240

@@ -94,7 +102,6 @@ impl Imap {
94102
}
95103

96104
info!(context, "Found folders: {folder_names:?}.");
97-
last_scan.replace(tools::Time::now());
98105
Ok(true)
99106
}
100107
}

0 commit comments

Comments
 (0)