Skip to main content

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;
19use crate::events::EventType;
20use crate::imap::{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 = match *inner {
215            InnerSchedulerState::Started(ref scheduler) => {
216                scheduler.maybe_network();
217                scheduler
218                    .inboxes
219                    .iter()
220                    .map(|b| b.conn_state.state.connectivity.clone())
221                    .collect::<Vec<_>>()
222            }
223            _ => return,
224        };
225        drop(inner);
226        connectivity::idle_interrupted(inboxes);
227    }
228
229    /// Indicate that the network likely is lost.
230    pub(crate) async fn maybe_network_lost(&self, context: &Context) {
231        let inner = self.inner.read().await;
232        let stores = match *inner {
233            InnerSchedulerState::Started(ref scheduler) => {
234                scheduler.maybe_network_lost();
235                scheduler
236                    .boxes()
237                    .map(|b| b.conn_state.state.connectivity.clone())
238                    .collect()
239            }
240            _ => return,
241        };
242        drop(inner);
243        connectivity::maybe_network_lost(context, stores);
244    }
245
246    pub(crate) async fn interrupt_inbox(&self) {
247        let inner = self.inner.read().await;
248        if let InnerSchedulerState::Started(ref scheduler) = *inner {
249            scheduler.interrupt_inbox();
250        }
251    }
252
253    pub(crate) async fn interrupt_smtp(&self) {
254        let inner = self.inner.read().await;
255        if let InnerSchedulerState::Started(ref scheduler) = *inner {
256            scheduler.interrupt_smtp();
257        }
258    }
259
260    pub(crate) async fn interrupt_ephemeral_task(&self) {
261        let inner = self.inner.read().await;
262        if let InnerSchedulerState::Started(ref scheduler) = *inner {
263            scheduler.interrupt_ephemeral_task();
264        }
265    }
266
267    pub(crate) async fn interrupt_location(&self) {
268        let inner = self.inner.read().await;
269        if let InnerSchedulerState::Started(ref scheduler) = *inner {
270            scheduler.interrupt_location();
271        }
272    }
273
274    pub(crate) async fn interrupt_recently_seen(&self, contact_id: ContactId, timestamp: i64) {
275        let inner = self.inner.read().await;
276        if let InnerSchedulerState::Started(ref scheduler) = *inner {
277            scheduler.interrupt_recently_seen(contact_id, timestamp);
278        }
279    }
280}
281
282#[derive(Debug, Default)]
283pub(crate) enum InnerSchedulerState {
284    Started(Scheduler),
285    #[default]
286    Stopped,
287    Paused {
288        started: bool,
289        pause_guards_count: NonZeroUsize,
290    },
291}
292
293/// Guard to make sure the IO Scheduler is resumed.
294///
295/// Returned by [`SchedulerState::pause`].  To resume the IO scheduler simply drop this
296/// guard.
297#[derive(Default, Debug)]
298pub(crate) struct IoPausedGuard {
299    sender: Option<oneshot::Sender<()>>,
300}
301
302impl Drop for IoPausedGuard {
303    fn drop(&mut self) {
304        if let Some(sender) = self.sender.take() {
305            // Can only fail if receiver is dropped, but then we're already resumed.
306            sender.send(()).ok();
307        }
308    }
309}
310
311#[derive(Debug)]
312struct SchedBox {
313    /// Address at the used chatmail/email relay
314    addr: String,
315
316    /// Folder name
317    folder: String,
318
319    conn_state: ImapConnectionState,
320
321    /// IMAP loop task handle.
322    handle: task::JoinHandle<()>,
323}
324
325/// Job and connection scheduler.
326#[derive(Debug)]
327pub(crate) struct Scheduler {
328    /// Inboxes, one per transport.
329    inboxes: Vec<SchedBox>,
330    smtp: SmtpConnectionState,
331    smtp_handle: task::JoinHandle<()>,
332    ephemeral_handle: task::JoinHandle<()>,
333    ephemeral_interrupt_send: Sender<()>,
334    location_handle: task::JoinHandle<()>,
335    location_interrupt_send: Sender<()>,
336
337    recently_seen_loop: RecentlySeenLoop,
338}
339
340async fn inbox_loop(
341    ctx: Context,
342    started: oneshot::Sender<()>,
343    inbox_handlers: ImapConnectionHandlers,
344) {
345    use futures::future::FutureExt;
346
347    info!(ctx, "Starting inbox loop.");
348    let ImapConnectionHandlers {
349        mut connection,
350        stop_token,
351    } = inbox_handlers;
352
353    let transport_id = connection.transport_id();
354    let ctx1 = ctx.clone();
355    let fut = async move {
356        let ctx = ctx1;
357        if let Err(()) = started.send(()) {
358            warn!(ctx, "Inbox loop, missing started receiver.");
359            return;
360        };
361
362        let mut old_session: Option<Session> = None;
363        loop {
364            let session = if let Some(session) = old_session.take() {
365                session
366            } else {
367                info!(
368                    ctx,
369                    "Transport {transport_id}: Preparing new IMAP session for inbox."
370                );
371                match connection.prepare(&ctx).await {
372                    Err(err) => {
373                        warn!(
374                            ctx,
375                            "Transport {transport_id}: Failed to prepare inbox connection: {err:#}."
376                        );
377                        continue;
378                    }
379                    Ok(session) => {
380                        info!(
381                            ctx,
382                            "Transport {transport_id}: Prepared new IMAP session for inbox."
383                        );
384                        session
385                    }
386                }
387            };
388
389            match inbox_fetch_idle(&ctx, &mut connection, session).await {
390                Err(err) => warn!(
391                    ctx,
392                    "Transport {transport_id}: Failed inbox fetch_idle: {err:#}."
393                ),
394                Ok(session) => {
395                    old_session = Some(session);
396                }
397            }
398        }
399    };
400
401    stop_token
402        .cancelled()
403        .map(|_| {
404            info!(ctx, "Transport {transport_id}: Shutting down inbox loop.");
405        })
406        .race(fut)
407        .await;
408}
409
410async fn inbox_fetch_idle(ctx: &Context, imap: &mut Imap, mut session: Session) -> Result<Session> {
411    let transport_id = session.transport_id();
412
413    // Update quota no more than once a minute.
414    if ctx.quota_needs_update(session.transport_id(), 60).await
415        && let Err(err) = ctx.update_recent_quota(&mut session, &imap.folder).await
416    {
417        warn!(
418            ctx,
419            "Transport {transport_id}: Failed to update quota: {err:#}."
420        );
421    }
422
423    if let Ok(()) = imap.resync_request_receiver.try_recv()
424        && let Err(err) = session.resync_folders(ctx).await
425    {
426        warn!(
427            ctx,
428            "Transport {transport_id}: Failed to resync folders: {err:#}."
429        );
430        imap.resync_request_sender.try_send(()).ok();
431    }
432
433    maybe_add_time_based_warnings(ctx).await;
434
435    match ctx.get_config_i64(Config::LastHousekeeping).await {
436        Ok(last_housekeeping_time) => {
437            let next_housekeeping_time =
438                last_housekeeping_time.saturating_add(constants::HOUSEKEEPING_PERIOD);
439            if next_housekeeping_time <= time() {
440                sql::housekeeping(ctx).await.log_err(ctx).ok();
441            }
442        }
443        Err(err) => {
444            warn!(
445                ctx,
446                "Transport {transport_id}: Failed to get last housekeeping time: {err:#}"
447            );
448        }
449    };
450
451    maybe_send_stats(ctx).await.log_err(ctx).ok();
452
453    session
454        .update_metadata(ctx)
455        .await
456        .context("update_metadata")?;
457    session
458        .register_token(ctx)
459        .await
460        .context("Failed to register push token")?;
461
462    let session = fetch_idle(ctx, imap, session).await?;
463    Ok(session)
464}
465
466/// Implement a single iteration of IMAP loop.
467///
468/// This function performs all IMAP operations on a single folder, selecting it if necessary and
469/// handling all the errors. In case of an error, an error is returned and connection is dropped,
470/// otherwise connection is returned.
471async fn fetch_idle(ctx: &Context, connection: &mut Imap, mut session: Session) -> Result<Session> {
472    let transport_id = session.transport_id();
473
474    let watch_folder = connection.folder.clone();
475
476    session
477        .store_seen_flags_on_imap(ctx)
478        .await
479        .context("store_seen_flags_on_imap")?;
480
481    // Fetch the watched folder.
482    connection
483        .fetch_move_delete(ctx, &mut session, &watch_folder)
484        .await
485        .context("fetch_move_delete")?;
486
487    download_known_post_messages_without_pre_message(ctx, &mut session).await?;
488    download_msgs(ctx, &mut session)
489        .await
490        .context("download_msgs")?;
491
492    // Synchronize Seen flags.
493    session
494        .sync_seen_flags(ctx, &watch_folder)
495        .await
496        .context("sync_seen_flags")
497        .log_err(ctx)
498        .ok();
499
500    connection.connectivity.set_idle(ctx);
501
502    ctx.emit_event(EventType::ImapInboxIdle);
503
504    if !session.can_idle() {
505        info!(
506            ctx,
507            "Transport {transport_id}: IMAP session does not support IDLE, going to fake idle."
508        );
509        connection.fake_idle(ctx, &watch_folder).await?;
510        return Ok(session);
511    }
512
513    if ctx
514        .get_config_bool(Config::DisableIdle)
515        .await
516        .context("Failed to get disable_idle config")
517        .log_err(ctx)
518        .unwrap_or_default()
519    {
520        info!(
521            ctx,
522            "Transport {transport_id}: IMAP IDLE is disabled, going to fake idle."
523        );
524        connection.fake_idle(ctx, &watch_folder).await?;
525        return Ok(session);
526    }
527
528    let session = session
529        .idle(
530            ctx,
531            connection.idle_interrupt_receiver.clone(),
532            &watch_folder,
533        )
534        .await
535        .context("idle")?;
536
537    Ok(session)
538}
539
540async fn smtp_loop(
541    ctx: Context,
542    started: oneshot::Sender<()>,
543    smtp_handlers: SmtpConnectionHandlers,
544) {
545    use futures::future::FutureExt;
546
547    info!(ctx, "Starting SMTP loop.");
548    let SmtpConnectionHandlers {
549        mut connection,
550        stop_token,
551        idle_interrupt_receiver,
552    } = smtp_handlers;
553
554    let ctx1 = ctx.clone();
555    let fut = async move {
556        let ctx = ctx1;
557        if let Err(()) = started.send(()) {
558            warn!(&ctx, "SMTP loop, missing started receiver.");
559            return;
560        }
561
562        let mut timeout = None;
563        loop {
564            if let Err(err) = send_smtp_messages(&ctx, &mut connection).await {
565                warn!(ctx, "send_smtp_messages failed: {:#}.", err);
566                timeout = Some(timeout.unwrap_or(30));
567            } else {
568                timeout = None;
569                let duration_until_can_send = ctx.ratelimit.read().await.until_can_send();
570                if !duration_until_can_send.is_zero() {
571                    info!(
572                        ctx,
573                        "smtp got rate limited, waiting for {} until can send again",
574                        duration_to_str(duration_until_can_send)
575                    );
576                    tokio::time::sleep(duration_until_can_send).await;
577                    continue;
578                }
579            }
580
581            stats::maybe_update_message_stats(&ctx)
582                .await
583                .log_err(&ctx)
584                .ok();
585
586            // Fake Idle
587            info!(ctx, "SMTP fake idle started.");
588            match &connection.last_send_error {
589                None => connection.connectivity.set_idle(&ctx),
590                Some(err) => connection.connectivity.set_err(&ctx, err.clone()),
591            }
592
593            // If send_smtp_messages() failed, we set a timeout for the fake-idle so that
594            // sending is retried (at the latest) after the timeout. If sending fails
595            // again, we increase the timeout exponentially, in order not to do lots of
596            // unnecessary retries.
597            if let Some(t) = timeout {
598                let now = tools::Time::now();
599                info!(
600                    ctx,
601                    "SMTP has messages to retry, planning to retry {t} seconds later."
602                );
603                let duration = std::time::Duration::from_secs(t);
604                tokio::time::timeout(duration, async {
605                    idle_interrupt_receiver.recv().await.unwrap_or_default()
606                })
607                .await
608                .unwrap_or_default();
609                let slept = time_elapsed(&now).as_secs();
610                timeout = Some(cmp::max(
611                    t,
612                    slept.saturating_add(rand::random_range((slept / 2)..=slept)),
613                ));
614            } else {
615                info!(ctx, "SMTP has no messages to retry, waiting for interrupt.");
616                idle_interrupt_receiver.recv().await.unwrap_or_default();
617            };
618
619            info!(ctx, "SMTP fake idle interrupted.")
620        }
621    };
622
623    stop_token
624        .cancelled()
625        .map(|_| {
626            info!(ctx, "Shutting down SMTP loop.");
627        })
628        .race(fut)
629        .await;
630}
631
632impl Scheduler {
633    /// Start the scheduler.
634    pub async fn start(ctx: &Context) -> Result<Self> {
635        let (smtp, smtp_handlers) = SmtpConnectionState::new();
636
637        let (smtp_start_send, smtp_start_recv) = oneshot::channel();
638        let (ephemeral_interrupt_send, ephemeral_interrupt_recv) = channel::bounded(1);
639        let (location_interrupt_send, location_interrupt_recv) = channel::bounded(1);
640
641        let mut inboxes = Vec::new();
642        let mut start_recvs = Vec::new();
643
644        for (transport_id, configured_login_param) in ConfiguredLoginParam::load_all(ctx).await? {
645            let (conn_state, inbox_handlers) =
646                ImapConnectionState::new(ctx, transport_id, configured_login_param.clone()).await?;
647            let (inbox_start_send, inbox_start_recv) = oneshot::channel();
648            let handle = {
649                let ctx = ctx.clone();
650                task::spawn(inbox_loop(ctx, inbox_start_send, inbox_handlers))
651            };
652            let addr = configured_login_param.addr.clone();
653            let folder = configured_login_param
654                .imap_folder
655                .unwrap_or_else(|| "INBOX".to_string());
656            let inbox = SchedBox {
657                addr: addr.clone(),
658                folder,
659                conn_state,
660                handle,
661            };
662            inboxes.push(inbox);
663            start_recvs.push(inbox_start_recv);
664        }
665
666        let smtp_handle = {
667            let ctx = ctx.clone();
668            task::spawn(smtp_loop(ctx, smtp_start_send, smtp_handlers))
669        };
670        start_recvs.push(smtp_start_recv);
671
672        let ephemeral_handle = {
673            let ctx = ctx.clone();
674            task::spawn(async move {
675                ephemeral::ephemeral_loop(&ctx, ephemeral_interrupt_recv).await;
676            })
677        };
678
679        let location_handle = {
680            let ctx = ctx.clone();
681            task::spawn(async move {
682                location::location_loop(&ctx, location_interrupt_recv).await;
683            })
684        };
685
686        let recently_seen_loop = RecentlySeenLoop::new(ctx.clone());
687
688        let res = Self {
689            inboxes,
690            smtp,
691            smtp_handle,
692            ephemeral_handle,
693            ephemeral_interrupt_send,
694            location_handle,
695            location_interrupt_send,
696            recently_seen_loop,
697        };
698
699        // wait for all loops to be started
700        if let Err(err) = try_join_all(start_recvs).await {
701            bail!("failed to start scheduler: {err}");
702        }
703
704        info!(ctx, "scheduler is running");
705        Ok(res)
706    }
707
708    fn boxes(&self) -> impl Iterator<Item = &SchedBox> {
709        self.inboxes.iter()
710    }
711
712    fn maybe_network(&self) {
713        for b in self.boxes() {
714            b.conn_state.interrupt();
715        }
716        self.interrupt_smtp();
717    }
718
719    fn maybe_network_lost(&self) {
720        for b in self.boxes() {
721            b.conn_state.interrupt();
722        }
723        self.interrupt_smtp();
724    }
725
726    fn interrupt_inbox(&self) {
727        for b in &self.inboxes {
728            b.conn_state.interrupt();
729        }
730    }
731
732    fn interrupt_smtp(&self) {
733        self.smtp.interrupt();
734    }
735
736    fn interrupt_ephemeral_task(&self) {
737        self.ephemeral_interrupt_send.try_send(()).ok();
738    }
739
740    fn interrupt_location(&self) {
741        self.location_interrupt_send.try_send(()).ok();
742    }
743
744    fn interrupt_recently_seen(&self, contact_id: ContactId, timestamp: i64) {
745        self.recently_seen_loop.try_interrupt(contact_id, timestamp);
746    }
747
748    /// Halt the scheduler.
749    ///
750    /// It consumes the scheduler and never fails to stop it. In the worst case, long-running tasks
751    /// are forcefully terminated if they cannot shutdown within the timeout.
752    pub(crate) async fn stop(self, context: &Context) {
753        // Send stop signals to tasks so they can shutdown cleanly.
754        for b in self.boxes() {
755            b.conn_state.stop();
756        }
757        self.smtp.stop();
758
759        // Actually shutdown tasks.
760        let timeout_duration = std::time::Duration::from_secs(30);
761
762        let tracker = TaskTracker::new();
763        for b in self.inboxes {
764            let context = context.clone();
765            tracker.spawn(async move {
766                tokio::time::timeout(timeout_duration, b.handle)
767                    .await
768                    .log_err(&context)
769            });
770        }
771        {
772            let context = context.clone();
773            tracker.spawn(async move {
774                tokio::time::timeout(timeout_duration, self.smtp_handle)
775                    .await
776                    .log_err(&context)
777            });
778        }
779        tracker.close();
780        tracker.wait().await;
781
782        // Abort tasks, then await them to ensure the `Future` is dropped.
783        // Just aborting the task may keep resources such as `Context` clone
784        // moved into it indefinitely, resulting in database not being
785        // closed etc.
786        self.ephemeral_handle.abort();
787        self.ephemeral_handle.await.ok();
788        self.location_handle.abort();
789        self.location_handle.await.ok();
790        self.recently_seen_loop.abort().await;
791    }
792}
793
794/// Connection state logic shared between imap and smtp connections.
795#[derive(Debug)]
796struct ConnectionState {
797    /// Cancellation token to interrupt the whole connection.
798    stop_token: CancellationToken,
799    /// Channel to interrupt idle.
800    idle_interrupt_sender: Sender<()>,
801    /// Mutex to pass connectivity info between IMAP/SMTP threads and the API
802    connectivity: ConnectivityStore,
803}
804
805impl ConnectionState {
806    /// Shutdown this connection completely.
807    fn stop(&self) {
808        // Trigger shutdown of the run loop.
809        self.stop_token.cancel();
810    }
811
812    fn interrupt(&self) {
813        // Use try_send to avoid blocking on interrupts.
814        self.idle_interrupt_sender.try_send(()).ok();
815    }
816}
817
818#[derive(Debug)]
819pub(crate) struct SmtpConnectionState {
820    state: ConnectionState,
821}
822
823impl SmtpConnectionState {
824    fn new() -> (Self, SmtpConnectionHandlers) {
825        let stop_token = CancellationToken::new();
826        let (idle_interrupt_sender, idle_interrupt_receiver) = channel::bounded(1);
827
828        let handlers = SmtpConnectionHandlers {
829            connection: Smtp::new(),
830            stop_token: stop_token.clone(),
831            idle_interrupt_receiver,
832        };
833
834        let state = ConnectionState {
835            stop_token,
836            idle_interrupt_sender,
837            connectivity: handlers.connection.connectivity.clone(),
838        };
839
840        let conn = SmtpConnectionState { state };
841
842        (conn, handlers)
843    }
844
845    /// Interrupt any form of idle.
846    fn interrupt(&self) {
847        self.state.interrupt();
848    }
849
850    /// Shutdown this connection completely.
851    fn stop(&self) {
852        self.state.stop();
853    }
854}
855
856struct SmtpConnectionHandlers {
857    connection: Smtp,
858    stop_token: CancellationToken,
859    idle_interrupt_receiver: Receiver<()>,
860}
861
862#[derive(Debug)]
863pub(crate) struct ImapConnectionState {
864    state: ConnectionState,
865}
866
867impl ImapConnectionState {
868    /// Construct a new connection.
869    async fn new(
870        context: &Context,
871        transport_id: u32,
872        login_param: ConfiguredLoginParam,
873    ) -> Result<(Self, ImapConnectionHandlers)> {
874        let stop_token = CancellationToken::new();
875        let (idle_interrupt_sender, idle_interrupt_receiver) = channel::bounded(1);
876
877        let handlers = ImapConnectionHandlers {
878            connection: Imap::new(context, transport_id, login_param, idle_interrupt_receiver)
879                .await?,
880            stop_token: stop_token.clone(),
881        };
882
883        let state = ConnectionState {
884            stop_token,
885            idle_interrupt_sender,
886            connectivity: handlers.connection.connectivity.clone(),
887        };
888
889        let conn = ImapConnectionState { state };
890
891        Ok((conn, handlers))
892    }
893
894    /// Interrupt any form of idle.
895    fn interrupt(&self) {
896        self.state.interrupt();
897    }
898
899    /// Shutdown this connection completely.
900    fn stop(&self) {
901        self.state.stop();
902    }
903}
904
905#[derive(Debug)]
906struct ImapConnectionHandlers {
907    connection: Imap,
908    stop_token: CancellationToken,
909}