From 5a9094359c8d54ec7841ad99fe7bdc9aa658f28a Mon Sep 17 00:00:00 2001 From: David Torcivia Date: Wed, 5 Aug 2026 12:23:10 -0400 Subject: [PATCH 1/5] recovery: floor loss delay at observed ack latency Acknowledgements are processed in batches and the RTT estimate only samples the freshest packet of each batch. On a path whose RTT is close to or below the granularity of ack processing, the time and packet thresholds declare packets lost whose acknowledgements have not had a chance to be processed yet. Each spurious declaration triggers a retransmission and a congestion event for a packet that was delivered. This adds a config option, off by default, that floors the loss delay at the slowest send-to-processed-ack loop observed in the last 512ms, and applies the packet reordering threshold only to packets older than that loop. Both recovery implementations are covered. Measured on a 10GbE path with 0.2-0.6ms RTT, six connections carrying 1GB with CUBIC: declared losses fell from 25-30k per run to 1.3-2.3k, acks that disproved a declaration fell from 8-10k to 100-200, and goodput rose from 7.9 to 8.8 Gbit/s median over five runs. (cherry picked from commit 0d2f3542c1edddb1c957ddb6919dd6c61e5acaf5) Signed-off-by: David Torcivia --- quiche/src/lib.rs | 19 ++ quiche/src/recovery/congestion/recovery.rs | 52 ++++- quiche/src/recovery/gcongestion/recovery.rs | 48 ++++- quiche/src/recovery/mod.rs | 205 ++++++++++++++++++++ 4 files changed, 316 insertions(+), 8 deletions(-) diff --git a/quiche/src/lib.rs b/quiche/src/lib.rs index 4b5ffaef5b1..9ffa3ee9a38 100644 --- a/quiche/src/lib.rs +++ b/quiche/src/lib.rs @@ -576,6 +576,7 @@ pub struct Config { enable_relaxed_loss_threshold: bool, enable_cubic_idle_restart_fix: bool, enable_send_streams_blocked: bool, + enable_ack_latency_loss_floor: bool, pmtud: bool, pmtud_max_probes: u8, @@ -658,6 +659,7 @@ impl Config { enable_relaxed_loss_threshold: false, enable_cubic_idle_restart_fix: true, enable_send_streams_blocked: false, + enable_ack_latency_loss_floor: false, pmtud: false, pmtud_max_probes: pmtud::MAX_PROBES_DEFAULT, hystart: true, @@ -1150,6 +1152,23 @@ impl Config { self.enable_send_streams_blocked = enable; } + /// Configure whether the loss delay is floored at the ack latency the + /// connection has observed. + /// + /// Acknowledgements are processed in batches, and the RTT estimate only + /// samples the freshest packet of each batch. On a path whose RTT is + /// close to or below the granularity of ack processing, the time and + /// packet thresholds declare packets lost whose acknowledgements have + /// not had a chance to be processed yet. With this enabled, the loss + /// delay is never lower than the slowest send-to-processed-ack loop + /// observed in the last half second, and the packet reordering + /// threshold only applies to packets older than that loop. + /// + /// The default value is false. + pub fn set_enable_ack_latency_loss_floor(&mut self, enable: bool) { + self.enable_ack_latency_loss_floor = enable; + } + /// Configures whether to enable HyStart++. /// /// The default value is `true`. diff --git a/quiche/src/recovery/congestion/recovery.rs b/quiche/src/recovery/congestion/recovery.rs index e02a132b1e0..b78c14acd5e 100644 --- a/quiche/src/recovery/congestion/recovery.rs +++ b/quiche/src/recovery/congestion/recovery.rs @@ -59,6 +59,7 @@ use crate::recovery::LossDetectionTimer; use crate::recovery::OnAckReceivedOutcome; use crate::recovery::ReleaseDecision; use crate::recovery::ReleaseTime; +use crate::recovery::ACK_LOOP_WINDOW; use crate::recovery::GRANULARITY; use crate::recovery::INITIAL_PACKET_THRESHOLD; use crate::recovery::INITIAL_TIME_THRESHOLD; @@ -221,8 +222,8 @@ impl RecoveryEpoch { } fn detect_lost_packets( - &mut self, loss_delay: Duration, pkt_thresh: u64, now: Instant, - trace_id: &str, epoch: Epoch, + &mut self, loss_delay: Duration, pkt_thresh: u64, pkt_age_gate: Duration, + now: Instant, trace_id: &str, epoch: Epoch, ) -> LossDetectionResult { self.loss_time = None; @@ -232,6 +233,9 @@ impl RecoveryEpoch { // Packets sent before this time are deemed lost. let lost_send_time = now.checked_sub(loss_delay).unwrap(); + // The reordering threshold only convicts a packet old enough + // that its acknowledgement had a fair chance to be processed. + let pkt_gate_time = now.checked_sub(pkt_age_gate).unwrap(); let mut lost_packets = 0; let mut lost_bytes = 0; @@ -249,7 +253,8 @@ impl RecoveryEpoch { for unacked in unacked_iter { // Mark packet as lost, or set time when it should be marked. if unacked.time_sent <= lost_send_time || - largest_acked >= unacked.pkt_num + pkt_thresh + (largest_acked >= unacked.pkt_num + pkt_thresh && + unacked.time_sent <= pkt_gate_time) { self.lost_frames_ack.extend(unacked.frames.drain(..)); @@ -357,6 +362,16 @@ pub struct LegacyRecovery { loss_timer: LossDetectionTimer, + // The worst full-loop ack latency (send to processed acknowledgement) + // seen in the current window, and when it was recorded. A loss delay + // below this declares packets whose acknowledgements could not have + // been processed yet whatever the RTT estimate says, because RTT is + // sampled from the freshest packet of each ack batch and batched ack + // processing hides exactly the latency that matters here. + ack_loop_max: Duration, + ack_loop_at: Option, + ack_latency_loss_floor: bool, + pto_count: u32, rtt_stats: RttStats, @@ -396,6 +411,9 @@ impl LegacyRecovery { epochs: Default::default(), loss_timer: Default::default(), + ack_loop_max: Duration::ZERO, + ack_loop_at: None, + ack_latency_loss_floor: recovery_config.enable_ack_latency_loss_floor, pto_count: 0, @@ -531,12 +549,19 @@ impl LegacyRecovery { fn detect_lost_packets( &mut self, epoch: Epoch, now: Instant, trace_id: &str, ) -> (usize, usize) { - let loss_delay = cmp::max(self.rtt_stats.latest_rtt, self.rtt()) + let mut loss_delay = cmp::max(self.rtt_stats.latest_rtt, self.rtt()) .mul_f64(self.time_thresh); + if self.ack_latency_loss_floor { + loss_delay = cmp::max( + loss_delay, + self.ack_loop_max.saturating_add(GRANULARITY), + ); + } let loss = self.epochs[epoch].detect_lost_packets( loss_delay, self.pkt_thresh, + self.ack_loop_max, now, trace_id, epoch, @@ -705,6 +730,25 @@ impl RecoveryOps for LegacyRecovery { return Ok(OnAckReceivedOutcome::default()); } + // The slowest acknowledgement in this batch proves how late an + // honest ack can currently be. Windowed so a transient stall + // stops inflating the loss delay shortly after it passes. + if self.ack_latency_loss_floor { + let batch_loop = self + .newly_acked + .iter() + .map(|acked| now.saturating_duration_since(acked.time_sent)) + .max() + .unwrap_or(Duration::ZERO); + let expired = self.ack_loop_at.is_none_or(|at| { + now.saturating_duration_since(at) > ACK_LOOP_WINDOW + }); + if expired || batch_loop >= self.ack_loop_max { + self.ack_loop_max = batch_loop; + self.ack_loop_at = Some(now); + } + } + let largest_newly_acked = self.newly_acked.last().unwrap(); // Update `largest_acked_packet` based on the validated `newly_acked` diff --git a/quiche/src/recovery/gcongestion/recovery.rs b/quiche/src/recovery/gcongestion/recovery.rs index 3fdd473a33d..5fd19077f0e 100644 --- a/quiche/src/recovery/gcongestion/recovery.rs +++ b/quiche/src/recovery/gcongestion/recovery.rs @@ -33,6 +33,7 @@ use crate::recovery::RecoveryStats; use crate::recovery::ReleaseDecision; use crate::recovery::Sent; use crate::recovery::StartupExit; +use crate::recovery::ACK_LOOP_WINDOW; use crate::recovery::GRANULARITY; use crate::recovery::INITIAL_PACKET_THRESHOLD; use crate::recovery::INITIAL_TIME_THRESHOLD; @@ -267,14 +268,17 @@ impl RecoveryEpoch { } fn detect_and_remove_lost_packets( - &mut self, loss_delay: Duration, pkt_thresh: Option, now: Instant, - newly_lost: &mut Vec, + &mut self, loss_delay: Duration, pkt_thresh: Option, + pkt_age_gate: Duration, now: Instant, newly_lost: &mut Vec, ) -> LossDetectionResult { newly_lost.clear(); let mut lost_bytes = 0; self.loss_time = None; let lost_send_time = now.checked_sub(loss_delay).unwrap(); + // The reordering threshold only convicts a packet old enough + // that its acknowledgement had a fair chance to be processed. + let pkt_gate_time = now.checked_sub(pkt_age_gate).unwrap(); let largest_acked = self.largest_acked_packet.unwrap_or(0); let mut pmtud_lost_bytes = 0; let mut pmtud_lost_packets = SmallVec::new(); @@ -287,7 +291,9 @@ impl RecoveryEpoch { if let SentStatus::Sent { time_sent, .. } = status { let loss_by_time = *time_sent <= lost_send_time; let loss_by_pkt = match pkt_thresh { - Some(pkt_thresh) => largest_acked >= *pkt_num + pkt_thresh, + Some(pkt_thresh) => + largest_acked >= *pkt_num + pkt_thresh && + *time_sent <= pkt_gate_time, None => false, }; @@ -460,6 +466,13 @@ pub struct GRecovery { loss_timer: LossDetectionTimer, + // The worst full-loop ack latency (send to processed acknowledgement) + // seen in the current window, and when it was recorded. Only used + // when `enable_ack_latency_loss_floor` is set. + ack_loop_max: Duration, + ack_loop_at: Option, + ack_latency_loss_floor: bool, + pto_count: u32, rtt_stats: RttStats, @@ -531,6 +544,9 @@ impl GRecovery { ), recovery_stats: RecoveryStats::default(), loss_timer: Default::default(), + ack_loop_max: Duration::ZERO, + ack_loop_at: None, + ack_latency_loss_floor: recovery_config.enable_ack_latency_loss_floor, pto_count: 0, lost_count: 0, @@ -568,8 +584,12 @@ impl GRecovery { fn detect_and_remove_lost_packets( &mut self, epoch: packet::Epoch, now: Instant, ) -> (usize, usize) { - let loss_delay = + let mut loss_delay = self.rtt_stats.loss_delay(self.loss_thresh.time_thresh()); + if self.ack_latency_loss_floor { + loss_delay = + loss_delay.max(self.ack_loop_max.saturating_add(GRANULARITY)); + } let lost = &mut self.lost_reuse; let LossDetectionResult { @@ -580,6 +600,7 @@ impl GRecovery { } = self.epochs[epoch].detect_and_remove_lost_packets( loss_delay, self.loss_thresh.pkt_thresh(), + self.ack_loop_max, now, lost, ); @@ -840,6 +861,25 @@ impl RecoveryOps for GRecovery { }); } + // The slowest acknowledgement in this batch proves how late an + // honest ack can currently be. Windowed so a transient stall + // stops inflating the loss delay shortly after it passes. + if self.ack_latency_loss_floor { + let batch_loop = self + .newly_acked + .iter() + .map(|acked| now.saturating_duration_since(acked.time_sent)) + .max() + .unwrap_or(Duration::ZERO); + let expired = self.ack_loop_at.is_none_or(|at| { + now.saturating_duration_since(at) > ACK_LOOP_WINDOW + }); + if expired || batch_loop >= self.ack_loop_max { + self.ack_loop_max = batch_loop; + self.ack_loop_at = Some(now); + } + } + self.bytes_in_flight.saturating_subtract(acked_bytes, now); let largest_newly_acked = self.newly_acked.last().unwrap(); diff --git a/quiche/src/recovery/mod.rs b/quiche/src/recovery/mod.rs index dca61f4ebec..5ed50062462 100644 --- a/quiche/src/recovery/mod.rs +++ b/quiche/src/recovery/mod.rs @@ -86,6 +86,9 @@ const TIME_THRESHOLD_OVERHEAD_MULTIPLIER: f64 = 2.0; const GRANULARITY: Duration = Duration::from_millis(1); +// How long an observed ack-loop latency keeps flooring the loss delay. +const ACK_LOOP_WINDOW: Duration = Duration::from_millis(512); + const MAX_PTO_PROBES_COUNT: usize = 2; const MINIMUM_WINDOW_PACKETS: usize = 2; @@ -141,6 +144,7 @@ pub struct RecoveryConfig { pub initial_congestion_window_packets: usize, pub enable_relaxed_loss_threshold: bool, pub enable_cubic_idle_restart_fix: bool, + pub enable_ack_latency_loss_floor: bool, } impl RecoveryConfig { @@ -158,6 +162,7 @@ impl RecoveryConfig { .initial_congestion_window_packets, enable_relaxed_loss_threshold: config.enable_relaxed_loss_threshold, enable_cubic_idle_restart_fix: config.enable_cubic_idle_restart_fix, + enable_ack_latency_loss_floor: config.enable_ack_latency_loss_floor, } } } @@ -1476,6 +1481,206 @@ mod tests { } } + #[rstest] + fn ack_latency_floor_spares_packets_younger_than_the_ack_loop( + #[values("reno", "cubic", "bbr2", "bbr2_gcongestion")] + cc_algorithm_name: &str, + ) { + let mut cfg = Config::new(crate::PROTOCOL_VERSION).unwrap(); + assert_eq!(cfg.set_cc_algorithm_name(cc_algorithm_name), Ok(())); + cfg.set_enable_ack_latency_loss_floor(true); + + let mut r = Recovery::new(&cfg); + let mut now = Instant::now(); + + // Two packets whose acks take 4ms to come back, which teaches the + // recovery how late an honest ack can be on this path. + for i in 0..2 { + let p = test_utils::helper_packet_sent(i, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + } + + now += Duration::from_millis(4); + + let mut acked = RangeSet::default(); + acked.insert(0..2); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap(), + OnAckReceivedOutcome { + lost_packets: 0, + lost_bytes: 0, + acked_bytes: 1000 * 2, + spurious_losses: 0, + } + ); + + // Six more packets, followed 1ms later by an ack batch that only + // covers the last three. Without the floor the packet threshold + // declares 2, 3 and 4 lost on the spot; with it they are younger + // than the 4ms ack loop just observed, so they are spared. + for i in 2..8 { + let p = test_utils::helper_packet_sent(i, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + } + + now += Duration::from_millis(1); + + let mut acked = RangeSet::default(); + acked.insert(5..8); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap(), + OnAckReceivedOutcome { + lost_packets: 0, + lost_bytes: 0, + acked_bytes: 1000 * 3, + spurious_losses: 0, + } + ); + + // The late acks arrive and nothing was ever declared. + now += Duration::from_millis(1); + + let mut acked = RangeSet::default(); + acked.insert(2..5); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap(), + OnAckReceivedOutcome { + lost_packets: 0, + lost_bytes: 0, + acked_bytes: 1000 * 3, + spurious_losses: 0, + } + ); + + assert_eq!(r.lost_count(), 0); + assert_eq!(r.lost_spurious_count(), 0); + assert_eq!(r.bytes_in_flight(), 0); + } + + #[rstest] + fn ack_latency_floor_still_detects_loss_by_time( + #[values("reno", "cubic", "bbr2", "bbr2_gcongestion")] + cc_algorithm_name: &str, + ) { + let mut cfg = Config::new(crate::PROTOCOL_VERSION).unwrap(); + assert_eq!(cfg.set_cc_algorithm_name(cc_algorithm_name), Ok(())); + cfg.set_enable_ack_latency_loss_floor(true); + + let mut r = Recovery::new(&cfg); + let mut now = Instant::now(); + + for i in 0..2 { + let p = test_utils::helper_packet_sent(i, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + } + + // Packet 1 is acked 2ms after sending; packet 0 stays out. The + // floor lifts the loss delay to 3ms (2ms observed loop plus the + // granularity), so packet 0 is not declared here. + now += Duration::from_millis(2); + + let mut acked = RangeSet::default(); + acked.insert(1..2); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap(), + OnAckReceivedOutcome { + lost_packets: 0, + lost_bytes: 0, + acked_bytes: 1000, + spurious_losses: 0, + } + ); + + // A third packet is sent and acked past the floored delay, and + // the time threshold convicts packet 0 on that ack. + let p = test_utils::helper_packet_sent(2, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + + now += Duration::from_millis(2); + + let mut acked = RangeSet::default(); + acked.insert(2..3); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap(), + OnAckReceivedOutcome { + lost_packets: 1, + lost_bytes: 1000, + acked_bytes: 1000, + spurious_losses: 0, + } + ); + assert_eq!(r.lost_count(), 1); + } + // TODO: This should run agains both `congestion` and `gcongestion`. // `congestion` and `gcongestion` behave differently. That might be ok // given the different algorithms but it would be ideal to merge and share From 8082b8e8c9ffb9dff815a51abe5d70e9be050976 Mon Sep 17 00:00:00 2001 From: David Torcivia Date: Tue, 11 Aug 2026 13:22:38 -0400 Subject: [PATCH 2/5] Require established quiche TLS exporters Returns InvalidState when export_keying_material is called before connection establishment. Tests cover fresh and partial handshakes while retaining the existing established exporter checks. (cherry picked from commit 31a2d9c4f11c3a1dd290ad42bf5b9018b4d6f30d) Signed-off-by: David Torcivia --- quiche/src/lib.rs | 9 ++++++++- quiche/src/tests.rs | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/quiche/src/lib.rs b/quiche/src/lib.rs index 9ffa3ee9a38..c2a6697cc35 100644 --- a/quiche/src/lib.rs +++ b/quiche/src/lib.rs @@ -7637,11 +7637,18 @@ impl Connection { /// /// The exporter is available after the TLS handshake completes. The label /// identifies the protocol usage, while the optional context can separate - /// exporter instances within that usage. + /// exporter instances within that usage. Returns [`InvalidState`] until + /// the connection is established. + /// + /// [`InvalidState`]: enum.Error.html#variant.InvalidState #[inline] pub fn export_keying_material( &self, out: &mut [u8], label: &[u8], context: Option<&[u8]>, ) -> Result<()> { + if !self.is_established() { + return Err(Error::InvalidState); + } + self.handshake.export_keying_material(out, label, context) } diff --git a/quiche/src/tests.rs b/quiche/src/tests.rs index 9a7c6b9d5a8..9f358b90beb 100644 --- a/quiche/src/tests.rs +++ b/quiche/src/tests.rs @@ -464,6 +464,15 @@ fn handshake(#[values("cubic", "bbr2_gcongestion")] cc_algorithm_name: &str) { #[test] fn export_keying_material() { let mut pipe = test_utils::Pipe::new("cubic").unwrap(); + let mut before_handshake = [0; 32]; + assert_eq!( + pipe.client.export_keying_material( + &mut before_handshake, + b"EXPORTER-quiche-test", + None, + ), + Err(Error::InvalidState), + ); assert_eq!(pipe.handshake(), Ok(())); let mut client = [0; 32]; @@ -530,6 +539,15 @@ fn handshake_confirmation( assert!(!pipe.server.is_established()); assert!(!pipe.server.handshake_confirmed); + let mut server_exporter = [0; 32]; + assert_eq!( + pipe.server.export_keying_material( + &mut server_exporter, + b"EXPORTER-quiche-test", + None, + ), + Err(Error::InvalidState), + ); test_utils::process_flight(&mut pipe.client, flight).unwrap(); From 58d73771dae1259978768853dc254daf1ece0517 Mon Sep 17 00:00:00 2001 From: David Torcivia Date: Wed, 19 Aug 2026 00:36:03 -0400 Subject: [PATCH 3/5] fix(recovery): expire ACK loss floor on its timer Signed-off-by: David Torcivia --- quiche/src/recovery/congestion/recovery.rs | 32 +++++++++- quiche/src/recovery/gcongestion/recovery.rs | 32 +++++++++- quiche/src/recovery/mod.rs | 65 +++++++++++++++++++++ 3 files changed, 123 insertions(+), 6 deletions(-) diff --git a/quiche/src/recovery/congestion/recovery.rs b/quiche/src/recovery/congestion/recovery.rs index b78c14acd5e..b7669e06aec 100644 --- a/quiche/src/recovery/congestion/recovery.rs +++ b/quiche/src/recovery/congestion/recovery.rs @@ -471,6 +471,23 @@ impl LegacyRecovery { (time, epoch) } + fn expire_ack_loop_floor(&mut self, now: Instant) -> bool { + let expired = self.ack_latency_loss_floor && + self.ack_loop_at.is_some_and(|at| { + now.saturating_duration_since(at) >= ACK_LOOP_WINDOW + }); + if expired { + self.ack_loop_max = Duration::ZERO; + self.ack_loop_at = None; + } + expired + } + + fn cap_timer_at_ack_loop_expiry(&self, timeout: Instant) -> Instant { + self.ack_loop_at + .map_or(timeout, |at| cmp::min(timeout, at + ACK_LOOP_WINDOW)) + } + fn pto_time_and_space( &self, handshake_status: HandshakeStatus, now: Instant, ) -> (Option, Epoch) { @@ -526,7 +543,8 @@ impl LegacyRecovery { if let Some(to) = earliest_loss_time { // Time threshold loss detection. - self.loss_timer.update(to); + self.loss_timer + .update(self.cap_timer_at_ack_loop_expiry(to)); return; } @@ -540,7 +558,8 @@ impl LegacyRecovery { // PTO timer. if let (Some(timeout), _) = self.pto_time_and_space(handshake_status, now) { - self.loss_timer.update(timeout); + self.loss_timer + .update(self.cap_timer_at_ack_loop_expiry(timeout)); } else { self.loss_timer.clear(); } @@ -549,6 +568,7 @@ impl LegacyRecovery { fn detect_lost_packets( &mut self, epoch: Epoch, now: Instant, trace_id: &str, ) -> (usize, usize) { + self.expire_ack_loop_floor(now); let mut loss_delay = cmp::max(self.rtt_stats.latest_rtt, self.rtt()) .mul_f64(self.time_thresh); if self.ack_latency_loss_floor { @@ -741,7 +761,7 @@ impl RecoveryOps for LegacyRecovery { .max() .unwrap_or(Duration::ZERO); let expired = self.ack_loop_at.is_none_or(|at| { - now.saturating_duration_since(at) > ACK_LOOP_WINDOW + now.saturating_duration_since(at) >= ACK_LOOP_WINDOW }); if expired || batch_loop >= self.ack_loop_max { self.ack_loop_max = batch_loop; @@ -805,6 +825,7 @@ impl RecoveryOps for LegacyRecovery { &mut self, handshake_status: HandshakeStatus, now: Instant, trace_id: &str, ) -> OnLossDetectionTimeoutOutcome { + let ack_loop_expired = self.expire_ack_loop_floor(now); let (earliest_loss_time, epoch) = self.loss_time_and_space(); if earliest_loss_time.is_some() { @@ -821,6 +842,11 @@ impl RecoveryOps for LegacyRecovery { }; } + if ack_loop_expired { + self.set_loss_detection_timer(handshake_status, now); + return OnLossDetectionTimeoutOutcome::default(); + } + let epoch = if self.bytes_in_flight.get() > 0 { // Send new data if available, else retransmit old data. If neither // is available, send a single PING frame. diff --git a/quiche/src/recovery/gcongestion/recovery.rs b/quiche/src/recovery/gcongestion/recovery.rs index 5fd19077f0e..f9456926fe4 100644 --- a/quiche/src/recovery/gcongestion/recovery.rs +++ b/quiche/src/recovery/gcongestion/recovery.rs @@ -584,6 +584,7 @@ impl GRecovery { fn detect_and_remove_lost_packets( &mut self, epoch: packet::Epoch, now: Instant, ) -> (usize, usize) { + self.expire_ack_loop_floor(now); let mut loss_delay = self.rtt_stats.loss_delay(self.loss_thresh.time_thresh()); if self.ack_latency_loss_floor { @@ -631,6 +632,23 @@ impl GRecovery { (time, epoch) } + fn expire_ack_loop_floor(&mut self, now: Instant) -> bool { + let expired = self.ack_latency_loss_floor && + self.ack_loop_at.is_some_and(|at| { + now.saturating_duration_since(at) >= ACK_LOOP_WINDOW + }); + if expired { + self.ack_loop_max = Duration::ZERO; + self.ack_loop_at = None; + } + expired + } + + fn cap_timer_at_ack_loop_expiry(&self, timeout: Instant) -> Instant { + self.ack_loop_at + .map_or(timeout, |at| timeout.min(at + ACK_LOOP_WINDOW)) + } + fn pto_time_and_space( &self, handshake_status: HandshakeStatus, now: Instant, ) -> (Option, packet::Epoch) { @@ -685,7 +703,8 @@ impl GRecovery { ) { if let (Some(earliest_loss_time), _) = self.loss_time_and_space() { // Time threshold loss detection. - self.loss_timer.update(earliest_loss_time); + self.loss_timer + .update(self.cap_timer_at_ack_loop_expiry(earliest_loss_time)); return; } @@ -699,7 +718,8 @@ impl GRecovery { // PTO timer. if let (Some(timeout), _) = self.pto_time_and_space(handshake_status, now) { - self.loss_timer.update(timeout); + self.loss_timer + .update(self.cap_timer_at_ack_loop_expiry(timeout)); } else { self.loss_timer.clear(); } @@ -872,7 +892,7 @@ impl RecoveryOps for GRecovery { .max() .unwrap_or(Duration::ZERO); let expired = self.ack_loop_at.is_none_or(|at| { - now.saturating_duration_since(at) > ACK_LOOP_WINDOW + now.saturating_duration_since(at) >= ACK_LOOP_WINDOW }); if expired || batch_loop >= self.ack_loop_max { self.ack_loop_max = batch_loop; @@ -939,6 +959,7 @@ impl RecoveryOps for GRecovery { &mut self, handshake_status: HandshakeStatus, now: Instant, trace_id: &str, ) -> OnLossDetectionTimeoutOutcome { + let ack_loop_expired = self.expire_ack_loop_floor(now); let (earliest_loss_time, epoch) = self.loss_time_and_space(); if earliest_loss_time.is_some() { @@ -970,6 +991,11 @@ impl RecoveryOps for GRecovery { }; } + if ack_loop_expired { + self.set_loss_detection_timer(handshake_status, now); + return OnLossDetectionTimeoutOutcome::default(); + } + let epoch = if self.bytes_in_flight.get() > 0 { // Send new data if available, else retransmit old data. If neither // is available, send a single PING frame. diff --git a/quiche/src/recovery/mod.rs b/quiche/src/recovery/mod.rs index 5ed50062462..4c0257fa6ef 100644 --- a/quiche/src/recovery/mod.rs +++ b/quiche/src/recovery/mod.rs @@ -1681,6 +1681,71 @@ mod tests { assert_eq!(r.lost_count(), 1); } + #[rstest] + fn ack_latency_floor_expires_without_another_ack( + #[values("reno", "cubic", "bbr2", "bbr2_gcongestion")] + cc_algorithm_name: &str, + ) { + let mut cfg = Config::new(crate::PROTOCOL_VERSION).unwrap(); + assert_eq!(cfg.set_cc_algorithm_name(cc_algorithm_name), Ok(())); + cfg.set_enable_ack_latency_loss_floor(true); + + let mut r = Recovery::new(&cfg); + let mut now = Instant::now(); + + let p = test_utils::helper_packet_sent(0, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + + // A pump stall makes packet 0's acknowledgement appear two seconds + // old. Packet 1 is the tail loss and packet 2 provides a fresh RTT + // sample in the same acknowledgement batch. + now += Duration::from_secs(2); + for i in 1..3 { + let p = test_utils::helper_packet_sent(i, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + } + now += Duration::from_millis(4); + + let mut acked = RangeSet::default(); + acked.insert(0..1); + acked.insert(2..3); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap() + .lost_packets, + 0 + ); + + let timer = r.loss_detection_timer().unwrap(); + assert!(timer <= now + ACK_LOOP_WINDOW); + assert_eq!( + r.on_loss_detection_timeout(HandshakeStatus::default(), timer, "",) + .lost_packets, + 1 + ); + assert_eq!(r.lost_count(), 1); + } + // TODO: This should run agains both `congestion` and `gcongestion`. // `congestion` and `gcongestion` behave differently. That might be ok // given the different algorithms but it would be ideal to merge and share From 927464f6249cf75e3bae529bfde9696a6efbdc7e Mon Sep 17 00:00:00 2001 From: David Torcivia Date: Wed, 19 Aug 2026 01:12:33 -0400 Subject: [PATCH 4/5] fix(recovery): preserve overdue PTO on floor expiry Signed-off-by: David Torcivia --- quiche/src/recovery/congestion/recovery.rs | 6 +- quiche/src/recovery/gcongestion/recovery.rs | 6 +- quiche/src/recovery/mod.rs | 64 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/quiche/src/recovery/congestion/recovery.rs b/quiche/src/recovery/congestion/recovery.rs index b7669e06aec..2d33a545ed9 100644 --- a/quiche/src/recovery/congestion/recovery.rs +++ b/quiche/src/recovery/congestion/recovery.rs @@ -842,7 +842,11 @@ impl RecoveryOps for LegacyRecovery { }; } - if ack_loop_expired { + if ack_loop_expired && + self.pto_time_and_space(handshake_status, now) + .0 + .is_none_or(|timeout| timeout > now) + { self.set_loss_detection_timer(handshake_status, now); return OnLossDetectionTimeoutOutcome::default(); } diff --git a/quiche/src/recovery/gcongestion/recovery.rs b/quiche/src/recovery/gcongestion/recovery.rs index f9456926fe4..521a4512f93 100644 --- a/quiche/src/recovery/gcongestion/recovery.rs +++ b/quiche/src/recovery/gcongestion/recovery.rs @@ -991,7 +991,11 @@ impl RecoveryOps for GRecovery { }; } - if ack_loop_expired { + if ack_loop_expired && + self.pto_time_and_space(handshake_status, now) + .0 + .is_none_or(|timeout| timeout > now) + { self.set_loss_detection_timer(handshake_status, now); return OnLossDetectionTimeoutOutcome::default(); } diff --git a/quiche/src/recovery/mod.rs b/quiche/src/recovery/mod.rs index 4c0257fa6ef..86b74577da1 100644 --- a/quiche/src/recovery/mod.rs +++ b/quiche/src/recovery/mod.rs @@ -1746,6 +1746,70 @@ mod tests { assert_eq!(r.lost_count(), 1); } + #[rstest] + fn an_overdue_pto_is_not_skipped_when_the_ack_latency_floor_expires( + #[values("reno", "cubic", "bbr2", "bbr2_gcongestion")] + cc_algorithm_name: &str, + ) { + let mut cfg = Config::new(crate::PROTOCOL_VERSION).unwrap(); + assert_eq!(cfg.set_cc_algorithm_name(cc_algorithm_name), Ok(())); + cfg.set_enable_ack_latency_loss_floor(true); + + let mut r = Recovery::new(&cfg); + let mut now = Instant::now(); + + let p = test_utils::helper_packet_sent(0, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + + now += Duration::from_secs(2); + for i in 1..3 { + let p = test_utils::helper_packet_sent(i, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + "", + ); + } + now += Duration::from_millis(4); + + // The old and fresh acknowledgements establish the floor while the + // higher-numbered tail packet remains outstanding, so there is no + // time-threshold loss to handle before its PTO. + let mut acked = RangeSet::default(); + acked.insert(0..2); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + HandshakeStatus::default(), + now, + None, + "", + ) + .unwrap() + .lost_packets, + 0 + ); + + now += ACK_LOOP_WINDOW + Duration::from_secs(1); + assert_eq!( + r.on_loss_detection_timeout(HandshakeStatus::default(), now, "",) + .lost_packets, + 0 + ); + assert_eq!(r.pto_count(), 1); + assert_eq!(r.loss_probes(packet::Epoch::Application), 1); + } + // TODO: This should run agains both `congestion` and `gcongestion`. // `congestion` and `gcongestion` behave differently. That might be ok // given the different algorithms but it would be ideal to merge and share From 2bb421877826605514a4c21e7315b3439619c35f Mon Sep 17 00:00:00 2001 From: David Torcivia Date: Wed, 19 Aug 2026 01:19:50 -0400 Subject: [PATCH 5/5] fix(recovery): preserve anti-deadlock PTO timing Signed-off-by: David Torcivia --- quiche/src/recovery/congestion/recovery.rs | 8 ++- quiche/src/recovery/gcongestion/recovery.rs | 8 ++- quiche/src/recovery/mod.rs | 64 +++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/quiche/src/recovery/congestion/recovery.rs b/quiche/src/recovery/congestion/recovery.rs index 2d33a545ed9..46f7526ac24 100644 --- a/quiche/src/recovery/congestion/recovery.rs +++ b/quiche/src/recovery/congestion/recovery.rs @@ -558,8 +558,11 @@ impl LegacyRecovery { // PTO timer. if let (Some(timeout), _) = self.pto_time_and_space(handshake_status, now) { - self.loss_timer - .update(self.cap_timer_at_ack_loop_expiry(timeout)); + self.loss_timer.update(if self.bytes_in_flight.is_zero() { + timeout + } else { + self.cap_timer_at_ack_loop_expiry(timeout) + }); } else { self.loss_timer.clear(); } @@ -843,6 +846,7 @@ impl RecoveryOps for LegacyRecovery { } if ack_loop_expired && + !self.bytes_in_flight.is_zero() && self.pto_time_and_space(handshake_status, now) .0 .is_none_or(|timeout| timeout > now) diff --git a/quiche/src/recovery/gcongestion/recovery.rs b/quiche/src/recovery/gcongestion/recovery.rs index 521a4512f93..5ae97276b17 100644 --- a/quiche/src/recovery/gcongestion/recovery.rs +++ b/quiche/src/recovery/gcongestion/recovery.rs @@ -718,8 +718,11 @@ impl GRecovery { // PTO timer. if let (Some(timeout), _) = self.pto_time_and_space(handshake_status, now) { - self.loss_timer - .update(self.cap_timer_at_ack_loop_expiry(timeout)); + self.loss_timer.update(if self.bytes_in_flight.is_zero() { + timeout + } else { + self.cap_timer_at_ack_loop_expiry(timeout) + }); } else { self.loss_timer.clear(); } @@ -992,6 +995,7 @@ impl RecoveryOps for GRecovery { } if ack_loop_expired && + !self.bytes_in_flight.is_zero() && self.pto_time_and_space(handshake_status, now) .0 .is_none_or(|timeout| timeout > now) diff --git a/quiche/src/recovery/mod.rs b/quiche/src/recovery/mod.rs index 86b74577da1..5e9645ac2e7 100644 --- a/quiche/src/recovery/mod.rs +++ b/quiche/src/recovery/mod.rs @@ -1810,6 +1810,70 @@ mod tests { assert_eq!(r.loss_probes(packet::Epoch::Application), 1); } + #[rstest] + fn ack_floor_expiry_does_not_delay_an_anti_deadlock_pto( + #[values("reno", "cubic", "bbr2", "bbr2_gcongestion")] + cc_algorithm_name: &str, + ) { + let mut cfg = Config::new(crate::PROTOCOL_VERSION).unwrap(); + assert_eq!(cfg.set_cc_algorithm_name(cc_algorithm_name), Ok(())); + cfg.set_enable_ack_latency_loss_floor(true); + + let mut r = Recovery::new(&cfg); + let mut now = Instant::now(); + let handshake_status = HandshakeStatus { + peer_verified_address: false, + ..HandshakeStatus::default() + }; + + let p = test_utils::helper_packet_sent(0, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + handshake_status, + now, + "", + ); + + now += Duration::from_secs(2); + let p = test_utils::helper_packet_sent(1, now, 1000); + r.on_packet_sent( + p, + packet::Epoch::Application, + handshake_status, + now, + "", + ); + now += Duration::from_millis(4); + + let mut acked = RangeSet::default(); + acked.insert(0..2); + assert_eq!( + r.on_ack_received( + &acked, + 25, + packet::Epoch::Application, + handshake_status, + now, + None, + "", + ) + .unwrap() + .acked_bytes, + 2000 + ); + assert_eq!(r.bytes_in_flight(), 0); + + // With no bytes in flight, this is the anti-deadlock PTO. The ACK + // floor must neither pull it earlier nor suppress a late callback. + let timer = r.loss_detection_timer().unwrap(); + assert_eq!(timer, now + r.pto()); + now = timer + ACK_LOOP_WINDOW; + r.on_loss_detection_timeout(handshake_status, now, ""); + assert_eq!(r.pto_count(), 1); + assert_eq!(r.loss_probes(packet::Epoch::Handshake), 1); + } + // TODO: This should run agains both `congestion` and `gcongestion`. // `congestion` and `gcongestion` behave differently. That might be ok // given the different algorithms but it would be ideal to merge and share