Skip to content

Commit 06a83bd

Browse files
fix: ObjectDisposedException on Unity Transport shutdown [backport] (#3152)
* fix: ObjectDisposedException on Unity Transport shutdown [backport] * Update changelog * Swap the location of the changelog entry --------- Co-authored-by: Noel Stephens <[email protected]>
1 parent be7f604 commit 06a83bd

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1818

1919
### Fixed
2020

21+
- Fixed issue where an exception was thrown when calling `NetworkManager.Shutdown` after calling `UnityTransport.Shutdown`. (#3118)
2122
- 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)
2223
- 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)
2324
- 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)
@@ -209,7 +210,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
209210
### Added
210211

211212
- Added a protected virtual method `NetworkTransform.OnInitialize(ref NetworkTransformState replicatedState)` that just returns the replicated state reference.
212-
213+
213214
### Fixed
214215

215216
- 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)

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,11 @@ public override bool StartServer()
14421442
/// </summary>
14431443
public override void Shutdown()
14441444
{
1445+
if (NetworkManager && !NetworkManager.ShutdownInProgress)
1446+
{
1447+
Debug.LogWarning("Directly calling `UnityTransport.Shutdown()` results in unexpected shutdown behaviour. All pending events will be lost. Use `NetworkManager.Shutdown()` instead.");
1448+
}
1449+
14451450
if (m_Driver.IsCreated)
14461451
{
14471452
// Flush all send queues to the network. NGO can be configured to flush its message
@@ -1461,6 +1466,7 @@ public override void Shutdown()
14611466
DisposeInternals();
14621467

14631468
m_ReliableReceiveQueues.Clear();
1469+
m_State = State.Disconnected;
14641470

14651471
// We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect
14661472
m_ServerClientId = 0;

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,5 +517,42 @@ public IEnumerator ReliablePayloadsCanBeLargerThanMaximum()
517517

518518
yield return null;
519519
}
520+
521+
public enum AfterShutdownAction
522+
{
523+
Send,
524+
DisconnectRemoteClient,
525+
DisconnectLocalClient,
526+
}
527+
528+
[UnityTest]
529+
public IEnumerator DoesNotActAfterShutdown([Values] AfterShutdownAction afterShutdownAction)
530+
{
531+
InitializeTransport(out m_Server, out m_ServerEvents);
532+
InitializeTransport(out m_Client1, out m_Client1Events);
533+
534+
m_Server.StartServer();
535+
m_Client1.StartClient();
536+
537+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_Client1Events);
538+
539+
m_Server.Shutdown();
540+
541+
if (afterShutdownAction == AfterShutdownAction.Send)
542+
{
543+
var data = new ArraySegment<byte>(new byte[16]);
544+
m_Server.Send(m_Client1.ServerClientId, data, NetworkDelivery.Reliable);
545+
}
546+
else if (afterShutdownAction == AfterShutdownAction.DisconnectRemoteClient)
547+
{
548+
m_Server.DisconnectRemoteClient(m_Client1.ServerClientId);
549+
550+
LogAssert.Expect(LogType.Assert, "DisconnectRemoteClient should be called on a listening server");
551+
}
552+
else if (afterShutdownAction == AfterShutdownAction.DisconnectLocalClient)
553+
{
554+
m_Server.DisconnectLocalClient();
555+
}
556+
}
520557
}
521558
}

0 commit comments

Comments
 (0)