@@ -1712,8 +1712,6 @@ impl Connection {
17121712 let rtt = self . path . rtt . conservative ( ) ;
17131713 let loss_delay = cmp:: max ( rtt. mul_f32 ( self . config . time_threshold ) , TIMER_GRANULARITY ) ;
17141714
1715- // Packets sent before this time are deemed lost.
1716- let lost_send_time = now. checked_sub ( loss_delay) . unwrap ( ) ;
17171715 let largest_acked_packet = self . spaces [ pn_space] . largest_acked_packet . unwrap ( ) ;
17181716 let packet_threshold = self . config . packet_threshold as u64 ;
17191717 let mut size_of_lost_packets = 0u64 ;
@@ -1737,8 +1735,10 @@ impl Connection {
17371735 persistent_congestion_start = None ;
17381736 }
17391737
1740- if info. time_sent <= lost_send_time || largest_acked_packet >= packet + packet_threshold
1741- {
1738+ // Packets sent before now - loss_delay are deemed lost.
1739+ // However, we avoid this subtraction as it can panic.
1740+ let packet_too_old = instant_saturating_sub ( now, info. time_sent ) > loss_delay;
1741+ if packet_too_old || largest_acked_packet >= packet + packet_threshold {
17421742 if Some ( packet) == in_flight_mtu_probe {
17431743 // Lost MTU probes are not included in `lost_packets`, because they should not
17441744 // trigger a congestion control response
@@ -1791,6 +1791,17 @@ impl Connection {
17911791 lost_packets, size_of_lost_packets
17921792 ) ;
17931793
1794+ // Packets sent before this time are deemed lost.
1795+ // We avoid computing this value above, since it's possible for this to panic
1796+ // if the `loss_delay` value internally stores a bigger `Duration` than the
1797+ // `Duration` that's stored inside the `Instant`, because some platforms may
1798+ // implement the `Instant` with a counter relative to system or even process
1799+ // startup (Wasm is one such case).
1800+ // If we're at this point, then it must be possible to have instants that are
1801+ // longer ago than `loss_delay` (see the `packet_too_old` computation
1802+ // above).
1803+ let lost_send_time = now. checked_sub ( loss_delay) . unwrap ( ) ;
1804+
17941805 for & packet in & lost_packets {
17951806 let info = self . spaces [ pn_space] . take ( packet) . unwrap ( ) ; // safe: lost_packets is populated just above
17961807 self . config . qlog_sink . emit_packet_lost (
0 commit comments