From 4ee65a049fe7fbe6d0e68e3c49634716ef9904d6 Mon Sep 17 00:00:00 2001 From: link2xt Date: Fri, 1 Nov 2024 02:09:29 +0000 Subject: [PATCH] fix: always exit fake IDLE after at most 60 seconds Do not call `fetch_new_messages`, always exit and let the IMAP loop prepare the connection properly and run all pending tasks. --- src/imap/idle.rs | 30 ++++-------------------------- src/scheduler.rs | 8 ++------ 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/src/imap/idle.rs b/src/imap/idle.rs index f53a1d1cc..3680b0c9a 100644 --- a/src/imap/idle.rs +++ b/src/imap/idle.rs @@ -9,7 +9,6 @@ use tokio::time::timeout; use super::session::Session; use super::Imap; use crate::context::Context; -use crate::imap::FolderMeaning; use crate::net::TIMEOUT; use crate::tools::{self, time_elapsed}; @@ -109,37 +108,16 @@ impl Imap { pub(crate) async fn fake_idle( &mut self, context: &Context, - session: &mut Session, watch_folder: String, - folder_meaning: FolderMeaning, ) -> Result<()> { let fake_idle_start_time = tools::Time::now(); info!(context, "IMAP-fake-IDLEing folder={:?}", watch_folder); - // Loop until we are interrupted or until we fetch something. - loop { - match timeout(Duration::from_secs(60), self.idle_interrupt_receiver.recv()).await { - Err(_) => { - // Let's see if fetching messages results - // in anything. If so, we behave as if IDLE had data but - // will have already fetched the messages so perform_*_fetch - // will not find any new. - let res = self - .fetch_new_messages(context, session, &watch_folder, folder_meaning, false) - .await?; - - info!(context, "fetch_new_messages returned {:?}", res); - - if res { - break; - } - } - Ok(_) => { - info!(context, "Fake IDLE interrupted."); - break; - } - } + // Wait for 60 seconds or until we are interrupted. + match timeout(Duration::from_secs(60), self.idle_interrupt_receiver.recv()).await { + Err(_) => info!(context, "Fake IDLE finished."), + Ok(_) => info!(context, "Fake IDLE interrupted."), } info!( diff --git a/src/scheduler.rs b/src/scheduler.rs index a901c7c97..fd987c0f0 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -655,9 +655,7 @@ async fn fetch_idle( ctx, "IMAP session does not support IDLE, going to fake idle." ); - connection - .fake_idle(ctx, &mut session, watch_folder, folder_meaning) - .await?; + connection.fake_idle(ctx, watch_folder).await?; return Ok(session); } @@ -669,9 +667,7 @@ async fn fetch_idle( .unwrap_or_default() { info!(ctx, "IMAP IDLE is disabled, going to fake idle."); - connection - .fake_idle(ctx, &mut session, watch_folder, folder_meaning) - .await?; + connection.fake_idle(ctx, watch_folder).await?; return Ok(session); }