Skip to content

Commit 59a973c

Browse files
authored
fix: catching potential exceptions from user code during disconnect callback (#2288)
* fix: catching potential exceptions from user code during disconnect callback
1 parent b4b3929 commit 59a973c

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,14 @@ private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, A
18311831
// or, if we are the server, so we got everything from that client.
18321832
MessagingSystem.ProcessIncomingMessageQueue();
18331833

1834-
OnClientDisconnectCallback?.Invoke(clientId);
1834+
try
1835+
{
1836+
OnClientDisconnectCallback?.Invoke(clientId);
1837+
}
1838+
catch (Exception exception)
1839+
{
1840+
Debug.LogException(exception);
1841+
}
18351842

18361843
if (IsServer)
18371844
{
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections;
3+
using System.Text.RegularExpressions;
4+
using UnityEngine;
5+
using UnityEngine.TestTools;
6+
using Unity.Netcode.TestHelpers.Runtime;
7+
8+
namespace Unity.Netcode.RuntimeTests
9+
{
10+
public class DisconnectReasonObject : NetworkBehaviour
11+
{
12+
13+
}
14+
15+
public class DisconnectReasonTests : NetcodeIntegrationTest
16+
{
17+
protected override int NumberOfClients => 2;
18+
19+
private GameObject m_PrefabToSpawn;
20+
21+
protected override void OnServerAndClientsCreated()
22+
{
23+
m_PrefabToSpawn = CreateNetworkObjectPrefab("DisconnectReasonObject");
24+
m_PrefabToSpawn.AddComponent<DisconnectReasonObject>();
25+
}
26+
27+
private int m_DisconnectCount;
28+
29+
public void OnClientDisconnectCallback(ulong clientId)
30+
{
31+
m_DisconnectCount++;
32+
throw new SystemException("whatever");
33+
}
34+
35+
[UnityTest]
36+
public IEnumerator DisconnectExceptionTest()
37+
{
38+
float startTime = Time.realtimeSinceStartup;
39+
40+
// Add a callback for first client, when they get disconnected
41+
m_ClientNetworkManagers[0].OnClientDisconnectCallback += OnClientDisconnectCallback;
42+
m_ClientNetworkManagers[1].OnClientDisconnectCallback += OnClientDisconnectCallback;
43+
44+
// Disconnect first client, from the server
45+
LogAssert.Expect(LogType.Exception, new Regex(".*whatever.*"));
46+
m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[0].LocalClientId);
47+
48+
// Disconnect second client, from the server
49+
LogAssert.Expect(LogType.Exception, new Regex(".*whatever.*"));
50+
m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[1].LocalClientId);
51+
52+
while (m_DisconnectCount < 2 && Time.realtimeSinceStartup < startTime + 10.0f)
53+
{
54+
yield return null;
55+
}
56+
57+
Debug.Assert(m_DisconnectCount == 2);
58+
}
59+
}
60+
}

com.unity.netcode.gameobjects/Tests/Runtime/DisconnectReasonTests.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)