C#: a send failure's goodbye belongs to the generation that sent

Send ended an unsendable session after releasing SessionGate, and the
goodbye was built under the gate but sent outside it. If the polling
thread admitted a new Connect from the same endpoint in that window, the
ended generation's goodbye reached the new client: it saw Data, Accept,
Disconnect and believed itself disconnected while the listener held it
connected.

Every path that finds a permanent send failure now reads the session's
generation under the gate at the send, and EndUnsendableSession builds
and sends the goodbye under the gate only while that generation is
unchanged. The generation check replaces the UnsendableReason once-guard:
a second failure in the same generation finds it already ended.

The test orders the new Connect inside the send, before the fragment
that can never be sent, and asserts the new client hears no Disconnect
and stays connected on both sides. It fails on the pre-fix code
(Data, Accept, Disconnect) and kills the mutant that drops the check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 17:23:30 +02:00
commit 3b9ffac2fe
2 changed files with 68 additions and 13 deletions

View file

@ -313,6 +313,47 @@ namespace GameCult.Networking.Tests
}
}
// A send that fails after its first fragment ends the generation it began in, and only that
// one. Here X connects again while the send is under way, and the polling thread admits the
// new Connect before the fragment that can never be sent. The hook runs that poll inside the
// send so the order is fixed; in production it runs on the polling thread between the send
// and the failure's handling. The goodbye belonged to the ended generation, and the client
// must not hear it: it would believe itself disconnected while the listener holds it
// connected.
[Test]
public void ASendFailureAfterANewConnectReplacedThePeerSaysNoGoodbyeToTheNewSession()
{
var (server, listenerEndPoint, x, _) = TwoPeers(maxFragmentBytes: 1000);
using (server)
{
var stale = x.Server;
Drain(x.Socket);
x.Session = new CultNetRudpSession(new CultNetRudpSessionOptions { ConnectionId = ConnectionId });
x.Socket.SendTo(CultNetRudpPacketCodec.Encode(x.Session.CreateConnect(0)), listenerEndPoint);
var fragments = 0;
server.BeforeSend = (remote, packet) =>
{
if (!remote.Equals(x.EndPoint) || packet.PacketType != CultNetRudpPacketType.Data || ++fragments < 2)
return;
server.BeforeSend = null;
server.ReceiveOnce();
server.UnsendableAfter[x.EndPoint] = 0;
};
Assert.Throws<SocketException>(() => server.SendSchema(stale, new byte[3000]));
server.ReceiveOnce();
var packets = Drain(x.Socket);
Assert.That(packets.Select(p => p.PacketType), Has.None.EqualTo(CultNetRudpPacketType.Disconnect),
"the ended generation's goodbye reached the session that replaced it");
x.Session.Receive(packets.Single(p => p.PacketType == CultNetRudpPacketType.Accept), 0);
Assert.That(x.Session.Connected, Is.True);
var replacing = server.Peers.Single(p => p.RemoteEndPoint.Equals(x.EndPoint));
Assert.That(replacing, Is.Not.SameAs(stale));
Assert.That(replacing.Connected, Is.True);
}
}
[Test]
public void ASessionEndedByACallerAndThePollingThreadTogetherSaysGoodbyeOnce()
{