Skip to content

Commit 9ca4efb

Browse files
fix: shutdown causes exception if invoked twice [MTT-6752] (#2622)
* fix This fixes the issue with shutdown throwing an exception when being invoked again within the shutdown callback. * test Added test to validate the fix
1 parent b8eed68 commit 9ca4efb

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

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

1616
- Fixed issue where `NetworkObject.SpawnWithObservers` was not being honored for late joining clients. (#2623)
17+
- Fixed issue where invoking `NetworkManager.Shutdown` multiple times, depending upon the timing, could cause an exception. (#2622)
1718

1819
## Changed
1920

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,16 @@ public void Shutdown(bool discardMessageQueue = false)
942942
if (IsServer || IsClient)
943943
{
944944
m_ShuttingDown = true;
945-
MessageManager.StopProcessing = discardMessageQueue;
945+
if (MessageManager != null)
946+
{
947+
MessageManager.StopProcessing = discardMessageQueue;
948+
}
946949
}
947950

948-
NetworkConfig.NetworkTransport.OnTransportEvent -= ConnectionManager.HandleNetworkEvent;
951+
if (NetworkConfig != null && NetworkConfig.NetworkTransport != null)
952+
{
953+
NetworkConfig.NetworkTransport.OnTransportEvent -= ConnectionManager.HandleNetworkEvent;
954+
}
949955
}
950956

951957
// Ensures that the NetworkManager is cleaned up before OnDestroy is run on NetworkObjects and NetworkBehaviours when unloading a scene with a NetworkManager

testproject/Assets/Tests/Runtime/NetworkManagerTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Unity.Netcode.TestHelpers.Runtime;
55
using UnityEngine;
66
using UnityEngine.SceneManagement;
7+
using UnityEngine.TestTools;
78

89
namespace TestProject.RuntimeTests
910
{
@@ -94,5 +95,25 @@ public void ValidateHostSettings()
9495
Assert.IsTrue(m_NumberOfTimesInvoked == 1, $"OnClientConnectedCallback was invoked {m_NumberOfTimesInvoked} as opposed to just once!");
9596
Assert.IsTrue(m_NetworkBehaviourIsServerWasSet, $"IsServer was not true when OnClientConnectedCallback was invoked!");
9697
}
98+
99+
/// <summary>
100+
/// Validate shutting down a second time does not cause an exception.
101+
/// </summary>
102+
[UnityTest]
103+
public IEnumerator ValidateShutdown()
104+
{
105+
// Register for the server stopped notification so we know we have shutdown completely
106+
m_ServerNetworkManager.OnServerStopped += M_ServerNetworkManager_OnServerStopped;
107+
// Shutdown
108+
m_ServerNetworkManager.Shutdown();
109+
yield return s_DefaultWaitForTick;
110+
}
111+
112+
private void M_ServerNetworkManager_OnServerStopped(bool obj)
113+
{
114+
m_ServerNetworkManager.OnServerStopped -= M_ServerNetworkManager_OnServerStopped;
115+
// Verify that we can invoke shutdown again without an exception
116+
m_ServerNetworkManager.Shutdown();
117+
}
97118
}
98119
}

0 commit comments

Comments
 (0)