Skip to content

fix: Add OnPreShutdown to NetworkManager #3358

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 3 commits into from
Mar 26, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ public struct ConnectionApprovalRequest
/// </summary>
public event Action OnClientStarted = null;

/// <summary>
/// Subscribe to this event to get notifications before a <see cref="NetworkManager"/> instance is being destroyed.
/// This is useful if you want to use the state of anything the NetworkManager cleans up during its shutdown.
/// </summary>
public event Action OnPreShutdown = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor side note, you don't need to set it to null as that is the default value when the NetworkManager class is instantiated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I just figured I'd keep it consistent with the other events.


/// <summary>
/// This callback is invoked once the local server is stopped.
/// </summary>
Expand Down Expand Up @@ -1198,6 +1204,8 @@ internal void ShutdownInternal()
NetworkLog.LogInfo(nameof(ShutdownInternal));
}

OnPreShutdown?.Invoke();

this.UnregisterAllNetworkUpdates();

// Everything is shutdown in the order of their dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,46 @@ public IEnumerator OnClientAndServerStartedCalledWhenHostStarts()
Assert.AreEqual(2, callbacksInvoked, "either OnServerStarted or OnClientStarted wasn't invoked");
}

[UnityTest]
public IEnumerator OnPreShutdownCalledWhenShuttingDown()
{
bool preShutdownInvoked = false;
bool shutdownInvoked = false;
var gameObject = new GameObject(nameof(OnPreShutdownCalledWhenShuttingDown));
m_ServerManager = gameObject.AddComponent<NetworkManager>();

// Set dummy transport that does nothing
var transport = gameObject.AddComponent<DummyTransport>();
m_ServerManager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport };

Action onPreShutdown = () =>
{
preShutdownInvoked = true;
Assert.IsFalse(shutdownInvoked, "OnPreShutdown was invoked after OnServerStopped");
};

Action<bool> onServerStopped = (bool wasAlsoClient) =>
{
shutdownInvoked = true;
Assert.IsTrue(preShutdownInvoked, "OnPreShutdown wasn't invoked before OnServerStopped");
};

// Start server to cause initialization process
Assert.True(m_ServerManager.StartServer());
Assert.True(m_ServerManager.IsListening);

m_ServerManager.OnPreShutdown += onPreShutdown;
m_ServerManager.OnServerStopped += onServerStopped;
m_ServerManager.Shutdown();
Object.DestroyImmediate(gameObject);

yield return WaitUntilManagerShutsdown();

Assert.False(m_ServerManager.IsListening);
Assert.True(preShutdownInvoked, "OnPreShutdown wasn't invoked");
Assert.True(shutdownInvoked, "OnServerStopped wasn't invoked");
}

private IEnumerator WaitUntilManagerShutsdown()
{
/* Need two updates to actually shut down. First one to see the transport failing, which
Expand Down
1 change: 1 addition & 0 deletions pvpExceptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnServerStartedCalledWhenServerStarts(): undocumented",
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnClientStartedCalledWhenClientStarts(): undocumented",
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnClientAndServerStartedCalledWhenHostStarts(): undocumented",
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator OnPreShutdownCalledWhenShuttingDown(): undocumented",
"Unity.Netcode.RuntimeTests.NetworkManagerEventsTests: IEnumerator Teardown(): undocumented",
"Unity.Netcode.RuntimeTests.NetworkManagerSceneManagerTests: undocumented",
"Unity.Netcode.RuntimeTests.NetworkManagerSceneManagerTests: void SceneManagerAssigned(): undocumented",
Expand Down