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

AsyncTransport send() indefinitely waits for network up #917

Open
KronsyC opened this issue Nov 19, 2023 · 1 comment
Open

AsyncTransport send() indefinitely waits for network up #917

KronsyC opened this issue Nov 19, 2023 · 1 comment

Comments

@KronsyC
Copy link

KronsyC commented Nov 19, 2023

Describe the bug
When using either the send() method of the async transport or test_connection, upon the lack of a network connection, they will indefinitely wait for the network to go up before performing work

To Reproduce

Code pseudo-excerpt (config simply contains smtp settings)

    use lettre::transport::smtp;
    let smtp_credentials = smtp::authentication::Credentials::new(config.smtp_username.clone(), config.smtp_password.clone());

    let pool_config = smtp::PoolConfig::new()
        .idle_timeout(std::time::Duration::from_secs(5));

    let tls_params = smtp::client::TlsParameters::builder(config.smtp_domain.clone())
        .build()
        .map_err(|e| EmailInitError::Transport(e))?;



    let mailer = lettre::AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(&config.smtp_domain)
        .map_err(|e| EmailInitError::Transport(e))?
        .credentials(smtp_credentials)
        .port(config.smtp_port)
        .timeout(Some(std::time::Duration::from_secs(10)))
        .tls(smtp::client::Tls::Required(tls_params))
        .pool_config(pool_config)
        .build();

    // Disable your network connection during this sleep
    tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;

    // This hangs indefinitely until reconnect
    mailer.test_connection().await.unwrap();

To solve this, you can easily wrap any email code around a tokio timeout to be extra safe, but I would have expected this to be implicit behavior of the library itself

I propose adding a config flag for this, but i'm no expert

Expected behavior

I would expect for them to raise some form of Timeout error

Environment (please complete the following information):

  • Lettre version: 0.11.1
  • OS Arch Linux 6.6.1-arch-1-1
@De-Wohli
Copy link

I'm not sure if a problem I am currently facing is linked to a similar bug, tho I've not observed any connectivity issues.
I have the two following methods:

async fn prepare_email(event: &DebouncedEvent) {
    let smtp_credentials =
        Credentials::new("smtp_creds".to_string(), "smtp_passwd".to_string());

    let mailer: AsyncSmtpTransport<Tokio1Executor> = AsyncSmtpTransport::<Tokio1Executor>::relay("smtp.server.address").unwrap()
        .credentials(smtp_credentials)
        .build();
    let from = "from";
    let to = "to";
    let subject ="subject";
    let body = "body";
    send_email_smtp(&mailer, from, to, &subject, body).await.unwrap()
}

async fn send_email_smtp(
    mailer: &AsyncSmtpTransport<Tokio1Executor>,
    from: &str,
    to: &str,
    subject: &str,
    body: String,
) -> Result<(), Box<dyn std::error::Error>> {
    let email = Message::builder()
        .from(from.parse()?)
        .to(to.parse()?)
        .subject(subject)
        .header(ContentType::TEXT_PLAIN)
        .body(body.to_string())?;
    let x = mailer.send(email).await;
    match x{
        Ok(res) => {println!("OK: {:?}",res.code().detail)}
        Err(res) => {println!("Error: {:?}",res.status().unwrap().detail)}
    }
    Ok(())
}

Using these two methods, without fail, no matter the send rate, on the 4th call, the line let x = mailer.send(email).await; hangs indefinitely. Maybe it's unrelated and there's an error on my part that I missed because I'm still new to rust if so, sorry for the confusion.

I'm running it on Windows 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants