cultmesh-rs: RUDP document server drops refused Connects instead of dying

A Connect storm from a flow that never hears its Accept filled the session
reliable queue and accept_connect returned Err out of poll_once. A refused
Connect, a malformed frame and a packet for an unknown session are now
counted in packets_dropped() and poll_once keeps serving.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-29 17:38:20 +02:00
commit ba8466114d
2 changed files with 77 additions and 6 deletions

View file

@ -207,6 +207,7 @@ pub struct CultMeshRudpDocumentServer<S, Q, C> {
snapshot_source: Q,
clock: C,
options: CultMeshRudpDocumentServerOptions,
packets_dropped: u64,
}
impl<S, Q, C> CultMeshRudpDocumentServer<S, Q, C>
@ -234,9 +235,18 @@ where
snapshot_source,
clock,
options,
packets_dropped: 0,
})
}
/// Datagrams read and discarded because they belong to no admissible
/// session: malformed frames, unknown sessions, a Connect the session or
/// the session cap refuses, a packet that fails its session. Never an
/// error: a moved flow or a scanner must not end the daemon loop.
pub fn packets_dropped(&self) -> u64 {
self.packets_dropped
}
pub fn local_addr(&self) -> Result<SocketAddr> {
Ok(self.socket.local_addr()?)
}
@ -264,7 +274,10 @@ where
let packet = match decode_rudp_packet(&wire) {
Ok(packet) => packet,
Err(_) => return Ok(CultMeshRudpPollOutcome::Handled),
Err(_) => {
self.packets_dropped += 1;
return Ok(CultMeshRudpPollOutcome::Handled);
}
};
let now_unix = self.clock.now_unix_millis();
let now_monotonic = self.clock.now_monotonic_millis();
@ -274,11 +287,14 @@ where
};
if packet.packet_type == CultNetRudpPacketType::Connect {
self.accept_connection(key, &packet, now_monotonic)?;
if !self.accept_connection(key, &packet, now_monotonic)? {
self.packets_dropped += 1;
}
return Ok(CultMeshRudpPollOutcome::Handled);
}
if !self.sessions.contains_key(&key) {
self.packets_dropped += 1;
return Ok(CultMeshRudpPollOutcome::Handled);
}
if packet.packet_type == CultNetRudpPacketType::Data
@ -310,6 +326,7 @@ where
Ok(result) => result,
Err(_) => {
self.sessions.remove(&key);
self.packets_dropped += 1;
return Ok(CultMeshRudpPollOutcome::Handled);
}
};
@ -385,15 +402,17 @@ where
})
}
/// `Ok(false)` when the Connect was refused (session cap, or the session
/// cannot queue another Accept); only a socket failure is an `Err`.
fn accept_connection(
&mut self,
key: CultMeshRudpSessionKey,
packet: &CultNetRudpPacket,
now: u64,
) -> Result<()> {
) -> Result<bool> {
if !self.sessions.contains_key(&key) {
if self.sessions.len() >= self.options.max_sessions {
return Ok(());
return Ok(false);
}
self.sessions.insert(
key,
@ -421,9 +440,13 @@ where
// The same key is a retransmitted Connect for the existing epoch.
// A genuinely fresh client incarnation must choose a fresh id.
entry.last_activity_monotonic_millis = now;
entry.session.accept_connect(packet, now, Vec::new())?
match entry.session.accept_connect(packet, now, Vec::new()) {
Ok(accept) => accept,
Err(_) => return Ok(false),
}
};
self.send_packet(key.remote_addr, &accept)
self.send_packet(key.remote_addr, &accept)?;
Ok(true)
}
fn deliver_application_message(

View file

@ -553,3 +553,51 @@ fn session_cap_and_expiry_use_monotonic_time() -> Result<()> {
assert_eq!(server.session_count(), 0);
Ok(())
}
#[test]
fn a_connect_storm_and_stray_frames_are_dropped_not_fatal() -> Result<()> {
let clock = Clock::new(60_000);
let sink = Sink::default();
let options = CultMeshRudpDocumentServerOptions {
max_pending_reliable_packets_per_session: 4,
..Default::default()
};
let mut server = server(options, clock, sink.clone(), Source::default())?;
let target = server.local_addr()?;
let mut real = client(target, 101)?;
connect(&mut server, &mut [&mut real])?;
// A moved flow re-sends Connect from a socket that never hears the Accept:
// each one asks the session to queue one more unacknowledged Accept.
let storm = UdpSocket::bind("127.0.0.1:0")?;
let mut storm_client = cultnet_rs::CultNetRudpSession::new(cultnet_rs::CultNetRudpSessionOptions {
connection_id: 102,
initial_sequence: 1,
resend_delay_ms: 10,
max_pending_reliable_packets: None,
});
let connect_packet = storm_client.create_connect(0, Vec::new())?;
for _ in 0..12 {
storm.send_to(&cultnet_rs::encode_rudp_packet(&connect_packet)?, target)?;
server.poll_once()?;
}
storm.send_to(b"not a rudp packet", target)?;
server.poll_once()?;
send(
&mut real,
&CultNetMessage::DocumentPutRaw {
message_id: "after-storm".into(),
document: document("real", vec![9]),
},
)?;
for _ in 0..20 {
server.poll_once()?;
if sink.0.lock().unwrap().receipts.len() == 1 {
break;
}
}
assert_eq!(sink.0.lock().unwrap().receipts.len(), 1);
assert!(server.packets_dropped() > 0, "the storm and the stray are counted");
Ok(())
}