C#: a peer's undecodable message ends that peer's session, never the poll
RudpCultNetSchemaServer decoded each schema frame outside its per-message backstop, so a malformed payload (0xc1) or a well-formed message with an unknown schemaVersion threw MessagePackSerializationException out of PollAvailableAsync and PollOnceAsync. The drain aborted: a good peer's request in the same drain went unserved, a consumer loop that did not catch died, and the bad frame was already acknowledged while its peer stayed connected. Decoding now runs inside the backstop. A peer whose message cannot be decoded or handled has its session ended: the transport's new EndRefusedPeer sends the refusal goodbye under the gate, counts the drop in PacketsDropped, drops that peer's frames still queued, forgets the peer and raises PeerDisconnected. A session that already ended (replaced by a new Connect, or ended by a send failure) gets no goodbye, since the endpoint may belong to a new session. Other peers in the drain are still served. Ending the session matches cultmesh-py's server, where one peer's bad message ends that peer; runtime parity is the reason. The log names the fault's type only and never its message, because the decoder's message quotes the peer's input (the unknown schemaVersion). The dispatch-backstop test now asserts that rule instead of the handler message it used to require. Tests, each failing on the pre-fix code: a malformed payload and an unknown schemaVersion each leave the good peer served without Poll throwing (also through PollOnceAsync); the bad peer's session ends, counted and told; the log carries the type and not the canary value. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3b9ffac2fe
commit
a44e189e0c
4 changed files with 248 additions and 21 deletions
|
|
@ -288,23 +288,22 @@ namespace GameCult.Networking
|
|||
if (!string.Equals(delivered.Frame.ChannelId, "schema", StringComparison.Ordinal))
|
||||
return false;
|
||||
|
||||
var message = CultNetSchemaMessageSerialization.Deserialize(delivered.Frame.Payload);
|
||||
if (!_handlers.TryGetValue(message.GetType(), out var handler) || handler == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// R-AG: a handler is expected to catch its own typed refusals (HandleSnapshotRequestV1Async
|
||||
// catches the three selection exceptions and answers cursor_invalid/selection_invalid/
|
||||
// reference_outside_target on the wire, R-AM) - this catch is the backstop for whatever a
|
||||
// handler does not, so an untyped exception from a malformed/hostile message can never
|
||||
// reach the poll loop and stall the server for every other peer. It still answers nothing
|
||||
// on the wire (an exception this generic carries no typed refusal to report) and drops the
|
||||
// one malformed dispatch, exactly as an unregistered handler already does just above -
|
||||
// R-AM: but it always logs. A backstop that swallows silently is a compensator, and an
|
||||
// untested, unlogged one hid the SM-6 ArgumentNullException regression for a whole batch.
|
||||
// A peer's message that cannot be decoded (malformed bytes, an unknown schemaVersion) or
|
||||
// handled is that peer's failure, never the poll's: the drain goes on serving every other
|
||||
// peer. The transport already acknowledged the frame, so the peer's session cannot be kept
|
||||
// (a retransmit would never be handled). It ends, the peer is told, and the drop is counted
|
||||
// and logged, as cultmesh-py's server does. A handler is still expected to answer its own
|
||||
// typed refusals on the wire (HandleSnapshotRequestV1Async does, R-AM); this is the backstop
|
||||
// for whatever it does not. The log names the fault's type only: its message can quote
|
||||
// what the peer sent.
|
||||
try
|
||||
{
|
||||
var message = CultNetSchemaMessageSerialization.Deserialize(delivered.Frame.Payload);
|
||||
if (!_handlers.TryGetValue(message.GetType(), out var handler) || handler == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var result = handler.DynamicInvoke(message, new RudpCultNetSchemaServerPeer(this, delivered.Peer));
|
||||
if (result is Task task)
|
||||
{
|
||||
|
|
@ -313,11 +312,11 @@ namespace GameCult.Networking
|
|||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// DynamicInvoke wraps a synchronously-thrown handler fault in TargetInvocationException,
|
||||
// whose own .Message is the generic "Exception has been thrown by the target of an
|
||||
// invocation." - log the real fault, not that wrapper.
|
||||
// DynamicInvoke wraps a synchronously-thrown handler fault in TargetInvocationException;
|
||||
// name the real fault, not that wrapper.
|
||||
var reported = ex is System.Reflection.TargetInvocationException { InnerException: { } inner } ? inner : ex;
|
||||
Logger.LogError($"CultNet RUDP schema dispatch failed for {message.GetType().Name}: {reported.Message}");
|
||||
Logger.LogError($"CultNet RUDP schema server could not decode or handle a peer's message ({reported.GetType().Name}); that peer's session ended.");
|
||||
_transport.EndRefusedPeer(delivered.Peer);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -3188,6 +3188,35 @@ namespace GameCult.Networking
|
|||
_unsendablePeers.Enqueue(peer);
|
||||
}
|
||||
|
||||
// Polling thread only. Ends the session of a peer whose delivered frame the application could not
|
||||
// decode or handle. The frame is already acknowledged, so the session cannot be kept: a
|
||||
// retransmit would never be handled. The peer is told, the drop is counted, and the peer's
|
||||
// frames still queued are not delivered. A session that already ended (a new Connect replaced
|
||||
// it, or a send failure ended it) owes no goodbye; the endpoint may belong to a new session.
|
||||
internal void EndRefusedPeer(CultNetRudpSocketServerPeer peer)
|
||||
{
|
||||
if (peer == null) throw new ArgumentNullException(nameof(peer));
|
||||
_stats.PacketsDropped++;
|
||||
lock (peer.SessionGate)
|
||||
{
|
||||
if (peer.Session.Ended)
|
||||
return;
|
||||
SendPacket(peer.RemoteEndPoint, peer.Session.EndRefused());
|
||||
}
|
||||
|
||||
var kept = _deliveredFrames.Where(frame => !ReferenceEquals(frame.Peer, peer)).ToArray();
|
||||
_deliveredFrames.Clear();
|
||||
foreach (var frame in kept)
|
||||
_deliveredFrames.Enqueue(frame);
|
||||
|
||||
var key = RemoteKey(peer.RemoteEndPoint);
|
||||
if (!_peers.TryGetValue(key, out var current) || !ReferenceEquals(current, peer))
|
||||
return;
|
||||
_peers.Remove(key);
|
||||
peer.DisconnectReason = CultNetRudpSession.RefusedPacketReason;
|
||||
PeerDisconnected?.Invoke(peer);
|
||||
}
|
||||
|
||||
// Polling thread only. A peer a new Connect already replaced was reported then, and its key now
|
||||
// names the peer that replaced it.
|
||||
private void RetireUnsendablePeers()
|
||||
|
|
|
|||
Loading…
Reference in a new issue