Ack Cut 1c: a new generation drops what the old one owed before the queue is asked for room

A bounded session whose queue still held an unacknowledged Accept for a peer
that vanished refused the next Connect; the old server-mode path reset before
accepting, so this would have been a regression. accept_connect and
create_connect now end the generation first in every runtime, and a test per
runtime holds a one-slot queue to it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 06:50:23 +02:00
commit 0abb8d4380
8 changed files with 100 additions and 12 deletions

View file

@ -531,11 +531,12 @@ class CultNetRudpSession:
self._fragment_sets_evicted = 0
def create_connect(self, now_ms: int = 0, payload: bytes = b"") -> CultNetRudpPacket:
self._ensure_reliable_capacity(1)
# A session that has had a peer starts a new generation: nothing it
# learned from that peer describes the one this Connect reaches.
# learned from that peer describes the one this Connect reaches, and
# what it still owed that peer no longer takes room in the queue.
if self._connect_sequence is not None:
self.reset_peer_state()
self._ensure_reliable_capacity(1)
self._ended = False
packet = self._create_packet(
CultNetRudpPacketType.CONNECT,
@ -576,8 +577,8 @@ class CultNetRudpSession:
# generation, forgets the peer and accepts a new one.
if self.connect_repeats(packet):
return self._answer_repeated_connect(packet, now_ms)
self._ensure_reliable_capacity(1)
self.reset_peer_state()
self._ensure_reliable_capacity(1)
self._seed_received(packet.sequence)
self._last_received_at_ms = now_ms
self._connect_sequence = packet.sequence

View file

@ -122,6 +122,26 @@ class CultNetRudpConnectGenerationTests(unittest.TestCase):
client.receive(accept, 1)
self.assertFalse(client.connected, "an Accept revived an abandoned Connect")
def test_a_full_queue_owed_to_a_vanished_peer_does_not_refuse_a_new_generation(self) -> None:
def bounded(initial_sequence: int) -> CultNetRudpSession:
return CultNetRudpSession(
CultNetRudpSessionOptions(
connection_id=CONNECTION_ID, initial_sequence=initial_sequence, max_pending_reliable_packets=1
)
)
server = bounded(500)
server.accept_connect(session(1).create_connect(0), 0)
self.assertEqual(server.outstanding_reliable_packet_count, 1, "the Accept is owed and never acknowledged")
following = session(9).create_connect(1)
server.accept_connect(following, 1)
self.assertTrue(server.connect_repeats(following))
client = bounded(1)
client.create_connect(0)
client.create_connect(1)
self.assertEqual(client.outstanding_reliable_packet_count, 1, "only the new Connect is owed")
def test_a_duplicate_accept_does_not_reseed_the_watermark(self) -> None:
client, server = session(1), session(500)
_, accept = handshake(client, server)

View file

@ -426,12 +426,13 @@ impl CultNetRudpSession {
}
pub fn create_connect(&mut self, now_ms: u64, payload: Vec<u8>) -> Result<CultNetRudpPacket> {
self.ensure_reliable_capacity(1)?;
// A session that has had a peer starts a new generation: nothing it
// learned from that peer describes the one this Connect reaches.
// learned from that peer describes the one this Connect reaches, and
// what it still owed that peer no longer takes room in the queue.
if self.connect_sequence.is_some() {
self.reset_peer_state();
}
self.ensure_reliable_capacity(1)?;
let packet = self.create_packet(
CultNetRudpPacketType::Connect,
"control",
@ -486,8 +487,8 @@ impl CultNetRudpSession {
.unwrap_or_else(|| self.create_ack()));
}
self.ensure_reliable_capacity(1)?;
self.reset_peer_state();
self.ensure_reliable_capacity(1)?;
self.seed_received(packet.sequence);
self.last_received_at_ms = Some(now_ms);
self.connect_sequence = Some(packet.sequence);

View file

@ -215,6 +215,32 @@ fn an_accept_for_an_abandoned_connect_does_not_connect_the_session() -> Result<(
Ok(())
}
/// What a session still owed a vanished peer does not keep the next peer out:
/// a new generation drops it before the queue is asked for room.
#[test]
fn a_full_queue_owed_to_a_vanished_peer_does_not_refuse_a_new_generation() -> Result<()> {
let bounded = |initial_sequence: u32| {
CultNetRudpSession::new(CultNetRudpSessionOptions {
connection_id: CONNECTION_ID,
initial_sequence,
resend_delay_ms: 250,
max_pending_reliable_packets: Some(1),
})
};
let mut server = bounded(500);
server.accept_connect(&session(1).create_connect(0, Vec::new())?, 0, Vec::new())?;
assert_eq!(server.outstanding_reliable_packet_count(), 1, "the Accept is owed and never acknowledged");
let next = session(9).create_connect(1, Vec::new())?;
server.accept_connect(&next, 1, Vec::new())?;
assert!(server.connect_repeats(&next));
let mut client = bounded(1);
client.create_connect(0, Vec::new())?;
client.create_connect(1, Vec::new())?;
assert_eq!(client.outstanding_reliable_packet_count(), 1, "only the new Connect is owed");
Ok(())
}
/// An Accept counts once: a duplicate of the one honoured does not seed the
/// watermark again.
#[test]

View file

@ -281,12 +281,13 @@ export class CultNetRudpSession {
}
createConnect(nowMs = 0, payload = new Uint8Array()): CultNetRudpPacket {
this.#ensureReliableCapacity(1);
// A session that has had a peer starts a new generation: nothing it learned
// from that peer describes the one this Connect reaches.
// from that peer describes the one this Connect reaches, and what it still
// owed that peer no longer takes room in the queue.
if (this.#connectSequence !== undefined) {
this.resetPeerState();
}
this.#ensureReliableCapacity(1);
this.#ended = false;
const packet = this.#createPacket({
packetType: "connect",
@ -324,8 +325,8 @@ export class CultNetRudpSession {
if (this.connectRepeats(packet)) {
return this.#answerRepeatedConnect(packet, nowMs);
}
this.#ensureReliableCapacity(1);
this.resetPeerState();
this.#ensureReliableCapacity(1);
this.#seedReceived(packet.sequence);
this.#lastReceivedAtMs = nowMs;
this.#connectSequence = packet.sequence;

View file

@ -117,6 +117,21 @@ test("an Accept for an abandoned Connect does not connect the session", () => {
assert.equal(client.connected, false, "an Accept revived an abandoned Connect");
});
test("a full queue owed to a vanished peer does not refuse a new generation", () => {
const bounded = (initialSequence: number) => new CultNetRudpSession({ connectionId, initialSequence, maxPendingReliablePackets: 1 });
const server = bounded(500);
server.acceptConnect(session(1).createConnect(0), 0);
assert.equal(server.outstandingReliablePacketCount, 1, "the Accept is owed and never acknowledged");
const next = session(9).createConnect(1);
server.acceptConnect(next, 1);
assert.equal(server.connectRepeats(next), true);
const client = bounded(1);
client.createConnect(0);
client.createConnect(1);
assert.equal(client.outstandingReliablePacketCount, 1, "only the new Connect is owed");
});
test("a duplicate Accept does not reseed the watermark", () => {
const client = session(1);
const server = session(500);

View file

@ -1026,11 +1026,12 @@ namespace GameCult.Networking
/// </summary>
public CultNetRudpPacket CreateConnect(long nowMs = 0, byte[]? payload = null)
{
EnsureReliableCapacity(1);
// A session that has had a peer starts a new generation: nothing it learned from that
// peer describes the one this Connect reaches.
// peer describes the one this Connect reaches, and what it still owed that peer no
// longer takes room in the queue.
if (_connectSequence.HasValue)
ResetPeerState();
EnsureReliableCapacity(1);
_ended = false;
var packet = CreatePacket(CultNetRudpPacketType.Connect, "control", payload ?? Array.Empty<byte>(), reliable: true, ordered: true, sequenced: false);
_connectSequence = packet.Sequence;
@ -1069,8 +1070,8 @@ namespace GameCult.Networking
if (ConnectRepeats(packet))
return AnswerRepeatedConnect(packet, nowMs);
EnsureReliableCapacity(1);
ResetPeerState();
EnsureReliableCapacity(1);
SeedReceived(packet.Sequence);
_connectSequence = packet.Sequence;
_connected = true;

View file

@ -178,6 +178,29 @@ namespace GameCult.Networking.Tests
Assert.That(client.Connected, Is.False, "an Accept revived an abandoned Connect");
}
[Test]
public void AFullQueueOwedToAVanishedPeerDoesNotRefuseANewGeneration()
{
static CultNetRudpSession Bounded(uint initialSequence) => new CultNetRudpSession(new CultNetRudpSessionOptions
{
ConnectionId = ConnectionId,
InitialSequence = initialSequence,
MaxPendingReliablePackets = 1
});
var server = Bounded(500);
server.AcceptConnect(Session(1).CreateConnect(0), 0);
Assert.That(server.OutstandingReliablePacketCount, Is.EqualTo(1), "the Accept is owed and never acknowledged");
var next = Session(9).CreateConnect(1);
server.AcceptConnect(next, 1);
Assert.That(server.ConnectRepeats(next), Is.True);
var client = Bounded(1);
client.CreateConnect(0);
client.CreateConnect(1);
Assert.That(client.OutstandingReliablePacketCount, Is.EqualTo(1), "only the new Connect is owed");
}
[Test]
public void ADuplicateAcceptDoesNotReseedTheWatermark()
{