Skip to content

fix: shutdown causes exception if invoked twice [MTT-6752] #2622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
### Fixed

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

## Changed

Expand Down
10 changes: 8 additions & 2 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -942,10 +942,16 @@ public void Shutdown(bool discardMessageQueue = false)
if (IsServer || IsClient)
{
m_ShuttingDown = true;
MessageManager.StopProcessing = discardMessageQueue;
if (MessageManager != null)
{
MessageManager.StopProcessing = discardMessageQueue;
}
}

NetworkConfig.NetworkTransport.OnTransportEvent -= ConnectionManager.HandleNetworkEvent;
if (NetworkConfig != null && NetworkConfig.NetworkTransport != null)
{
NetworkConfig.NetworkTransport.OnTransportEvent -= ConnectionManager.HandleNetworkEvent;
}
}

// Ensures that the NetworkManager is cleaned up before OnDestroy is run on NetworkObjects and NetworkBehaviours when unloading a scene with a NetworkManager
Expand Down
21 changes: 21 additions & 0 deletions testproject/Assets/Tests/Runtime/NetworkManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;

namespace TestProject.RuntimeTests
{
Expand Down Expand Up @@ -94,5 +95,25 @@ public void ValidateHostSettings()
Assert.IsTrue(m_NumberOfTimesInvoked == 1, $"OnClientConnectedCallback was invoked {m_NumberOfTimesInvoked} as opposed to just once!");
Assert.IsTrue(m_NetworkBehaviourIsServerWasSet, $"IsServer was not true when OnClientConnectedCallback was invoked!");
}

/// <summary>
/// Validate shutting down a second time does not cause an exception.
/// </summary>
[UnityTest]
public IEnumerator ValidateShutdown()
{
// Register for the server stopped notification so we know we have shutdown completely
m_ServerNetworkManager.OnServerStopped += M_ServerNetworkManager_OnServerStopped;
// Shutdown
m_ServerNetworkManager.Shutdown();
yield return s_DefaultWaitForTick;
}

private void M_ServerNetworkManager_OnServerStopped(bool obj)
{
m_ServerNetworkManager.OnServerStopped -= M_ServerNetworkManager_OnServerStopped;
// Verify that we can invoke shutdown again without an exception
m_ServerNetworkManager.Shutdown();
}
}
}