From b0783879cd68c6881b5b32e0d32a5e725f5895ab Mon Sep 17 00:00:00 2001 From: James Frowen Date: Fri, 3 May 2024 15:07:55 +0100 Subject: [PATCH] fix(SocketLayer): fixing socket receive trying to handle message when length is negative UDP socket returns -1 when there is socket error. mirage would pass this -1 into metrics and try to handle the message. Instead Mirage should just ignore these messages --- Assets/Mirage/Runtime/SocketLayer/Peer.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Assets/Mirage/Runtime/SocketLayer/Peer.cs b/Assets/Mirage/Runtime/SocketLayer/Peer.cs index ed05b2e32d..16ec8d44e8 100644 --- a/Assets/Mirage/Runtime/SocketLayer/Peer.cs +++ b/Assets/Mirage/Runtime/SocketLayer/Peer.cs @@ -242,9 +242,15 @@ private void ReceiveLoop() { using (var buffer = _bufferPool.Take()) { - while (_socket.Poll()) + // check active, because socket might have been closed by message handler + while (_active && _socket.Poll()) { var length = _socket.Receive(buffer.array, out var receiveEndPoint); + if (length < 0) + { + _logger.Log(LogType.Warning, $"Receive returned less than 0 bytes, length={length}"); + continue; + } // this should never happen. buffer size is only MTU, if socket returns higher length then it has a bug. if (length > _maxPacketSize) @@ -262,9 +268,6 @@ private void ReceiveLoop() _metrics?.OnReceiveUnconnected(length); HandleNewConnection(receiveEndPoint, packet); } - - // socket might have been closed by message handler - if (!_active) { break; } } } }