From 793c06c17be4cec1bdf9199c6df69bd70f145f92 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Tue, 17 Nov 2026 16:46:43 +0100 Subject: [PATCH] fix(service/sending): Address review feedback on sender supervision Signed-off-by: Erwan Leboucher --- src/service/sending/mod.rs | 4 ++-- src/service/sending/sender.rs | 27 +++++++++++---------------- src/service/sending/sender/tests.rs | 28 ++++++++++++++++------------ src/service/sending/tests.rs | 4 ++-- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index 8f971dc7a..180f8da12 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -96,7 +96,7 @@ pub type EduVec = SmallVec<[EduBuf; EDU_VEC_CAP]>; const EDU_BUF_CAP: usize = 128; const EDU_VEC_CAP: usize = 1; const WORKER_RESTART_DELAY: Duration = Duration::from_millis(2500); -const WORKER_RESTART_DELAY_MAX: Duration = Duration::from_secs(300); +const WORKER_RESTART_DELAY_MAX: Duration = Duration::from_secs(30); #[async_trait] impl crate::Service for Service { @@ -171,7 +171,7 @@ impl crate::Service for Service { failures[shard].saturating_add(1) }; let delay = restart_delay(failures[shard]); - error!(shard, ?ret, ?delay, "sender worker stopped, restarting"); + error!(shard, ?ret, ?delay, "sender worker unexpectedly finished, restarting"); restarts.push(sleep(delay).map(move |()| shard)); }, Some(shard) = restarts.next() => { diff --git a/src/service/sending/sender.rs b/src/service/sending/sender.rs index f7e1c5ed8..fe94a8ff7 100644 --- a/src/service/sending/sender.rs +++ b/src/service/sending/sender.rs @@ -151,17 +151,10 @@ impl Service { statuses: &mut CurTransactionStatus, ) { match response { - | Ok(dest) => { - if let Destination::Federation(server_name) = &dest { - self.services.federation.mark_healthy(server_name); - } - self.handle_response_ok(&dest, futures, statuses).await; - }, - // A bug on our side, not the remote's fault, and resending the same - // events would panic again: drop them and move on. - | Err((dest, e)) if e.is_panic() => { - self.handle_response_ok(&dest, futures, statuses).await; - }, + | Ok(dest) => self.handle_response_ok(&dest, futures, statuses).await, + // A bug on our side, not the remote's fault: retry with backoff + // without marking the remote unhealthy. + | Err((dest, e)) if e.is_panic() => Self::handle_response_err(dest, statuses, &e), | Err((dest, e)) => { if let Destination::Federation(dest) = &dest { if self.services.federation.should_mark_stale(&e) @@ -208,6 +201,9 @@ impl Service { ) { let _cork = self.db.db.cork(); self.db.delete_all_active_requests_for(dest).await; + if let Destination::Federation(server_name) = dest { + self.services.federation.mark_healthy(server_name); + } // Find events that have been added since starting the last request let new_events = self @@ -292,7 +288,7 @@ impl Service { /// Performs startup netburst, which sends any previously queued up but not /// yet sent requests immediately. A restarted worker always resends its - /// shard's requests in flight, without trimming them. + /// shard's requests in flight. #[tracing::instrument( name = "netburst", level = "debug", @@ -318,9 +314,8 @@ impl Service { } let entry = txns.entry(dest.clone()).or_default(); - let over_budget = !restarted - && self.server.config.startup_netburst_keep >= 0 - && entry.len() >= keep; + let over_budget = + self.server.config.startup_netburst_keep >= 0 && entry.len() >= keep; if over_budget && !self.survives_netburst_trim(&event).await { warn!( @@ -810,7 +805,7 @@ impl Service { .map(move |result| { result.unwrap_or_else(|panic| { let error = Error::from_panic(panic); - error!(dest = ?panic_dest, ?error, "Panicked while sending transaction, dropping it"); + error!(dest = ?panic_dest, ?error, "Panicked while sending transaction"); self.server .metrics .instruments diff --git a/src/service/sending/sender/tests.rs b/src/service/sending/sender/tests.rs index 02b4eb005..e5104158d 100644 --- a/src/service/sending/sender/tests.rs +++ b/src/service/sending/sender/tests.rs @@ -12,7 +12,7 @@ use crate::{ }; #[tokio::test] -async fn panicked_transaction_is_dropped_without_blaming_the_remote() { +async fn panicked_transaction_is_kept_for_retry_without_blaming_the_remote() { let (services, path, _database) = test_services().await; let sending = &services.sending; let server = server_name!("remote.example"); @@ -22,7 +22,6 @@ async fn panicked_transaction_is_dropped_without_blaming_the_remote() { sending.db.queue_requests(once((&event, &dest))); let in_flight: Vec<_> = sending.db.queued_requests(&dest).collect().await; sending.db.mark_as_active(in_flight.iter()); - let next = sending.db.queue_requests(once((&event, &dest))); let mut futures = FuturesUnordered::new(); let mut statuses = CurTransactionStatus::from([(dest.clone(), TransactionStatus::Running)]); @@ -39,17 +38,25 @@ async fn panicked_transaction_is_dropped_without_blaming_the_remote() { .await; let retry_after = sending.services.federation.retry_after(server); let sending_next = futures.len(); + let failed = matches!(statuses.get(&dest), Some(TransactionStatus::Failed(1, _))); drop(futures); drop(services); std::fs::remove_dir_all(path).unwrap(); - assert_eq!(active, next); - assert_eq!(sending_next, 1); + assert_eq!( + active, + in_flight + .into_iter() + .map(|(key, _)| key) + .collect::>() + ); + assert_eq!(sending_next, 0); + assert!(failed); assert_eq!(retry_after, None); } #[tokio::test] -async fn restarted_worker_resends_in_flight_requests_without_trimming() { +async fn restarted_worker_resends_trimmed_in_flight_requests() { let (services, path, _database) = test_services_with(json!({ "startup_netburst_keep": 1 })).await; let sending = &services.sending; @@ -68,15 +75,12 @@ async fn restarted_worker_resends_in_flight_requests_without_trimming() { sending .startup_netburst(id, true, &mut futures, &mut statuses) .await; - let after_restart = sending.db.active_requests_for(&dest).count().await; - sending - .startup_netburst(id, false, &mut futures, &mut statuses) - .await; - let after_startup = sending.db.active_requests_for(&dest).count().await; + let active = sending.db.active_requests_for(&dest).count().await; + let sending_now = futures.len(); drop(futures); drop(services); std::fs::remove_dir_all(path).unwrap(); - assert_eq!(after_restart, 3); - assert_eq!(after_startup, 1); + assert_eq!(active, 1); + assert_eq!(sending_now, 1); } diff --git a/src/service/sending/tests.rs b/src/service/sending/tests.rs index 74e41d843..40c1cb957 100644 --- a/src/service/sending/tests.rs +++ b/src/service/sending/tests.rs @@ -37,6 +37,6 @@ fn restart_delay_doubles_up_to_the_cap() { assert_eq!(restart_delay(1), Duration::from_millis(2500)); assert_eq!(restart_delay(2), Duration::from_secs(5)); assert_eq!(restart_delay(4), Duration::from_secs(20)); - assert_eq!(restart_delay(9), Duration::from_secs(300)); - assert_eq!(restart_delay(u32::MAX), Duration::from_secs(300)); + assert_eq!(restart_delay(5), Duration::from_secs(30)); + assert_eq!(restart_delay(u32::MAX), Duration::from_secs(30)); }