Skip to content
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

fix: shutdown and shutdown notification issues [MTT-7791] [MTT-7540] #2789

Conversation

NoelStephensUnity
Copy link
Collaborator

@NoelStephensUnity NoelStephensUnity commented Dec 7, 2023

This PR includes several fixes to the NetworkManager shutdown sequence where:

  • A client disconnected by a server-host would not receive a notification
  • A server-host could shutdown during a relay connection but periodically the transport disconnect message to any connected clients could be dropped.
  • A host could disconnect its local client but remain running as a server
  • OnClientDisconnectedCallback was not being invoked under certain conditions when disconnected.
  • OnClientDisconnectedCallback was not returning the assigned client identifier on the client-side when invoked.
  • If a host or server shutdown while a client owned NetworkObjects (other than the player) it would throw an exception.
  • If you modify a NetworkVariable or NetworkList during a NetworkManager shutdown an exception was thrown.

MTT-7791
MTT-7540

fix: #2759
fix: #2705
fix: #2699
fix: #2389
fix: #2064
fix: #1880

Changelog

  • Fixed: Issue where a client disconnected by a server-host would not receive a local notification.
  • Fixed: Issue where a server-host could shutdown during a relay connection but periodically the transport disconnect message sent to any connected clients could be dropped.
  • Fixed: Issue where a host could disconnect its local client but remain running as a server.
  • Fixed: Issue where OnClientDisconnectedCallback was not being invoked under certain conditions.
  • Fixed: Issue where OnClientDisconnectedCallback was always returning 0 as the client identifier.
  • Fixed: Issue where if a host or server shutdown while a client owned NetworkObjects (other than the player) it would throw an exception.
  • Fixed: Issue where setting values on a NetworkVariable or NetworkList within OnNetworkDespawn during a shutdown sequence would throw an exception.
  • Changed: The server or host shutdown so it will now perform a "soft shutdown" when NetworkManager.Shutdown is invoked. This will send a disconnect notification to all connected clients and the server-host will wait for all connected clients to disconnect or timeout after a 5 second period before completing the shutdown process.
  • Changed: OnClientDisconnectedCallback will now return the assigned client identifier on the local client side if the client was approved and assigned one prior to being disconnected.

Testing and Documentation

  • Includes integration test updates.
  • Updates to public documentation: PR-1149

Just return from OnClientDisconnectFromServer when the server is shutting down as there is no point in doing ownership cleanup or client disconnected notification messages at that point.
Approaching this fix a different way since we still should send out client disconnect notifications and such.
Server or host was not sending disconnect when it shutdown.
When a client disconnects itself or is disconnected by the server, it now returns its client identifier.

Server or host shutdown sequence now includes a "soft shutdown" where the server-host will send out disconnection messages to all clients with the reason for the client being disconnected.
the test project exit button script is now a bit more intelligent with how it handles exiting and now logs the reason for disconnection.
fixing exceptions when certain missing properties are not set (primarily buttons or toggles)
updating tests for changes.
Don't allow a host or server to disconnect its local client.
Adding changelog entries
@NoelStephensUnity NoelStephensUnity changed the title fix: client shutdown and shutdown notification issues [MTT-7791] fix: client shutdown and shutdown notification issues [MTT-7791] [MTT-7540] Dec 7, 2023
removing unused namespace.
Fixes issue with NetworkVariable and NetworkList throwing exceptions upon shutdown.
Validates that setting NetworkVariables or NetworkList values during OnNetworkDespawn does not throw an exception.
adding change log entry
fixing warning message about setting network variable/list values during a shutdown.
Removing unrequired null check
@NoelStephensUnity NoelStephensUnity changed the title fix: client shutdown and shutdown notification issues [MTT-7791] [MTT-7540] fix: shutdown and shutdown notification issues [MTT-7791] [MTT-7540] Dec 7, 2023
@@ -925,6 +935,7 @@ internal void OnClientDisconnectFromServer(ulong clientId)
}
}
else
if (!NetworkManager.ShutdownInProgress)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any particular reason you're putting all these ifs on their own line instead of using the standard else if?

Copy link
Collaborator Author

@NoelStephensUnity NoelStephensUnity Dec 8, 2023

Choose a reason for hiding this comment

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

It is the standard way to do that, but can make it all one line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am mistaken... I have Rust in my brain.
"else if" is the correct way (my brain went on vacation Wednesday night)

ConnectedClients.Clear();
ConnectedClientIds.Clear();
ConnectedClientsList.Clear();

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a critique, just curious why these (and ProcessSendQueues) needed to be moved, just so I can understand the changes you made better.

Copy link
Collaborator Author

@NoelStephensUnity NoelStephensUnity Dec 8, 2023

Choose a reason for hiding this comment

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

Ahh... yeah this is because the DisconnectRemoteClient(clientId) invocation below on line 1132 can invoke OnClientDisconnectFromServer which invokes DisconnectRemoteClient:

        internal void DisconnectRemoteClient(ulong clientId)
        {
            MessageManager.ProcessSendQueues();
            OnClientDisconnectFromServer(clientId);
        }

It processes the send queue prior to invoking OnClientDisconnectFromServer(clientId);. However, on line 995 (within OnClientDisconnectFromServer) the ClientDisconnectedMessage is being sent to any remaining connected clients. This means the last client to be disconnected would not get the ClientDisconnectedMessage if a host or server was shutting down.

I didn't want to change the order of operations in DisconnectRemoteClient but wanted to make sure the last ClientDisconnectedMessage was sent out.
(it is sort of a mute point...but I guess I was just in the "make sure any last messages are sent" mode)

Thinking about it... the last client would get the last message... so I probably don't need to do that... and since I am sending disconnect messages prior to even shutting down... that probably doesn't even need to be invoked any longer.

{
if (m_NetworkBehaviour.NetworkManager.LogLevel <= LogLevel.Developer)
{
Debug.LogWarning($"NetworkVariable is written to during the NetworkManager shutdown! " +
Copy link
Collaborator

Choose a reason for hiding this comment

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

The wording of this message feels a little passive aggressive to me. "Are you doing X? If so, try not to do X." I feel like the second sentence is implied by the question and can be left off.

NoelStephensUnity and others added 5 commits December 8, 2023 13:50
remove the cr/lf after some else if statments
Adjusting warning message language.
Adjusting when the send queue is processed.
…er-shutdown' of https://github.com/Unity-Technologies/com.unity.netcode.gameobjects into fix/client-owned-objects-cause-exception-upon-host-server-shutdown
reverting back moving the MessageManager?.ProcessSendQueues(); within Shutdown and removing the migration of that out of OnClientDisconnectFromServer.
@NoelStephensUnity NoelStephensUnity enabled auto-merge (squash) December 9, 2023 01:19
@NoelStephensUnity NoelStephensUnity merged commit 654e413 into develop Dec 11, 2023
1 check passed
@NoelStephensUnity NoelStephensUnity deleted the fix/client-owned-objects-cause-exception-upon-host-server-shutdown branch December 11, 2023 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment