Skip to content

Commit 78cad42

Browse files
fix: Don't accept invalid connections in UnityTransport.Send (#3382)
This avoids the error message reported in [MTTB-1143](https://jira.unity3d.com/browse/MTTB-1143). The `UnityTransport.Send` method would happily accept invalid or stale connections, which would lead to the allocation of a `BatchedSendQueue` structure and to an error message when the batched sends are passed on to the driver. In MTTB-1143 we'd get a send on client ID 0, but that's not a valid client ID in `UnityTransport`. My guess is that there's something somewhere (possibly in Boss Room) triggering a send to the server after the connection has closed (which reverts `UnityTransport.ServerClientId` to its default value of 0). Of course, ideally there'd never be such a send-after-disconnect and that's what should be ultimately addressed. But the bug is quite hard to reproduce, so as a stopgap until we have something better I'm making a fix that only avoids the last and more user-visible consequences of the issue.
1 parent 53b94ca commit 78cad42

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2222

2323
### Fixed
2424

25+
- 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)
2526
- 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)
2627
- 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)
2728
- 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)

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,8 +1347,13 @@ public override NetcodeNetworkEvent PollEvent(out ulong clientId, out ArraySegme
13471347
/// <param name="networkDelivery">The delivery type (QoS) to send data with</param>
13481348
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
13491349
{
1350-
var pipeline = SelectSendPipeline(networkDelivery);
1350+
var connection = ParseClientId(clientId);
1351+
if (!m_Driver.IsCreated || m_Driver.GetConnectionState(connection) != NetworkConnection.State.Connected)
1352+
{
1353+
return;
1354+
}
13511355

1356+
var pipeline = SelectSendPipeline(networkDelivery);
13521357
if (pipeline != m_ReliableSequencedPipeline && payload.Count > m_MaxPayloadSize)
13531358
{
13541359
Debug.LogError($"Unreliable payload of size {payload.Count} larger than configured 'Max Payload Size' ({m_MaxPayloadSize}).");

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,5 +483,22 @@ public IEnumerator DoesNotActAfterShutdown([Values] AfterShutdownAction afterShu
483483
yield return EnsureNoNetworkEvent(m_Client1Events);
484484
}
485485
}
486+
487+
[UnityTest]
488+
public IEnumerator DoesNotAttemptToSendOnInvalidConnections()
489+
{
490+
InitializeTransport(out m_Server, out m_ServerEvents);
491+
InitializeTransport(out m_Client1, out m_Client1Events);
492+
493+
m_Server.StartServer();
494+
m_Client1.StartClient();
495+
496+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
497+
498+
var data = new ArraySegment<byte>(new byte[42]);
499+
m_Server.Send(0, data, NetworkDelivery.Reliable);
500+
501+
yield return EnsureNoNetworkEvent(m_Client1Events);
502+
}
486503
}
487504
}

0 commit comments

Comments
 (0)