forge
Explore
Log in

Don't you dare go Hollow

A refused batch writes each row's own retry clock: the envelope is shared but the ladder is not, so a fresh row batched with an exhausted one is set down to try again instead of being written off on its first failure, and a spent row is abandoned instead of riding a mate's remaining attempts; a store-level test pins the mixed-ladder batch, and a debug eprintln left in the activity wiring goes (with two formatter reflows of refusal strings riding along)

Browse files at this commit

5295ebec3f6c07aec2dd4608d7d0f2c64173a6d7Ahmet Kaan GümüşParent: c7f38ef
crates/iz-core/src/mail.rsmodified+18 -13
@@ -30,9 +30,8 @@ use time::{Duration, OffsetDateTime};
3030 use crate::board::Transition;
3131 use crate::detail::ActivityKind;
3232 use crate::store::{
33- ActivityEvent, Audience, ClaimedSend, Event, Freeing, MailOutcome, MailRule, MailSend, SendKind,
34- Store,
35- Trigger,
33+ ActivityEvent, Audience, ClaimedSend, Event, Freeing, MailOutcome, MailRule, MailSend,
34+ SendKind, Store, Trigger,
3635 };
3736
3837 /// Why the mail server would not take it, and whether that can change.
@@ -344,8 +343,7 @@ impl Engine {
344343 self.owe(rule, &event, task_id, &mut report).await?;
345344 }
346345 Trigger::StatusBecomes(watched) => {
347- let detail =
348- format!("unblocked:{}", watched.as_deref().unwrap_or("any"));
346+ let detail = format!("unblocked:{}", watched.as_deref().unwrap_or("any"));
349347 self.store
350348 .record_mail_decision(
351349 &rule.id,
@@ -739,9 +737,9 @@ impl Engine {
739737 let Some(mail) = mail else {
740738 return Ok(());
741739 };
742- let Some(send) = sends.first() else {
740+ if sends.is_empty() {
743741 return Ok(());
744- };
742+ }
745743 let now = OffsetDateTime::now_utc();
746744 match self.mailer.send(&mail).await {
747745 Ok(()) => {
@@ -768,17 +766,24 @@ impl Engine {
768766 report.held += 1;
769767 }
770768 Err(problem) => {
771- let attempts = send.attempts + 1;
772- // The last attempt is written down as given up on rather than
773- // left looking like it is still coming.
774- let retry_at =
775- (problem.retryable && attempts < MAX_ATTEMPTS).then(|| now + backoff(attempts));
769+ // The retry ladder is each row's own: a batch is one envelope,
770+ // but the rows in it arrived at different times and may be at
771+ // different rungs. Deciding from the first row would write a
772+ // fresh row off on its first failure, or keep retrying a row
773+ // whose five attempts are spent.
774+ let mut any_retry = false;
776775 for one in sends {
776+ let attempts = one.attempts + 1;
777+ // The last attempt is written down as given up on rather
778+ // than left looking like it is still coming.
779+ let retry_at = (problem.retryable && attempts < MAX_ATTEMPTS)
780+ .then(|| now + backoff(attempts));
781+ any_retry |= retry_at.is_some();
777782 self.store
778783 .record_send_refused(&one.id, &problem.message, retry_at, now)
779784 .await?;
780785 }
781- if retry_at.is_some() {
786+ if any_retry {
782787 report.failed += 1;
783788 } else {
784789 report.abandoned += 1;
crates/iz-core/tests/store.rsmodified+69 -0
@@ -3461,6 +3461,75 @@ async fn a_failing_send_is_retried_a_bounded_number_of_times_and_then_written_of
34613461 let _ = std::fs::remove_dir_all(&dir);
34623462 }
34633463
3464+#[tokio::test]
3465+async fn a_batch_mates_retry_clock_is_its_own() {
3466+ // Two owed rows, same card and reader, at different rungs of the retry
3467+ // ladder, answered by one envelope: the refusal writes the exhausted row
3468+ // off and sets the fresh one down to try again — the envelope is shared,
3469+ // the clocks are not.
3470+ let (dir, store, workspace, admin) = waiting(30).await;
3471+ let mate = member(&store, &workspace, "emre@iz.sh", "Emre").await;
3472+ let task = add_task(&store, &workspace, "Backlog", "Ship it", None, &admin).await;
3473+ store.assign_task(&task, &mate).await.unwrap();
3474+ let rule = a_rule(&store, &workspace, "Done", "Task completed").await;
3475+
3476+ let mailer = Remembering::refusing(
3477+ (0..iz_core::mail::MAX_ATTEMPTS)
3478+ .map(|_| MailError::retryable("connection timed out"))
3479+ .collect(),
3480+ );
3481+ let engine = Engine::new(store.clone(), mailer.clone(), "https://iz.sh");
3482+
3483+ // Row A: owed by the first crossing, refused on four delivery passes, one
3484+ // rung from written off.
3485+ let first = moved_to(&store, &workspace, &task, "Backlog", "Done", &admin).await;
3486+ engine.on_transition(&first).await.unwrap();
3487+ let mut when = OffsetDateTime::now_utc() + Duration::minutes(31);
3488+ for _ in 1..iz_core::mail::MAX_ATTEMPTS {
3489+ engine.deliver_owed(when, 10).await.unwrap();
3490+ when += backoff(iz_core::mail::MAX_ATTEMPTS) + Duration::minutes(1);
3491+ }
3492+
3493+ // Row B: a second crossing owes the same reader again, fresh on the
3494+ // ladder, held by the same quiet window.
3495+ moved_to(&store, &workspace, &task, "Done", "Backlog", &admin).await;
3496+ let second = moved_to(&store, &workspace, &task, "Backlog", "Done", &admin).await;
3497+ engine.on_transition(&second).await.unwrap();
3498+
3499+ // One envelope answers both rows and is refused: A's fifth attempt, B's
3500+ // first.
3501+ let due = OffsetDateTime::now_utc() + Duration::minutes(31);
3502+ let when = when.max(due);
3503+ engine.deliver_owed(when, 10).await.unwrap();
3504+
3505+ let rows = store.sends_for_rule(&rule.id, 10).await.unwrap();
3506+ assert_eq!(rows.len(), 2);
3507+ let exhausted = rows
3508+ .iter()
3509+ .find(|row| row.attempts == iz_core::mail::MAX_ATTEMPTS);
3510+ let fresh = rows.iter().find(|row| row.attempts == 1);
3511+ let Some(exhausted) = exhausted else {
3512+ panic!("no row at the top of the ladder: {rows:?}");
3513+ };
3514+ let Some(fresh) = fresh else {
3515+ panic!("the fresh row was not marked with its own first attempt: {rows:?}");
3516+ };
3517+ assert_eq!(exhausted.state, SendState::Abandoned);
3518+ assert_eq!(fresh.state, SendState::Failed);
3519+ assert!(
3520+ fresh.next_attempt_at.is_some(),
3521+ "the fresh row rides its own retry clock: {fresh:?}"
3522+ );
3523+
3524+ // And when the host answers again, the fresh row still goes out.
3525+ let later = fresh.next_attempt_at.unwrap() + Duration::minutes(1);
3526+ engine.deliver_owed(later, 10).await.unwrap();
3527+ let sent = mailer.sent();
3528+ assert_eq!(sent.len(), 1);
3529+ assert_eq!(sent[0].to, "emre@iz.sh");
3530+ let _ = std::fs::remove_dir_all(&dir);
3531+}
3532+
34643533 #[tokio::test]
34653534 async fn a_refused_address_is_written_off_at_the_first_answer() {
34663535 let (dir, store, workspace, admin) = shared().await;
crates/iz-web/src/server.rsmodified+2 -5
@@ -105,7 +105,6 @@ impl Mail {
105105 let ev = store.event(&activity_id).await;
106106 let report = match ev {
107107 Ok(Some(iz_core::store::Event::Happened(ev))) => {
108- eprintln!("AFTER_ACTIVITY happened kind={:?}", ev.kind);
109108 match engine.on_activity(&ev).await {
110109 Ok(_) => {
111110 engine
@@ -395,8 +394,7 @@ impl Refusal {
395394 Refusal::EmptyTitle => "Give the task a title.".to_string(),
396395 Refusal::EmptyName => "Give yourself a name.".to_string(),
397396 Refusal::BadLimit => {
398- "A limit has to be at least 1 MB, and no wider than 500 MB per file."
399- .to_string()
397+ "A limit has to be at least 1 MB, and no wider than 500 MB per file.".to_string()
400398 }
401399 Refusal::BadPolicy => "A security number is out of range.".to_string(),
402400 Refusal::BadZone => "That is not a timezone.".to_string(),
@@ -463,8 +461,7 @@ impl Refusal {
463461 Refusal::AlreadyMember => "Zaten üye.".to_string(),
464462 Refusal::Unavailable => "Bir şeyler ters gitti.".to_string(),
465463 Refusal::BadLimit => {
466- "Limit en az 1 MB, dosya başına en çok 500 MB olabilir."
467- .to_string()
464+ "Limit en az 1 MB, dosya başına en çok 500 MB olabilir.".to_string()
468465 }
469466 Refusal::BadPolicy => "Güvenlik sayısı aralığın dışında.".to_string(),
470467 Refusal::BadZone => "Bu bir saat dilimi değil.".to_string(),