-
Notifications
You must be signed in to change notification settings - Fork 450
fix: Fixed a few memory leak cases when shutting down NetworkManager during Incoming Message Queue processing #1323
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
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
75ee739
fix: Fixed memory leak on shutdown
ShadauxCat 22fd572
fix: Fixed a few memory leak cases when shutting down NetworkManager …
ShadauxCat 01055b5
standards check
ShadauxCat 905be89
Fixed test not cleaning up quite correctly.
ShadauxCat 8a30132
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity 3c6876d
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity c55f5b7
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
mattwalsh-unity cbe8f77
Merge develop into fix/fix_memory_leak_on_shutdown
github-actions[bot] 4977018
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity 924ab27
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity 0eec4f0
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity c162022
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity 4443fd4
-Reworked the way shutdown is handled so it can't be processed while …
ShadauxCat 9920207
Merge branch 'develop' into fix/fix_memory_leak_on_shutdown
andrews-unity b63924b
standards
ShadauxCat 72c9055
Fixed broken test, slight naming change to be more accurate.
ShadauxCat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
testproject/Assets/Tests/Runtime/NoMemoryLeakOnNetworkManagerShutdownTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
using System.Collections; | ||
using Unity.Netcode; | ||
using Unity.Netcode.RuntimeTests; | ||
using NUnit.Framework; | ||
using TestProject.RuntimeTests.Support; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
|
||
namespace TestProject.RuntimeTests | ||
{ | ||
public class NoMemoryLeakOnNetworkManagerShutdownTest | ||
{ | ||
private GameObject m_Prefab; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
ShutdownDuringOnNetworkSpawnBehaviour.SpawnCount = 0; | ||
ShutdownDuringOnNetworkSpawnBehaviour.ClientRpcsCalled = 0; | ||
ShutdownDuringOnNetworkSpawnBehaviour.ServerRpcsCalled = 0; | ||
ShutdownDuringOnNetworkSpawnBehaviour.ShutdownImmediately = false; | ||
} | ||
|
||
[UnityTearDown] | ||
public IEnumerator Teardown() | ||
{ | ||
MultiInstanceHelpers.Destroy(); | ||
// Shutdown and clean up both of our NetworkManager instances | ||
if (m_Prefab) | ||
{ | ||
Object.Destroy(m_Prefab); | ||
} | ||
yield break; | ||
} | ||
|
||
public IEnumerator RunTest() | ||
{ | ||
// Must be 1 for this test. | ||
const int numClients = 1; | ||
Assert.True(MultiInstanceHelpers.Create(numClients, out NetworkManager server, out NetworkManager[] clients)); | ||
m_Prefab = new GameObject("Object"); | ||
m_Prefab.AddComponent<ShutdownDuringOnNetworkSpawnBehaviour>(); | ||
var networkObject = m_Prefab.AddComponent<NetworkObject>(); | ||
|
||
// Make it a prefab | ||
MultiInstanceHelpers.MakeNetworkObjectTestPrefab(networkObject); | ||
|
||
var validNetworkPrefab = new NetworkPrefab(); | ||
validNetworkPrefab.Prefab = m_Prefab; | ||
server.NetworkConfig.NetworkPrefabs.Add(validNetworkPrefab); | ||
foreach (var client in clients) | ||
{ | ||
client.NetworkConfig.NetworkPrefabs.Add(validNetworkPrefab); | ||
} | ||
|
||
// Start the instances | ||
if (!MultiInstanceHelpers.Start(false, server, clients)) | ||
{ | ||
Debug.LogError("Failed to start instances"); | ||
Assert.Fail("Failed to start instances"); | ||
} | ||
|
||
// [Client-Side] Wait for a connection to the server | ||
yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.WaitForClientsConnected(clients, null, 512)); | ||
|
||
// [Host-Side] Check to make sure all clients are connected | ||
yield return MultiInstanceHelpers.Run( | ||
MultiInstanceHelpers.WaitForClientsConnectedToServer(server, clients.Length, null, 512)); | ||
|
||
var serverObject = Object.Instantiate(m_Prefab, Vector3.zero, Quaternion.identity); | ||
NetworkObject serverNetworkObject = serverObject.GetComponent<NetworkObject>(); | ||
serverNetworkObject.NetworkManagerOwner = server; | ||
serverNetworkObject.Spawn(); | ||
|
||
// Wait until all objects have spawned. | ||
const int maxFrames = 240; | ||
var doubleCheckTime = Time.realtimeSinceStartup + 5.0f; | ||
while (ShutdownDuringOnNetworkSpawnBehaviour.SpawnCount < clients.Length + 1) | ||
{ | ||
if (Time.frameCount > maxFrames) | ||
{ | ||
// This is here in the event a platform is running at a higher | ||
// frame rate than expected | ||
if (doubleCheckTime < Time.realtimeSinceStartup) | ||
{ | ||
Assert.Fail("Did not successfully call all expected client RPCs"); | ||
break; | ||
} | ||
} | ||
var nextFrameNumber = Time.frameCount + 1; | ||
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber); | ||
} | ||
|
||
Assert.AreEqual(ShutdownDuringOnNetworkSpawnBehaviour.SpawnCount, clients.Length + 1); | ||
// Extra frames to catch Native Container memory leak log message | ||
var lastFrameNumber = Time.frameCount + 10; | ||
yield return new WaitUntil(() => Time.frameCount >= lastFrameNumber); | ||
Object.Destroy(serverObject); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator WhenNetworkManagerShutsDownWhileTriggeredMessagesArePending_MemoryDoesNotLeak() | ||
{ | ||
yield return RunTest(); | ||
LogAssert.NoUnexpectedReceived(); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator WhenNetworkManagerShutsDownWhileTriggeredMessagesArePending_MessagesAreStillProcessed() | ||
{ | ||
yield return RunTest(); | ||
Assert.AreEqual(1, ShutdownDuringOnNetworkSpawnBehaviour.ClientRpcsCalled); | ||
Assert.AreEqual(1, ShutdownDuringOnNetworkSpawnBehaviour.ServerRpcsCalled); | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator WhenNetworkManagerShutsDownImmediatelyWhileTriggeredMessagesArePending_MessagesAreNotProcessed() | ||
{ | ||
ShutdownDuringOnNetworkSpawnBehaviour.ShutdownImmediately = true; | ||
yield return RunTest(); | ||
Assert.AreEqual(0, ShutdownDuringOnNetworkSpawnBehaviour.ClientRpcsCalled); | ||
Assert.AreEqual(0, ShutdownDuringOnNetworkSpawnBehaviour.ServerRpcsCalled); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
testproject/Assets/Tests/Runtime/NoMemoryLeakOnNetworkManagerShutdownTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fix is technically not a part of this change, but the added tests won't pass without this change because it causes warnings to get logged.