fix: emit SmtpMessageSent event after deleting the message from SMTP queue

This is mostly needed to fix flaky
tests/test_something.py::test_is_sending_finished
by making sure that is_sending_finished() return false
once we got an event that message was sent.
This commit is contained in:
link2xt
2026-09-16 20:22:20 +00:00
committed by l
parent 349c9650e3
commit 78caa6f53c
2 changed files with 13 additions and 13 deletions

View File

@@ -467,16 +467,16 @@ pub(crate) async fn send_msg_to_smtp(
let split_index = usize::try_from(chunk_size.min(unsent_len))
.context("Failed to convert SMTP chunk size")?;
let (chunk, rest) = unsent.split_at(split_index);
let status = smtp_send(context, chunk, body.as_str(), smtp, Some(msg_id)).await;
if !matches!(status, SendResult::Success) || rest.is_empty() {
break status;
}
for sent_to_addr in chunk {
let sent_to_addr_string = sent_to_addr.to_string();
if !sent_to_set.insert(sent_to_addr_string) {
error!(context, "Attempted to send to {sent_to_addr} twice.");
}
}
let status = smtp_send(context, chunk, body.as_str(), smtp, Some(msg_id)).await;
if !matches!(status, SendResult::Success) || rest.is_empty() {
break status;
}
let sent_to_str = sent_to_set
.iter()
.map(|a| a.as_ref())
@@ -499,6 +499,15 @@ pub(crate) async fn send_msg_to_smtp(
.sql
.execute("DELETE FROM smtp2 WHERE id=?", (rowid,))
.await?;
let sent_to_len = sent_to_set.len();
debug_assert!(sent_to_len > 0);
let info_msg = format!(
"Message len={} was SMTP-sent to {sent_to_len} recipients.",
body.len()
);
info!(context, "{info_msg}.");
context.emit_event(EventType::SmtpMessageSent(info_msg));
}
SendResult::Failure(ref err) => {
if err

View File

@@ -5,7 +5,6 @@ use async_smtp::{EmailAddress, Envelope, SendableEmail};
use super::Smtp;
use crate::config::Config;
use crate::context::Context;
use crate::events::EventType;
use crate::log::warn;
use crate::tools;
@@ -39,8 +38,6 @@ impl Smtp {
context.ratelimit.write().await.send();
}
let message_len_bytes = message.len();
let envelope =
Envelope::new(self.from.clone(), recipients.to_vec()).map_err(Error::Envelope)?;
let mail = SendableEmail::new(envelope, message);
@@ -55,12 +52,6 @@ impl Smtp {
transport.send(mail).await.map_err(Error::SmtpSend)?;
let info_msg = format!(
"Message len={message_len_bytes} was SMTP-sent to {} recipients.",
recipients.len()
);
info!(context, "{info_msg}.");
context.emit_event(EventType::SmtpMessageSent(info_msg));
self.last_success = Some(tools::Time::now());
Ok(())
}