Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: do not cancel the task returned from async_imap Handle.wait_with_timeout #6526

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions src/imap/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::time::Duration;
use anyhow::{Context as _, Result};
use async_channel::Receiver;
use async_imap::extensions::idle::IdleResponse;
use futures_lite::FutureExt;
use tokio::time::timeout;

use super::session::Session;
Expand All @@ -27,8 +26,6 @@ impl Session {
idle_interrupt_receiver: Receiver<()>,
folder: &str,
) -> Result<Self> {
use futures::future::FutureExt;

let create = true;
self.select_with_uidvalidity(context, folder, create)
.await?;
Expand Down Expand Up @@ -63,42 +60,46 @@ impl Session {
handle.as_mut().set_read_timeout(None);
let (idle_wait, interrupt) = handle.wait_with_timeout(IDLE_TIMEOUT);

enum Event {
IdleResponse(IdleResponse),
Interrupt,
}

info!(
context,
"IDLE entering wait-on-remote state in folder {folder:?}."
);
let fut = idle_wait.map(|ev| ev.map(Event::IdleResponse)).race(async {
idle_interrupt_receiver.recv().await.ok();

// cancel imap idle connection properly
drop(interrupt);
// Spawn a task to relay interrupts from `idle_interrupt_receiver`
// into interruptions of IDLE.
let interrupt_relay = {
let context = context.clone();
let folder = folder.to_string();

tokio::spawn(async move {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note that we wouldn't need tokio::spawn() here, tokio::join!()ing the two futures would suffice. But the current code has the advantage that it's only using simple concepts, whereas tokio::join!() is somewhat of a harder to grasp concept, so, I'm not actually proposing to change anything.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how I can cancel the second "relay" future once I have put it into join already.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something with join set is probably possible, but the task is good enough.

idle_interrupt_receiver.recv().await.ok();

Ok(Event::Interrupt)
});
info!(context, "{folder:?}: Received interrupt, stopping IDLE.");

match fut.await {
Ok(Event::IdleResponse(IdleResponse::NewData(x))) => {
// Drop `interrupt` in order to stop the IMAP IDLE.
drop(interrupt);
})
};

match idle_wait.await {
Ok(IdleResponse::NewData(x)) => {
info!(context, "{folder:?}: Idle has NewData {x:?}");
}
Ok(Event::IdleResponse(IdleResponse::Timeout)) => {
Ok(IdleResponse::Timeout) => {
info!(context, "{folder:?}: Idle-wait timeout or interruption.");
}
Ok(Event::IdleResponse(IdleResponse::ManualInterrupt)) => {
Ok(IdleResponse::ManualInterrupt) => {
info!(context, "{folder:?}: Idle wait was interrupted manually.");
}
Ok(Event::Interrupt) => {
info!(context, "{folder:?}: Idle wait was interrupted.");
}
Err(err) => {
warn!(context, "{folder:?}: Idle wait errored: {err:?}.");
}
}

// Abort the task, then await to ensure the future is dropped.
interrupt_relay.abort();
interrupt_relay.await.ok();

let mut session = tokio::time::timeout(Duration::from_secs(15), handle.done())
.await
.with_context(|| format!("{folder}: IMAP IDLE protocol timed out"))?
Expand Down
Loading