Skip to content

Commit e4d5166

Browse files
fix: Client-Side NetworkEvent.Disconnect can throw an exception in UnityTransport (#1884)
* fix MTT-3359 The fix for the bug. * test The integration test to verify the fix * style * style and update * Update CHANGELOG.md
1 parent 06f378f commit e4d5166

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
8+
## Unreleased
9+
10+
### Added
11+
12+
### Changed
13+
14+
### Removed
15+
16+
### Fixed
17+
- Fixed client throwing an exception if it has messages in the outbound queue when processing the NetworkEvent.Disconnect event and is using UTP. (#1884)
818

919
## [1.0.0-pre.7] - 2022-04-06
1020

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,11 @@ private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, A
14461446
}
14471447
else
14481448
{
1449-
Shutdown();
1449+
// We must pass true here and not process any sends messages
1450+
// as we are no longer connected and thus there is no one to
1451+
// send any messages to and this will cause an exception within
1452+
// UnityTransport as the client ID is no longer valid.
1453+
Shutdown(true);
14501454
}
14511455
#if DEVELOPMENT_BUILD || UNITY_EDITOR
14521456
s_TransportDisconnect.End();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Collections;
2+
using NUnit.Framework;
3+
using UnityEngine;
4+
using UnityEngine.TestTools;
5+
using Unity.Netcode;
6+
using Unity.Netcode.TestHelpers.Runtime;
7+
8+
9+
namespace TestProject.RuntimeTests
10+
{
11+
public class ServerDisconnectsClientTest : NetcodeIntegrationTest
12+
{
13+
protected override int NumberOfClients => 1;
14+
protected override void OnCreatePlayerPrefab()
15+
{
16+
m_PlayerPrefab.AddComponent<ClientSendRpcUponDisconnect>();
17+
base.OnCreatePlayerPrefab();
18+
}
19+
20+
[UnityTest]
21+
public IEnumerator ServerDisconnectsClient()
22+
{
23+
m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[0].LocalClientId);
24+
yield return WaitForConditionOrTimeOut(() => !m_ClientNetworkManagers[0].IsConnectedClient && !m_ClientNetworkManagers[0].IsListening);
25+
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for client to disconnect!");
26+
}
27+
public class ClientSendRpcUponDisconnect : NetworkBehaviour
28+
{
29+
public override void OnNetworkSpawn()
30+
{
31+
if (!IsServer)
32+
{
33+
NetworkManager.OnClientDisconnectCallback += NetworkManager_OnClientDisconnectCallback;
34+
}
35+
base.OnNetworkSpawn();
36+
}
37+
38+
[ServerRpc(RequireOwnership = false)]
39+
public void ClientToServerRpc()
40+
{
41+
Debug.Log($"Received {nameof(ClientToServerRpc)}");
42+
}
43+
44+
private void NetworkManager_OnClientDisconnectCallback(ulong obj)
45+
{
46+
NetworkManager.OnClientDisconnectCallback -= NetworkManager_OnClientDisconnectCallback;
47+
// To simulate that there were already messages to be sent this frame in the send queue,
48+
// we just send a few packets to the server even though we are considered disconnected
49+
// at this point.
50+
for (int i = 0; i < 5; i++)
51+
{
52+
ClientToServerRpc();
53+
}
54+
}
55+
}
56+
}
57+
}

testproject/Assets/Tests/Runtime/ServerDisconnectsClientTest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)