Keep receiving when fragment sets go stale

A fragment set that lost one fragment was never reclaimed: the buffer map
only shrank on successful reassembly or a payload-size violation. Once 64
sets were stranded, every new fragmented payload returned an error and the
receiver's loop terminated. Any nonzero loss on any fragmenting channel was
therefore a countdown, not a threshold: 3 fragments at 3% loss killed the
receiver at ~700 payloads, 1% at ~2,100. Found by the media load test.

When the bound is hit, evict the set untouched longest and count it via
fragment_sets_evicted(). A stranded set's siblings were delivered long ago;
dropping it is the right call and it was already lost.

Also stop saturating the fragment id. saturating_add parked every set after
the 65,535th on one shared id, and the reassembler stitched them together.
That is the July 2026 eight-minute failure; the id now wraps to 1.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-10 11:37:50 +02:00
commit d3bc6e6604
2 changed files with 120 additions and 2 deletions

View file

@ -167,6 +167,10 @@ struct FragmentBuffer {
fragment_count: u16,
payloads: BTreeMap<u16, Vec<u8>>,
sequences: BTreeMap<u16, u32>,
/// Session-local arrival ordinal of the newest fragment in this set. A
/// set that stops being touched is one that lost a fragment; it is the
/// first to go when the pending-set bound is reached.
last_touched: u64,
}
pub struct CultNetRudpSession {
@ -191,6 +195,8 @@ pub struct CultNetRudpSession {
ordered_next_sequence_by_channel: BTreeMap<String, u32>,
ordered_buffers: BTreeMap<String, BTreeMap<u32, PendingOrderedFrame>>,
fragment_buffers: BTreeMap<(String, u16), FragmentBuffer>,
fragment_touches: u64,
fragment_sets_evicted: u64,
}
impl CultNetRudpSession {
@ -217,6 +223,8 @@ impl CultNetRudpSession {
ordered_next_sequence_by_channel: BTreeMap::new(),
ordered_buffers: BTreeMap::new(),
fragment_buffers: BTreeMap::new(),
fragment_touches: 0,
fragment_sets_evicted: 0,
}
}
@ -247,6 +255,14 @@ impl CultNetRudpSession {
self.reliable_packets_expired
}
/// Incomplete fragment sets discarded to admit a newer one. Each is a
/// payload that lost at least one fragment on the wire; a rising count is
/// the link's loss rate multiplied by the fragment count, and the reason
/// media payloads should fit in one packet.
pub fn fragment_sets_evicted(&self) -> u64 {
self.fragment_sets_evicted
}
pub fn queued_reliable_packet_count(&self) -> usize {
self.queued_reliable.len()
}
@ -968,7 +984,19 @@ impl CultNetRudpSession {
if !self.fragment_buffers.contains_key(&key)
&& self.fragment_buffers.len() >= self.max_pending_fragment_sets
{
return Err(anyhow!("RUDP pending fragment-set limit reached"));
// A set that is still incomplete when the bound is reached has
// almost certainly lost a fragment; its siblings were delivered
// long ago. Drop the one untouched longest so the session keeps
// receiving. Erroring here made any lossy fragmenting channel a
// countdown to a dead receiver.
let stalest = self
.fragment_buffers
.iter()
.min_by_key(|(_, buffer)| buffer.last_touched)
.map(|(key, _)| key.clone())
.expect("bound reached implies a pending set exists");
self.fragment_buffers.remove(&stalest);
self.fragment_sets_evicted += 1;
}
let buffered_bytes = self
.fragment_buffers
@ -995,12 +1023,15 @@ impl CultNetRudpSession {
fragment_count: packet.fragment_count,
payloads: BTreeMap::new(),
sequences: BTreeMap::new(),
last_touched: 0,
});
if buffer.fragment_count != packet.fragment_count || buffer.ordered != packet.ordered {
return Err(anyhow!(
"RUDP fragment metadata changed within a fragment set"
));
}
self.fragment_touches += 1;
buffer.last_touched = self.fragment_touches;
buffer
.payloads
.insert(packet.fragment_index, packet.payload.clone());
@ -1171,7 +1202,9 @@ impl CultNetRudpSession {
fn allocate_fragment_id(&mut self) -> u16 {
let fragment_id = self.next_fragment_id;
self.next_fragment_id = self.next_fragment_id.saturating_add(1);
// Wrap, never saturate: `saturating_add` parked every set after the
// 65,535th on one shared id, and the reassembler stitched them together.
self.next_fragment_id = self.next_fragment_id.wrapping_add(1);
if self.next_fragment_id == 0 {
self.next_fragment_id = 1;
}

View file

@ -2856,3 +2856,88 @@ fn rudp_profile_expires_only_the_media_channel() -> Result<()> {
}
Ok(())
}
fn connected_rudp_pair() -> Result<(CultNetRudpSession, CultNetRudpSession)> {
let mut client = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 311,
initial_sequence: 1,
..CultNetRudpSessionOptions::default()
});
let mut server = CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: 311,
initial_sequence: 100,
..CultNetRudpSessionOptions::default()
});
let connect = client.create_connect(0, Vec::new())?;
let accept = server.accept_connect(&connect, 1, Vec::new())?;
client.receive(&accept, 2)?;
Ok((client, server))
}
#[test]
fn rudp_evicts_stalest_incomplete_fragment_set_instead_of_dying() -> Result<()> {
let (mut client, mut server) = connected_rudp_pair()?;
client.set_max_pending_fragment_sets(4)?;
let fragmented = |server: &mut CultNetRudpSession, fill: u8| {
server.send_many(
"media",
vec![fill; 2500],
CultNetRudpSendOptions::default(),
Some(1000),
)
};
// Eight payloads each lose their last fragment on the wire. Before, the
// fifth new set errored the receiver out and every later fragmented
// payload with it.
let mut stranded = Vec::new();
for fill in 1..=8u8 {
let packets = fragmented(&mut server, fill)?;
assert_eq!(packets.len(), 3);
for packet in &packets[..2] {
assert!(client.receive(packet, 10)?.delivered.is_empty());
}
stranded.push(packets[2].clone());
}
assert_eq!(client.fragment_sets_evicted(), 4);
// A complete payload still lands.
let complete = fragmented(&mut server, 9)?;
let mut delivered = Vec::new();
for packet in &complete {
delivered.extend(client.receive(packet, 11)?.delivered);
}
assert_eq!(delivered.len(), 1);
assert_eq!(delivered[0].payload, vec![9u8; 2500]);
assert_eq!(client.fragment_sets_evicted(), 5);
// The eviction order is stalest-first: set 8 is still pending and completes
// when its late fragment shows up; set 1 was evicted long ago, so its late
// fragment opens a fresh set and delivers nothing.
assert_eq!(
client.receive(&stranded[7], 12)?.delivered[0].payload,
vec![8u8; 2500]
);
assert!(client.receive(&stranded[0], 13)?.delivered.is_empty());
Ok(())
}
#[test]
fn rudp_fragment_ids_wrap_instead_of_saturating() -> Result<()> {
let (_client, mut server) = connected_rudp_pair()?;
let mut ids = Vec::new();
for _ in 0..(u16::MAX as usize + 1) {
let packets = server.send_many(
"media",
vec![0u8; 1500],
CultNetRudpSendOptions::default(),
Some(1000),
)?;
ids.push(packets[0].fragment_id);
}
assert_eq!(ids[0], 1);
assert_eq!(ids[u16::MAX as usize - 1], u16::MAX);
// The 65,536th set must not share an id with the 65,535th.
assert_eq!(ids[u16::MAX as usize], 1);
Ok(())
}