Skip to content

Commit 5c5566e

Browse files
fix: Make NetworkManager.Shutdown idempotent (#1877)
* Add a test for shutting down a NetworkManager twice * Don't set m_ShuttingDown if not running * Add CHANGELOG entry
1 parent 7414be3 commit 5c5566e

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ Additional documentation and release notes are available at [Multiplayer Documen
2020
- Fixed issue during client synchronization if 'ValidateSceneBeforeLoading' returned false it would halt the client synchronization process resulting in a client that was approved but not synchronized or fully connected with the server. (#1883)
2121
- Fixed an issue where UNetTransport.StartServer would return success even if the underlying transport failed to start (#854)
2222

23+
## [Unreleased]
24+
25+
### Removed
26+
- Removed `SIPTransport` (#1870)
27+
28+
### Fixed
29+
- Fixed an issue where calling `Shutdown` on a `NetworkManager` that was already shut down would cause an immediate shutdown the next time it was started (basically the fix makes `Shutdown` idempotent). (#1877)
30+
2331
## [1.0.0-pre.7] - 2022-04-06
2432

2533
### Added
@@ -38,7 +46,6 @@ Additional documentation and release notes are available at [Multiplayer Documen
3846

3947
### Removed
4048

41-
- Removed `SIPTransport` (#1870)
4249
- Removed `SnapshotSystem` (#1852)
4350
- Removed `com.unity.modules.animation`, `com.unity.modules.physics` and `com.unity.modules.physics2d` dependencies from the package (#1812)
4451
- Removed `com.unity.collections` dependency from the package (#1849)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,8 +1167,13 @@ public void Shutdown(bool discardMessageQueue = false)
11671167
NetworkLog.LogInfo(nameof(Shutdown));
11681168
}
11691169

1170-
m_ShuttingDown = true;
1171-
m_StopProcessingMessages = discardMessageQueue;
1170+
// If we're not running, don't start shutting down, it would only cause an immediate
1171+
// shutdown the next time the manager is started.
1172+
if (IsServer || IsClient)
1173+
{
1174+
m_ShuttingDown = true;
1175+
m_StopProcessingMessages = discardMessageQueue;
1176+
}
11721177
}
11731178

11741179
internal void ShutdownInternal()

com.unity.netcode.gameobjects/Tests/Runtime/StopStartRuntimeTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,37 @@ public IEnumerator WhenShuttingDownAndRestarting_SDKRestartsSuccessfullyAndStays
4444
Assert.IsTrue(m_ServerNetworkManager.IsServer);
4545
Assert.IsTrue(m_ServerNetworkManager.IsListening);
4646
}
47+
48+
[UnityTest]
49+
public IEnumerator WhenShuttingDownTwiceAndRestarting_SDKRestartsSuccessfullyAndStaysRunning()
50+
{
51+
// shutdown the server
52+
m_ServerNetworkManager.Shutdown();
53+
54+
// wait 1 frame because shutdowns are delayed
55+
var nextFrameNumber = Time.frameCount + 1;
56+
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber);
57+
58+
// Verify the shutdown occurred
59+
Assert.IsFalse(m_ServerNetworkManager.IsServer);
60+
Assert.IsFalse(m_ServerNetworkManager.IsListening);
61+
Assert.IsFalse(m_ServerNetworkManager.IsHost);
62+
Assert.IsFalse(m_ServerNetworkManager.IsClient);
63+
64+
// Shutdown the server again.
65+
m_ServerNetworkManager.Shutdown();
66+
67+
m_ServerNetworkManager.StartServer();
68+
// Verify the server started
69+
Assert.IsTrue(m_ServerNetworkManager.IsServer);
70+
Assert.IsTrue(m_ServerNetworkManager.IsListening);
71+
72+
// Wait several frames / one full network tick
73+
yield return s_DefaultWaitForTick;
74+
75+
// Verify the server is still running
76+
Assert.IsTrue(m_ServerNetworkManager.IsServer);
77+
Assert.IsTrue(m_ServerNetworkManager.IsListening);
78+
}
4779
}
4880
}

0 commit comments

Comments
 (0)