deltachat/
scheduler.rs

1use std::cmp;
2use std::num::NonZeroUsize;
3
4use anyhow::{Context as _, Error, Result, bail};
5use async_channel::{self as channel, Receiver, Sender};
6use futures::future::try_join_all;
7use futures_lite::FutureExt;
8use tokio::sync::{RwLock, oneshot};
9use tokio::task;
10use tokio_util::sync::CancellationToken;
11use tokio_util::task::TaskTracker;
12
13pub(crate) use self::connectivity::ConnectivityStore;
14use crate::config::Config;
15use crate::contact::{ContactId, RecentlySeenLoop};
16use crate::context::Context;
17use crate::download::{download_known_post_messages_without_pre_message, download_msgs};
18use crate::ephemeral::{self, delete_expired_imap_messages};
19use crate::events::EventType;
20use crate::imap::{FolderMeaning, Imap, session::Session};
21use crate::location;
22use crate::log::{LogExt, warn};
23use crate::smtp::{Smtp, send_smtp_messages};
24use crate::sql;
25use crate::stats::maybe_send_stats;
26use crate::tools::{self, duration_to_str, maybe_add_time_based_warnings, time, time_elapsed};
27use crate::transport::ConfiguredLoginParam;
28use crate::{constants, stats};
29
30pub(crate) mod connectivity;
31
32/// State of the IO scheduler, as stored on the [`Context`].
33///
34/// The IO scheduler can be stopped or started, but core can also pause it.  After pausing
35/// the IO scheduler will be restarted only if it was running before paused or
36/// [`Context::start_io`] was called in the meantime while it was paused.
37#[derive(Debug, Default)]
38pub(crate) struct SchedulerState {
39    inner: RwLock<InnerSchedulerState>,
40}
41
42impl SchedulerState {
43    pub(crate) fn new() -> Self {
44        Default::default()
45    }
46
47    /// Whether the scheduler is currently running.
48    pub(crate) async fn is_running(&self) -> bool {
49        let inner = self.inner.read().await;
50        matches!(*inner, InnerSchedulerState::Started(_))
51    }
52
53    /// Starts the scheduler if it is not yet started.
54    pub(crate) async fn start(&self, context: &Context) {
55        let mut inner = self.inner.write().await;
56        match *inner {
57            InnerSchedulerState::Started(_) => (),
58            InnerSchedulerState::Stopped => Self::do_start(&mut inner, context).await,
59            InnerSchedulerState::Paused {
60                ref mut started, ..
61            } => *started = true,
62        }
63        context.update_connectivities(&inner);
64    }
65
66    /// Starts the scheduler if it is not yet started.
67    async fn do_start(inner: &mut InnerSchedulerState, context: &Context) {
68        info!(context, "starting IO");
69
70        // Notify message processing loop
71        // to allow processing old messages after restart.
72        context.new_msgs_notify.notify_one();
73
74        match Scheduler::start(context).await {
75            Ok(scheduler) => {
76                *inner = InnerSchedulerState::Started(scheduler);
77                context.emit_event(EventType::ConnectivityChanged);
78            }
79            Err(err) => error!(context, "Failed to start IO: {:#}", err),
80        }
81    }
82
83    /// Stops the scheduler if it is currently running.
84    pub(crate) async fn stop(&self, context: &Context) {
85        let mut inner = self.inner.write().await;
86        match *inner {
87            InnerSchedulerState::Started(_) => {
88                Self::do_stop(&mut inner, context, InnerSchedulerState::Stopped).await
89            }
90            InnerSchedulerState::Stopped => (),
91            InnerSchedulerState::Paused {
92                ref mut started, ..
93            } => *started = false,
94        }
95        context.update_connectivities(&inner);
96    }
97
98    /// Stops the scheduler if it is currently running.
99    async fn do_stop(
100        inner: &mut InnerSchedulerState,
101        context: &Context,
102        new_state: InnerSchedulerState,
103    ) {
104        // Sending an event wakes up event pollers (get_next_event)
105        // so the caller of stop_io() can arrange for proper termination.
106        // For this, the caller needs to instruct the event poller
107        // to terminate on receiving the next event and then call stop_io()
108        // which will emit the below event(s)
109        info!(context, "stopping IO");
110
111        // Wake up message processing loop even if there are no messages
112        // to allow for clean shutdown.
113        context.new_msgs_notify.notify_one();
114
115        let debug_logging = context
116            .debug_logging
117            .write()
118            .expect("RwLock is poisoned")
119            .take();
120        if let Some(debug_logging) = debug_logging {
121            debug_logging.loop_handle.abort();
122            debug_logging.loop_handle.await.ok();
123        }
124        let prev_state = std::mem::replace(inner, new_state);
125        context.emit_event(EventType::ConnectivityChanged);
126        match prev_state {
127            InnerSchedulerState::Started(scheduler) => scheduler.stop(context).await,
128            InnerSchedulerState::Stopped | InnerSchedulerState::Paused { .. } => (),
129        }
130    }
131
132    /// Pauses the IO scheduler.
133    ///
134    /// If it is currently running the scheduler will be stopped.  When the
135    /// [`IoPausedGuard`] is dropped the scheduler is started again.
136    ///
137    /// If in the meantime [`SchedulerState::start`] or [`SchedulerState::stop`] is called
138    /// resume will do the right thing and restore the scheduler to the state requested by
139    /// the last call.
140    pub(crate) async fn pause(&'_ self, context: &Context) -> Result<IoPausedGuard> {
141        {
142            let mut inner = self.inner.write().await;
143            match *inner {
144                InnerSchedulerState::Started(_) => {
145                    let new_state = InnerSchedulerState::Paused {
146                        started: true,
147                        pause_guards_count: NonZeroUsize::MIN,
148                    };
149                    Self::do_stop(&mut inner, context, new_state).await;
150                }
151                InnerSchedulerState::Stopped => {
152                    *inner = InnerSchedulerState::Paused {
153                        started: false,
154                        pause_guards_count: NonZeroUsize::MIN,
155                    };
156                }
157                InnerSchedulerState::Paused {
158                    ref mut pause_guards_count,
159                    ..
160                } => {
161                    *pause_guards_count = pause_guards_count
162                        .checked_add(1)
163                        .ok_or_else(|| Error::msg("Too many pause guards active"))?
164                }
165            }
166            context.update_connectivities(&inner);
167        }
168
169        let (tx, rx) = oneshot::channel();
170        let context = context.clone();
171        tokio::spawn(async move {
172            rx.await.ok();
173            let mut inner = context.scheduler.inner.write().await;
174            match *inner {
175                InnerSchedulerState::Started(_) => {
176                    warn!(&context, "IoPausedGuard resume: started instead of paused");
177                }
178                InnerSchedulerState::Stopped => {
179                    warn!(&context, "IoPausedGuard resume: stopped instead of paused");
180                }
181                InnerSchedulerState::Paused {
182                    ref started,
183                    ref mut pause_guards_count,
184                } => {
185                    if *pause_guards_count == NonZeroUsize::MIN {
186                        match *started {
187                            true => SchedulerState::do_start(&mut inner, &context).await,
188                            false => *inner = InnerSchedulerState::Stopped,
189                        }
190                    } else {
191                        let new_count = pause_guards_count.get() - 1;
192                        // SAFETY: Value was >=2 before due to if condition
193                        *pause_guards_count = NonZeroUsize::new(new_count).unwrap();
194                    }
195                }
196            }
197            context.update_connectivities(&inner);
198        });
199        Ok(IoPausedGuard { sender: Some(tx) })
200    }
201
202    /// Restarts the scheduler, only if it is running.
203    pub(crate) async fn restart(&self, context: &Context) {
204        info!(context, "restarting IO");
205        if self.is_running().await {
206            self.stop(context).await;
207            self.start(context).await;
208        }
209    }
210
211    /// Indicate that the network likely has come back.
212    pub(crate) async fn maybe_network(&self) {
213        let inner = self.inner.read().await;
214        let (inboxes, oboxes) = match *inner {
215            InnerSchedulerState::Started(ref scheduler) => {
216                scheduler.maybe_network();
217                let inboxes = scheduler
218                    .inboxes
219                    .iter()
220                    .map(|b| b.conn_state.state.connectivity.clone())
221                    .collect::<Vec<_>>();
222                let oboxes = scheduler
223                    .oboxes
224                    .iter()
225                    .map(|b| b.conn_state.state.connectivity.clone())
226                    .collect::<Vec<_>>();
227                (inboxes, oboxes)
228            }
229            _ => return,
230        };
231        drop(inner);
232        connectivity::idle_interrupted(inboxes, oboxes);
233    }
234
235    /// Indicate that the network likely is lost.
236    pub(crate) async fn maybe_network_lost(&self, context: &Context) {
237        let inner = self.inner.read().await;
238        let stores = match *inner {
239            InnerSchedulerState::Started(ref scheduler) => {
240                scheduler.maybe_network_lost();
241                scheduler
242                    .boxes()
243                    .map(|b| b.conn_state.state.connectivity.clone())
244                    .collect()
245            }
246            _ => return,
247        };
248        drop(inner);
249        connectivity::maybe_network_lost(context, stores);
250    }
251
252    pub(crate) async fn interrupt_inbox(&self) {
253        let inner = self.inner.read().await;
254        if let InnerSchedulerState::Started(ref scheduler) = *inner {
255            scheduler.interrupt_inbox();
256        }
257    }
258
259    pub(crate) async fn interrupt_smtp(&self) {
260        let inner = self.inner.read().await;
261        if let InnerSchedulerState::Started(ref scheduler) = *inner {
262            scheduler.interrupt_smtp();
263        }
264    }
265
266    pub(crate) async fn interrupt_ephemeral_task(&self) {
267        let inner = self.inner.read().await;
268        if let InnerSchedulerState::Started(ref scheduler) = *inner {
269            scheduler.interrupt_ephemeral_task();
270        }
271    }
272
273    pub(crate) async fn interrupt_location(&self) {
274        let inner = self.inner.read().await;
275        if let InnerSchedulerState::Started(ref scheduler) = *inner {
276            scheduler.interrupt_location();
277        }
278    }
279
280    pub(crate) async fn interrupt_recently_seen(&self, contact_id: ContactId, timestamp: i64) {
281        let inner = self.inner.read().await;
282        if let InnerSchedulerState::Started(ref scheduler) = *inner {
283            scheduler.interrupt_recently_seen(contact_id, timestamp);
284        }
285    }
286}
287
288#[derive(Debug, Default)]
289pub(crate) enum InnerSchedulerState {
290    Started(Scheduler),
291    #[default]
292    Stopped,
293    Paused {
294        started: bool,
295        pause_guards_count: NonZeroUsize,
296    },
297}
298
299/// Guard to make sure the IO Scheduler is resumed.
300///
301/// Returned by [`SchedulerState::pause`].  To resume the IO scheduler simply drop this
302/// guard.
303#[derive(Default, Debug)]
304pub(crate) struct IoPausedGuard {
305    sender: Option<oneshot::Sender<()>>,
306}
307
308impl Drop for IoPausedGuard {
309    fn drop(&mut self) {
310        if let Some(sender) = self.sender.take() {
311            // Can only fail if receiver is dropped, but then we're already resumed.
312            sender.send(()).ok();
313        }
314    }
315}
316
317#[derive(Debug)]
318struct SchedBox {
319    /// Address at the used chatmail/email relay
320    addr: String,
321    meaning: FolderMeaning,
322    conn_state: ImapConnectionState,
323
324    /// IMAP loop task handle.
325    handle: task::JoinHandle<()>,
326}
327
328/// Job and connection scheduler.
329#[derive(Debug)]
330pub(crate) struct Scheduler {
331    /// Inboxes, one per transport.
332    inboxes: Vec<SchedBox>,
333    /// Optional boxes -- mvbox.
334    oboxes: Vec<SchedBox>,
335    smtp: SmtpConnectionState,
336    smtp_handle: task::JoinHandle<()>,
337    ephemeral_handle: task::JoinHandle<()>,
338    ephemeral_interrupt_send: Sender<()>,
339    location_handle: task::JoinHandle<()>,
340    location_interrupt_send: Sender<()>,
341
342    recently_seen_loop: RecentlySeenLoop,
343}
344
345async fn inbox_loop(
346    ctx: Context,
347    started: oneshot::Sender<()>,
348    inbox_handlers: ImapConnectionHandlers,
349) {
350    use futures::future::FutureExt;
351
352    info!(ctx, "Starting inbox loop.");
353    let ImapConnectionHandlers {
354        mut connection,
355        stop_token,
356    } = inbox_handlers;
357
358    let ctx1 = ctx.clone();
359    let fut = async move {
360        let ctx = ctx1;
361        if let Err(()) = started.send(()) {
362            warn!(ctx, "Inbox loop, missing started receiver.");
363            return;
364        };
365
366        let mut old_session: Option<Session> = None;
367        loop {
368            let session = if let Some(session) = old_session.take() {
369                session
370            } else {
371                info!(ctx, "Preparing new IMAP session for inbox.");
372                match connection.prepare(&ctx).await {
373                    Err(err) => {
374                        warn!(ctx, "Failed to prepare inbox connection: {err:#}.");
375                        continue;
376                    }
377                    Ok(session) => session,
378                }
379            };
380
381            match inbox_fetch_idle(&ctx, &mut connection, session).await {
382                Err(err) => warn!(ctx, "Failed inbox fetch_idle: {err:#}."),
383                Ok(session) => {
384                    info!(
385                        ctx,
386                        "IMAP loop iteration for inbox finished, keeping the session."
387                    );
388                    old_session = Some(session);
389                }
390            }
391        }
392    };
393
394    stop_token
395        .cancelled()
396        .map(|_| {
397            info!(ctx, "Shutting down inbox loop.");
398        })
399        .race(fut)
400        .await;
401}
402
403/// Convert folder meaning
404/// used internally by [fetch_idle] and [Context::background_fetch].
405///
406/// Returns folder configuration key and folder name
407/// if such folder is configured, `Ok(None)` otherwise.
408pub async fn convert_folder_meaning(
409    ctx: &Context,
410    folder_meaning: FolderMeaning,
411) -> Result<Option<(Config, String)>> {
412    let folder_config = match folder_meaning.to_config() {
413        Some(c) => c,
414        None => {
415            // Such folder cannot be configured,
416            // e.g. a `FolderMeaning::Spam` folder.
417            return Ok(None);
418        }
419    };
420
421    let folder = ctx
422        .get_config(folder_config)
423        .await
424        .with_context(|| format!("Failed to retrieve {folder_config} folder"))?;
425
426    if let Some(watch_folder) = folder {
427        Ok(Some((folder_config, watch_folder)))
428    } else {
429        Ok(None)
430    }
431}
432
433async fn inbox_fetch_idle(ctx: &Context, imap: &mut Imap, mut session: Session) -> Result<Session> {
434    if !ctx.get_config_bool(Config::FixIsChatmail).await? {
435        ctx.set_config_internal(
436            Config::IsChatmail,
437            crate::config::from_bool(session.is_chatmail()),
438        )
439        .await?;
440    }
441
442    // Update quota no more than once a minute.
443    if ctx.quota_needs_update(session.transport_id(), 60).await
444        && let Err(err) = ctx.update_recent_quota(&mut session).await
445    {
446        warn!(ctx, "Failed to update quota: {:#}.", err);
447    }
448
449    if let Ok(()) = imap.resync_request_receiver.try_recv()
450        && let Err(err) = session.resync_folders(ctx).await
451    {
452        warn!(ctx, "Failed to resync folders: {:#}.", err);
453        imap.resync_request_sender.try_send(()).ok();
454    }
455
456    maybe_add_time_based_warnings(ctx).await;
457
458    match ctx.get_config_i64(Config::LastHousekeeping).await {
459        Ok(last_housekeeping_time) => {
460            let next_housekeeping_time =
461                last_housekeeping_time.saturating_add(constants::HOUSEKEEPING_PERIOD);
462            if next_housekeeping_time <= time() {
463                sql::housekeeping(ctx).await.log_err(ctx).ok();
464            }
465        }
466        Err(err) => {
467            warn!(ctx, "Failed to get last housekeeping time: {}", err);
468        }
469    };
470
471    maybe_send_stats(ctx).await.log_err(ctx).ok();
472
473    session
474        .update_metadata(ctx)
475        .await
476        .context("update_metadata")?;
477    session
478        .register_token(ctx)
479        .await
480        .context("Failed to register push token")?;
481
482    let session = fetch_idle(ctx, imap, session, FolderMeaning::Inbox).await?;
483    Ok(session)
484}
485
486/// Implement a single iteration of IMAP loop.
487///
488/// This function performs all IMAP operations on a single folder, selecting it if necessary and
489/// handling all the errors. In case of an error, an error is returned and connection is dropped,
490/// otherwise connection is returned.
491async fn fetch_idle(
492    ctx: &Context,
493    connection: &mut Imap,
494    mut session: Session,
495    folder_meaning: FolderMeaning,
496) -> Result<Session> {
497    let Some((folder_config, watch_folder)) = convert_folder_meaning(ctx, folder_meaning).await?
498    else {
499        // The folder is not configured.
500        // For example, this happens if the server does not have Sent folder
501        // but watching Sent folder is enabled.
502        connection.connectivity.set_not_configured(ctx);
503        connection.idle_interrupt_receiver.recv().await.ok();
504        bail!("Cannot fetch folder {folder_meaning} because it is not configured");
505    };
506
507    if folder_config == Config::ConfiguredInboxFolder {
508        session
509            .store_seen_flags_on_imap(ctx)
510            .await
511            .context("store_seen_flags_on_imap")?;
512    }
513
514    // Fetch the watched folder.
515    connection
516        .fetch_move_delete(ctx, &mut session, &watch_folder, folder_meaning)
517        .await
518        .context("fetch_move_delete")?;
519
520    // Mark expired messages for deletion. Marked messages will be deleted from the server
521    // on the next iteration of `fetch_move_delete`. `delete_expired_imap_messages` is not
522    // called right before `fetch_move_delete` because it is not well optimized and would
523    // otherwise slow down message fetching.
524    delete_expired_imap_messages(ctx)
525        .await
526        .context("delete_expired_imap_messages")?;
527
528    download_known_post_messages_without_pre_message(ctx, &mut session).await?;
529    download_msgs(ctx, &mut session)
530        .await
531        .context("download_msgs")?;
532
533    // Synchronize Seen flags.
534    session
535        .sync_seen_flags(ctx, &watch_folder)
536        .await
537        .context("sync_seen_flags")
538        .log_err(ctx)
539        .ok();
540
541    connection.connectivity.set_idle(ctx);
542
543    ctx.emit_event(EventType::ImapInboxIdle);
544
545    if !session.can_idle() {
546        info!(
547            ctx,
548            "IMAP session does not support IDLE, going to fake idle."
549        );
550        connection.fake_idle(ctx, watch_folder).await?;
551        return Ok(session);
552    }
553
554    if ctx
555        .get_config_bool(Config::DisableIdle)
556        .await
557        .context("Failed to get disable_idle config")
558        .log_err(ctx)
559        .unwrap_or_default()
560    {
561        info!(ctx, "IMAP IDLE is disabled, going to fake idle.");
562        connection.fake_idle(ctx, watch_folder).await?;
563        return Ok(session);
564    }
565
566    info!(
567        ctx,
568        "IMAP session in folder {watch_folder:?} supports IDLE, using it."
569    );
570    let session = session
571        .idle(
572            ctx,
573            connection.idle_interrupt_receiver.clone(),
574            &watch_folder,
575        )
576        .await
577        .context("idle")?;
578
579    Ok(session)
580}
581
582/// Simplified IMAP loop to watch non-inbox folders.
583async fn simple_imap_loop(
584    ctx: Context,
585    started: oneshot::Sender<()>,
586    inbox_handlers: ImapConnectionHandlers,
587    folder_meaning: FolderMeaning,
588) {
589    use futures::future::FutureExt;
590
591    info!(ctx, "Starting simple loop for {folder_meaning}.");
592    let ImapConnectionHandlers {
593        mut connection,
594        stop_token,
595    } = inbox_handlers;
596
597    let ctx1 = ctx.clone();
598
599    let fut = async move {
600        let ctx = ctx1;
601        if let Err(()) = started.send(()) {
602            warn!(
603                ctx,
604                "Simple imap loop for {folder_meaning}, missing started receiver."
605            );
606            return;
607        }
608
609        let mut old_session: Option<Session> = None;
610        loop {
611            let session = if let Some(session) = old_session.take() {
612                session
613            } else {
614                info!(ctx, "Preparing new IMAP session for {folder_meaning}.");
615                match connection.prepare(&ctx).await {
616                    Err(err) => {
617                        warn!(
618                            ctx,
619                            "Failed to prepare {folder_meaning} connection: {err:#}."
620                        );
621                        continue;
622                    }
623                    Ok(session) => session,
624                }
625            };
626
627            match fetch_idle(&ctx, &mut connection, session, folder_meaning).await {
628                Err(err) => warn!(ctx, "Failed fetch_idle: {err:#}"),
629                Ok(session) => {
630                    info!(
631                        ctx,
632                        "IMAP loop iteration for {folder_meaning} finished, keeping the session"
633                    );
634                    old_session = Some(session);
635                }
636            }
637        }
638    };
639
640    stop_token
641        .cancelled()
642        .map(|_| {
643            info!(ctx, "Shutting down IMAP loop for {folder_meaning}.");
644        })
645        .race(fut)
646        .await;
647}
648
649async fn smtp_loop(
650    ctx: Context,
651    started: oneshot::Sender<()>,
652    smtp_handlers: SmtpConnectionHandlers,
653) {
654    use futures::future::FutureExt;
655
656    info!(ctx, "Starting SMTP loop.");
657    let SmtpConnectionHandlers {
658        mut connection,
659        stop_token,
660        idle_interrupt_receiver,
661    } = smtp_handlers;
662
663    let ctx1 = ctx.clone();
664    let fut = async move {
665        let ctx = ctx1;
666        if let Err(()) = started.send(()) {
667            warn!(&ctx, "SMTP loop, missing started receiver.");
668            return;
669        }
670
671        let mut timeout = None;
672        loop {
673            if let Err(err) = send_smtp_messages(&ctx, &mut connection).await {
674                warn!(ctx, "send_smtp_messages failed: {:#}.", err);
675                timeout = Some(timeout.unwrap_or(30));
676            } else {
677                timeout = None;
678                let duration_until_can_send = ctx.ratelimit.read().await.until_can_send();
679                if !duration_until_can_send.is_zero() {
680                    info!(
681                        ctx,
682                        "smtp got rate limited, waiting for {} until can send again",
683                        duration_to_str(duration_until_can_send)
684                    );
685                    tokio::time::sleep(duration_until_can_send).await;
686                    continue;
687                }
688            }
689
690            stats::maybe_update_message_stats(&ctx)
691                .await
692                .log_err(&ctx)
693                .ok();
694
695            // Fake Idle
696            info!(ctx, "SMTP fake idle started.");
697            match &connection.last_send_error {
698                None => connection.connectivity.set_idle(&ctx),
699                Some(err) => connection.connectivity.set_err(&ctx, err),
700            }
701
702            // If send_smtp_messages() failed, we set a timeout for the fake-idle so that
703            // sending is retried (at the latest) after the timeout. If sending fails
704            // again, we increase the timeout exponentially, in order not to do lots of
705            // unnecessary retries.
706            if let Some(t) = timeout {
707                let now = tools::Time::now();
708                info!(
709                    ctx,
710                    "SMTP has messages to retry, planning to retry {t} seconds later."
711                );
712                let duration = std::time::Duration::from_secs(t);
713                tokio::time::timeout(duration, async {
714                    idle_interrupt_receiver.recv().await.unwrap_or_default()
715                })
716                .await
717                .unwrap_or_default();
718                let slept = time_elapsed(&now).as_secs();
719                timeout = Some(cmp::max(
720                    t,
721                    slept.saturating_add(rand::random_range((slept / 2)..=slept)),
722                ));
723            } else {
724                info!(ctx, "SMTP has no messages to retry, waiting for interrupt.");
725                idle_interrupt_receiver.recv().await.unwrap_or_default();
726            };
727
728            info!(ctx, "SMTP fake idle interrupted.")
729        }
730    };
731
732    stop_token
733        .cancelled()
734        .map(|_| {
735            info!(ctx, "Shutting down SMTP loop.");
736        })
737        .race(fut)
738        .await;
739}
740
741impl Scheduler {
742    /// Start the scheduler.
743    pub async fn start(ctx: &Context) -> Result<Self> {
744        let (smtp, smtp_handlers) = SmtpConnectionState::new();
745
746        let (smtp_start_send, smtp_start_recv) = oneshot::channel();
747        let (ephemeral_interrupt_send, ephemeral_interrupt_recv) = channel::bounded(1);
748        let (location_interrupt_send, location_interrupt_recv) = channel::bounded(1);
749
750        let mut inboxes = Vec::new();
751        let mut oboxes = Vec::new();
752        let mut start_recvs = Vec::new();
753
754        for (transport_id, configured_login_param) in ConfiguredLoginParam::load_all(ctx).await? {
755            let (conn_state, inbox_handlers) =
756                ImapConnectionState::new(ctx, transport_id, configured_login_param.clone()).await?;
757            let (inbox_start_send, inbox_start_recv) = oneshot::channel();
758            let handle = {
759                let ctx = ctx.clone();
760                task::spawn(inbox_loop(ctx, inbox_start_send, inbox_handlers))
761            };
762            let addr = configured_login_param.addr.clone();
763            let inbox = SchedBox {
764                addr: addr.clone(),
765                meaning: FolderMeaning::Inbox,
766                conn_state,
767                handle,
768            };
769            inboxes.push(inbox);
770            start_recvs.push(inbox_start_recv);
771
772            if ctx.should_watch_mvbox().await? {
773                let (conn_state, handlers) =
774                    ImapConnectionState::new(ctx, transport_id, configured_login_param).await?;
775                let (start_send, start_recv) = oneshot::channel();
776                let ctx = ctx.clone();
777                let meaning = FolderMeaning::Mvbox;
778                let handle = task::spawn(simple_imap_loop(ctx, start_send, handlers, meaning));
779                oboxes.push(SchedBox {
780                    addr,
781                    meaning,
782                    conn_state,
783                    handle,
784                });
785                start_recvs.push(start_recv);
786            }
787        }
788
789        let smtp_handle = {
790            let ctx = ctx.clone();
791            task::spawn(smtp_loop(ctx, smtp_start_send, smtp_handlers))
792        };
793        start_recvs.push(smtp_start_recv);
794
795        let ephemeral_handle = {
796            let ctx = ctx.clone();
797            task::spawn(async move {
798                ephemeral::ephemeral_loop(&ctx, ephemeral_interrupt_recv).await;
799            })
800        };
801
802        let location_handle = {
803            let ctx = ctx.clone();
804            task::spawn(async move {
805                location::location_loop(&ctx, location_interrupt_recv).await;
806            })
807        };
808
809        let recently_seen_loop = RecentlySeenLoop::new(ctx.clone());
810
811        let res = Self {
812            inboxes,
813            oboxes,
814            smtp,
815            smtp_handle,
816            ephemeral_handle,
817            ephemeral_interrupt_send,
818            location_handle,
819            location_interrupt_send,
820            recently_seen_loop,
821        };
822
823        // wait for all loops to be started
824        if let Err(err) = try_join_all(start_recvs).await {
825            bail!("failed to start scheduler: {err}");
826        }
827
828        info!(ctx, "scheduler is running");
829        Ok(res)
830    }
831
832    fn boxes(&self) -> impl Iterator<Item = &SchedBox> {
833        self.inboxes.iter().chain(self.oboxes.iter())
834    }
835
836    fn maybe_network(&self) {
837        for b in self.boxes() {
838            b.conn_state.interrupt();
839        }
840        self.interrupt_smtp();
841    }
842
843    fn maybe_network_lost(&self) {
844        for b in self.boxes() {
845            b.conn_state.interrupt();
846        }
847        self.interrupt_smtp();
848    }
849
850    fn interrupt_inbox(&self) {
851        for b in &self.inboxes {
852            b.conn_state.interrupt();
853        }
854    }
855
856    fn interrupt_smtp(&self) {
857        self.smtp.interrupt();
858    }
859
860    fn interrupt_ephemeral_task(&self) {
861        self.ephemeral_interrupt_send.try_send(()).ok();
862    }
863
864    fn interrupt_location(&self) {
865        self.location_interrupt_send.try_send(()).ok();
866    }
867
868    fn interrupt_recently_seen(&self, contact_id: ContactId, timestamp: i64) {
869        self.recently_seen_loop.try_interrupt(contact_id, timestamp);
870    }
871
872    /// Halt the scheduler.
873    ///
874    /// It consumes the scheduler and never fails to stop it. In the worst case, long-running tasks
875    /// are forcefully terminated if they cannot shutdown within the timeout.
876    pub(crate) async fn stop(self, context: &Context) {
877        // Send stop signals to tasks so they can shutdown cleanly.
878        for b in self.boxes() {
879            b.conn_state.stop();
880        }
881        self.smtp.stop();
882
883        // Actually shutdown tasks.
884        let timeout_duration = std::time::Duration::from_secs(30);
885
886        let tracker = TaskTracker::new();
887        for b in self.inboxes.into_iter().chain(self.oboxes.into_iter()) {
888            let context = context.clone();
889            tracker.spawn(async move {
890                tokio::time::timeout(timeout_duration, b.handle)
891                    .await
892                    .log_err(&context)
893            });
894        }
895        {
896            let context = context.clone();
897            tracker.spawn(async move {
898                tokio::time::timeout(timeout_duration, self.smtp_handle)
899                    .await
900                    .log_err(&context)
901            });
902        }
903        tracker.close();
904        tracker.wait().await;
905
906        // Abort tasks, then await them to ensure the `Future` is dropped.
907        // Just aborting the task may keep resources such as `Context` clone
908        // moved into it indefinitely, resulting in database not being
909        // closed etc.
910        self.ephemeral_handle.abort();
911        self.ephemeral_handle.await.ok();
912        self.location_handle.abort();
913        self.location_handle.await.ok();
914        self.recently_seen_loop.abort().await;
915    }
916}
917
918/// Connection state logic shared between imap and smtp connections.
919#[derive(Debug)]
920struct ConnectionState {
921    /// Cancellation token to interrupt the whole connection.
922    stop_token: CancellationToken,
923    /// Channel to interrupt idle.
924    idle_interrupt_sender: Sender<()>,
925    /// Mutex to pass connectivity info between IMAP/SMTP threads and the API
926    connectivity: ConnectivityStore,
927}
928
929impl ConnectionState {
930    /// Shutdown this connection completely.
931    fn stop(&self) {
932        // Trigger shutdown of the run loop.
933        self.stop_token.cancel();
934    }
935
936    fn interrupt(&self) {
937        // Use try_send to avoid blocking on interrupts.
938        self.idle_interrupt_sender.try_send(()).ok();
939    }
940}
941
942#[derive(Debug)]
943pub(crate) struct SmtpConnectionState {
944    state: ConnectionState,
945}
946
947impl SmtpConnectionState {
948    fn new() -> (Self, SmtpConnectionHandlers) {
949        let stop_token = CancellationToken::new();
950        let (idle_interrupt_sender, idle_interrupt_receiver) = channel::bounded(1);
951
952        let handlers = SmtpConnectionHandlers {
953            connection: Smtp::new(),
954            stop_token: stop_token.clone(),
955            idle_interrupt_receiver,
956        };
957
958        let state = ConnectionState {
959            stop_token,
960            idle_interrupt_sender,
961            connectivity: handlers.connection.connectivity.clone(),
962        };
963
964        let conn = SmtpConnectionState { state };
965
966        (conn, handlers)
967    }
968
969    /// Interrupt any form of idle.
970    fn interrupt(&self) {
971        self.state.interrupt();
972    }
973
974    /// Shutdown this connection completely.
975    fn stop(&self) {
976        self.state.stop();
977    }
978}
979
980struct SmtpConnectionHandlers {
981    connection: Smtp,
982    stop_token: CancellationToken,
983    idle_interrupt_receiver: Receiver<()>,
984}
985
986#[derive(Debug)]
987pub(crate) struct ImapConnectionState {
988    state: ConnectionState,
989}
990
991impl ImapConnectionState {
992    /// Construct a new connection.
993    async fn new(
994        context: &Context,
995        transport_id: u32,
996        login_param: ConfiguredLoginParam,
997    ) -> Result<(Self, ImapConnectionHandlers)> {
998        let stop_token = CancellationToken::new();
999        let (idle_interrupt_sender, idle_interrupt_receiver) = channel::bounded(1);
1000
1001        let handlers = ImapConnectionHandlers {
1002            connection: Imap::new(context, transport_id, login_param, idle_interrupt_receiver)
1003                .await?,
1004            stop_token: stop_token.clone(),
1005        };
1006
1007        let state = ConnectionState {
1008            stop_token,
1009            idle_interrupt_sender,
1010            connectivity: handlers.connection.connectivity.clone(),
1011        };
1012
1013        let conn = ImapConnectionState { state };
1014
1015        Ok((conn, handlers))
1016    }
1017
1018    /// Interrupt any form of idle.
1019    fn interrupt(&self) {
1020        self.state.interrupt();
1021    }
1022
1023    /// Shutdown this connection completely.
1024    fn stop(&self) {
1025        self.state.stop();
1026    }
1027}
1028
1029#[derive(Debug)]
1030struct ImapConnectionHandlers {
1031    connection: Imap,
1032    stop_token: CancellationToken,
1033}