Skip to content

Commit 4ee8da0

Browse files
fix: NetworkManager.ShutdownInProgress not being reset (#2661)
* fix This resolves the issue where the NetworkManager.m_ShuttingDown property could be set to true again if the shutdown method is invoked within OnClientStopped or OnServerStopped. * test This validates that if you invoke shutdown within OnClientStopped or OnServerStopped that NetworkManager.ShutdownInProgress remains false once completing the shutdown process. * update Adding the changelog entry for this fix. Making the changelog entry for v1.6.0 branch match #2657 * Update CHANGELOG.md Removing the adjusted changelog entry
1 parent 960b58a commit 4ee8da0

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
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

1515
### Fixed
1616

17+
- Fixed issue where invoking `NetworkManager.Shutdown` within `NetworkManager.OnClientStopped` or `NetworkManager.OnServerStopped` would force `NetworkManager.ShutdownInProgress` to remain true after completing the shutdown process. (#2661)
1718
- Fixed issue with client synchronization of position when using half precision and the delta position reaches the maximum value and is collapsed on the host prior to being forwarded to the non-owner clients. (#2636)
1819
- Fixed issue with scale not synchronizing properly depending upon the spawn order of NetworkObjects. (#2636)
1920
- Fixed issue position was not properly transitioning between ownership changes with an owner authoritative NetworkTransform. (#2636)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,9 @@ internal void ShutdownInternal()
10131013
OnServerStopped?.Invoke(ConnectionManager.LocalClient.IsClient);
10141014
}
10151015

1016+
// In the event shutdown is invoked within OnClientStopped or OnServerStopped, set it to false again
1017+
m_ShuttingDown = false;
1018+
10161019
// Reset the client's roles
10171020
ConnectionManager.LocalClient.SetRole(false, false);
10181021

testproject/Assets/Tests/Runtime/NetworkManagerTests.cs

Lines changed: 81 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Linq;
23
using NUnit.Framework;
34
using Unity.Netcode;
45
using Unity.Netcode.TestHelpers.Runtime;
@@ -40,12 +41,15 @@ public NetworkManagerTests(UseSceneManagement useSceneManagement)
4041

4142
private void OnClientConnectedCallback(NetworkObject networkObject, int numberOfTimesInvoked, bool isHost, bool isClient, bool isServer)
4243
{
43-
m_NetworkObject = networkObject;
44-
m_NetworkObjectWasSpawned = networkObject.IsSpawned;
45-
m_NetworkBehaviourIsHostWasSet = isHost;
46-
m_NetworkBehaviourIsClientWasSet = isClient;
47-
m_NetworkBehaviourIsServerWasSet = isServer;
48-
m_NumberOfTimesInvoked = numberOfTimesInvoked;
44+
if (networkObject != null)
45+
{
46+
m_NetworkObject = networkObject;
47+
m_NetworkObjectWasSpawned = networkObject.IsSpawned;
48+
m_NetworkBehaviourIsHostWasSet = isHost;
49+
m_NetworkBehaviourIsClientWasSet = isClient;
50+
m_NetworkBehaviourIsServerWasSet = isServer;
51+
m_NumberOfTimesInvoked = numberOfTimesInvoked;
52+
}
4953
}
5054

5155
private bool TestComponentFound()
@@ -96,22 +100,86 @@ public void ValidateHostSettings()
96100
Assert.IsTrue(m_NetworkBehaviourIsServerWasSet, $"IsServer was not true when OnClientConnectedCallback was invoked!");
97101
}
98102

103+
public enum ShutdownChecks
104+
{
105+
Server,
106+
Client
107+
}
108+
109+
protected override void OnNewClientCreated(NetworkManager networkManager)
110+
{
111+
networkManager.NetworkConfig.EnableSceneManagement = m_EnableSceneManagement;
112+
foreach (var prefab in m_ServerNetworkManager.NetworkConfig.Prefabs.Prefabs)
113+
{
114+
networkManager.NetworkConfig.Prefabs.Add(prefab);
115+
}
116+
base.OnNewClientCreated(networkManager);
117+
}
118+
99119
/// <summary>
100120
/// Validate shutting down a second time does not cause an exception.
101121
/// </summary>
102122
[UnityTest]
103-
public IEnumerator ValidateShutdown()
123+
public IEnumerator ValidateShutdown([Values] ShutdownChecks shutdownCheck)
104124
{
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();
125+
126+
127+
if (shutdownCheck == ShutdownChecks.Server)
128+
{
129+
// Register for the server stopped notification so we know we have shutdown completely
130+
m_ServerNetworkManager.OnServerStopped += OnServerStopped;
131+
// Shutdown
132+
m_ServerNetworkManager.Shutdown();
133+
}
134+
else
135+
{
136+
// For this test (simplify the complexity) with a late joining client, just remove the
137+
// in-scene placed NetworkObject prior to the client connecting
138+
// (We are testing the shutdown sequence)
139+
var spawnedObjects = m_ServerNetworkManager.SpawnManager.SpawnedObjectsList.ToList();
140+
141+
for (int i = spawnedObjects.Count - 1; i >= 0; i--)
142+
{
143+
var spawnedObject = spawnedObjects[i];
144+
if (spawnedObject.IsSceneObject != null && spawnedObject.IsSceneObject.Value)
145+
{
146+
spawnedObject.Despawn();
147+
}
148+
}
149+
150+
yield return s_DefaultWaitForTick;
151+
152+
yield return CreateAndStartNewClient();
153+
154+
// Register for the server stopped notification so we know we have shutdown completely
155+
m_ClientNetworkManagers[0].OnClientStopped += OnClientStopped;
156+
m_ClientNetworkManagers[0].Shutdown();
157+
}
158+
159+
// Let the network manager instance shutdown
109160
yield return s_DefaultWaitForTick;
161+
162+
// Validate the shutdown is no longer in progress
163+
if (shutdownCheck == ShutdownChecks.Server)
164+
{
165+
Assert.False(m_ServerNetworkManager.ShutdownInProgress, $"[{shutdownCheck}] Shutdown in progress was still detected!");
166+
}
167+
else
168+
{
169+
Assert.False(m_ClientNetworkManagers[0].ShutdownInProgress, $"[{shutdownCheck}] Shutdown in progress was still detected!");
170+
}
171+
}
172+
173+
private void OnClientStopped(bool obj)
174+
{
175+
m_ServerNetworkManager.OnServerStopped -= OnClientStopped;
176+
// Verify that we can invoke shutdown again without an exception
177+
m_ServerNetworkManager.Shutdown();
110178
}
111179

112-
private void M_ServerNetworkManager_OnServerStopped(bool obj)
180+
private void OnServerStopped(bool obj)
113181
{
114-
m_ServerNetworkManager.OnServerStopped -= M_ServerNetworkManager_OnServerStopped;
182+
m_ServerNetworkManager.OnServerStopped -= OnServerStopped;
115183
// Verify that we can invoke shutdown again without an exception
116184
m_ServerNetworkManager.Shutdown();
117185
}

0 commit comments

Comments
 (0)