From 93be5d15c655bee96d77ff7d387c4fba99c98255 Mon Sep 17 00:00:00 2001 From: EmandM Date: Wed, 4 Dec 2024 17:35:09 -0500 Subject: [PATCH 1/3] fix: ObjectDisposedException on Unity Transport shutdown [backport] --- .../Runtime/Transports/UTP/UnityTransport.cs | 6 +++ .../Runtime/Transports/UnityTransportTests.cs | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index 420bad1460..034aa56eff 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -1442,6 +1442,11 @@ public override bool StartServer() /// public override void Shutdown() { + if (NetworkManager && !NetworkManager.ShutdownInProgress) + { + Debug.LogWarning("Directly calling `UnityTransport.Shutdown()` results in unexpected shutdown behaviour. All pending events will be lost. Use `NetworkManager.Shutdown()` instead."); + } + if (m_Driver.IsCreated) { // Flush all send queues to the network. NGO can be configured to flush its message @@ -1461,6 +1466,7 @@ public override void Shutdown() DisposeInternals(); m_ReliableReceiveQueues.Clear(); + m_State = State.Disconnected; // We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect m_ServerClientId = 0; diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs index d2dcddf7ea..5cbd338355 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs @@ -517,5 +517,42 @@ public IEnumerator ReliablePayloadsCanBeLargerThanMaximum() yield return null; } + + public enum AfterShutdownAction + { + Send, + DisconnectRemoteClient, + DisconnectLocalClient, + } + + [UnityTest] + public IEnumerator DoesNotActAfterShutdown([Values] AfterShutdownAction afterShutdownAction) + { + InitializeTransport(out m_Server, out m_ServerEvents); + InitializeTransport(out m_Client1, out m_Client1Events); + + m_Server.StartServer(); + m_Client1.StartClient(); + + yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events); + + m_Server.Shutdown(); + + if (afterShutdownAction == AfterShutdownAction.Send) + { + var data = new ArraySegment(new byte[16]); + m_Server.Send(m_Client1.ServerClientId, data, NetworkDelivery.Reliable); + } + else if (afterShutdownAction == AfterShutdownAction.DisconnectRemoteClient) + { + m_Server.DisconnectRemoteClient(m_Client1.ServerClientId); + + LogAssert.Expect(LogType.Assert, "DisconnectRemoteClient should be called on a listening server"); + } + else if (afterShutdownAction == AfterShutdownAction.DisconnectLocalClient) + { + m_Server.DisconnectLocalClient(); + } + } } } From c9b908a20a791c8871834964040f66ea47f8d2c0 Mon Sep 17 00:00:00 2001 From: EmandM Date: Thu, 5 Dec 2024 10:29:02 -0500 Subject: [PATCH 2/3] Update changelog --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index e47ac97fc3..5b5de2ccf8 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -30,6 +30,7 @@ Additional documentation and release notes are available at [Multiplayer Documen - Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033) - Fixed issue where the `NetworkSpawnManager.HandleNetworkObjectShow` could throw an exception if one of the `NetworkObject` components to show was destroyed during the same frame. (#3029) - Fixed issue where the `NetworkManagerHelper` was continuing to check for hierarchy changes when in play mode. (#3027) +- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118) ### Changed From 51b246dfe033d40cdbc700e5fd758967ca99d309 Mon Sep 17 00:00:00 2001 From: EmandM Date: Thu, 5 Dec 2024 14:40:28 -0500 Subject: [PATCH 3/3] Swap the location of the changelog entry --- com.unity.netcode.gameobjects/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 5b5de2ccf8..126431e85b 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -18,6 +18,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118) - Fixed issue where a newly synchronizing client would be synchronized with the current `NetworkVariable` values always which could cause issues with collections if there were any pending state updates. Now, when initially synchronizing a client, if a `NetworkVariable` has a pending state update it will serialize the previously known value(s) to the synchronizing client so when the pending updates are sent they aren't duplicate values on the newly connected client side. (#3126) - Fixed issue where changing ownership would mark every `NetworkVariable` dirty. Now, it will only mark any `NetworkVariable` with owner read permissions as dirty and will send/flush any pending updates to all clients prior to sending the change in ownership message. (#3126) - Fixed issue with `NetworkVariable` collections where transferring ownership to another client would not update the new owner's previous value to the most current value which could cause the last/previous added value to be detected as a change when adding or removing an entry (as long as the entry removed was not the last/previously added value). (#3126) @@ -30,7 +31,6 @@ Additional documentation and release notes are available at [Multiplayer Documen - Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033) - Fixed issue where the `NetworkSpawnManager.HandleNetworkObjectShow` could throw an exception if one of the `NetworkObject` components to show was destroyed during the same frame. (#3029) - Fixed issue where the `NetworkManagerHelper` was continuing to check for hierarchy changes when in play mode. (#3027) -- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118) ### Changed @@ -210,7 +210,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Added - Added a protected virtual method `NetworkTransform.OnInitialize(ref NetworkTransformState replicatedState)` that just returns the replicated state reference. - + ### Fixed - Fixed issue where invoking `NetworkManager.Shutdown` within `NetworkManager.OnClientStopped` or `NetworkManager.OnServerStopped` would force `NetworkManager.ShutdownInProgress` to remain true after completing the shutdown process. (#2661)