From 7ebb7bc89b3deea2144ce538ef507af53575515f Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Fri, 4 Apr 2025 15:15:52 -0400 Subject: [PATCH 1/4] fix: Don't accept invalid connections in UnityTransport.Send --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + .../Runtime/Transports/UTP/UnityTransport.cs | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 74718c95b9..07e89d010b 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- Fixed an issue in `UnityTransport` where the transport would accept sends on invalid connections, leading to a useless memory allocation and confusing error message. - Fixed issue where the time delta that interpolators used would not be properly updated during multiple fixed update invocations within the same player loop frame. (#3355) - Fixed issue when using a distributed authority network topology and many clients attempt to connect simultaneously the session owner could max-out the maximum in-flight reliable messages allowed, start dropping packets, and some of the connecting clients would fail to fully synchronize. (#3350) - Fixed issue when using a distributed authority network topology and scene management was disabled clients would not be able to spawn any new network prefab instances until synchronization was complete. (#3350) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index 3bd74f7aab..da3b797aea 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -1347,8 +1347,13 @@ public override NetcodeNetworkEvent PollEvent(out ulong clientId, out ArraySegme /// The delivery type (QoS) to send data with public override void Send(ulong clientId, ArraySegment payload, NetworkDelivery networkDelivery) { - var pipeline = SelectSendPipeline(networkDelivery); + var connection = ParseClientId(clientId); + if (!m_Driver.IsCreated || m_Driver.GetConnectionState(connection) != NetworkConnection.State.Connected) + { + return; + } + var pipeline = SelectSendPipeline(networkDelivery); if (pipeline != m_ReliableSequencedPipeline && payload.Count > m_MaxPayloadSize) { Debug.LogError($"Unreliable payload of size {payload.Count} larger than configured 'Max Payload Size' ({m_MaxPayloadSize})."); From 4aeb3fa199afa7b6fec264cd0bd03023e45795e9 Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Fri, 4 Apr 2025 15:24:33 -0400 Subject: [PATCH 2/4] Add PR number to CHANGELOG entry --- com.unity.netcode.gameobjects/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 07e89d010b..480152dc66 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -22,7 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed -- Fixed an issue in `UnityTransport` where the transport would accept sends on invalid connections, leading to a useless memory allocation and confusing error message. +- Fixed an issue in `UnityTransport` where the transport would accept sends on invalid connections, leading to a useless memory allocation and confusing error message. (#3382) - Fixed issue where the time delta that interpolators used would not be properly updated during multiple fixed update invocations within the same player loop frame. (#3355) - Fixed issue when using a distributed authority network topology and many clients attempt to connect simultaneously the session owner could max-out the maximum in-flight reliable messages allowed, start dropping packets, and some of the connecting clients would fail to fully synchronize. (#3350) - Fixed issue when using a distributed authority network topology and scene management was disabled clients would not be able to spawn any new network prefab instances until synchronization was complete. (#3350) From 3e5d4dfe418d3ff808503b8a73a272543cd91abc Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Mon, 7 Apr 2025 10:52:33 -0400 Subject: [PATCH 3/4] Add test --- .../Runtime/Transports/UnityTransportTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs index 4cd33fcc9b..f25696e4ab 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs @@ -483,5 +483,22 @@ public IEnumerator DoesNotActAfterShutdown([Values] AfterShutdownAction afterShu yield return EnsureNoNetworkEvent(m_Client1Events); } } + + [UnityTest] + public IEnumerator DoesNotAttemptToSendOnInvalidConnections() + { + InitializeTransport(out m_Server, out m_ServerEvents); + InitializeTransport(out m_Client1, out m_Client1Events); + + m_Server.StartServer(); + m_Client1.StartClient(); + + yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events); + + var data = new ArraySegment(new byte[42]); + m_Server.Send(m_Client1.ServerClientId, data, NetworkDelivery.Reliable); + + yield return EnsureNoNetworkEvent(m_Client1Events); + } } } From 6c57f8bc9ff797bdd03b1bafa732a9bb3f4e421d Mon Sep 17 00:00:00 2001 From: Simon Lemay Date: Mon, 7 Apr 2025 11:14:23 -0400 Subject: [PATCH 4/4] Fix test case --- .../Tests/Runtime/Transports/UnityTransportTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs index f25696e4ab..ba68c4147f 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs @@ -496,7 +496,7 @@ public IEnumerator DoesNotAttemptToSendOnInvalidConnections() yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events); var data = new ArraySegment(new byte[42]); - m_Server.Send(m_Client1.ServerClientId, data, NetworkDelivery.Reliable); + m_Server.Send(0, data, NetworkDelivery.Reliable); yield return EnsureNoNetworkEvent(m_Client1Events); }