Skip to content

Commit 93be5d1

Browse files
committed
fix: ObjectDisposedException on Unity Transport shutdown [backport]
1 parent 6e777f9 commit 93be5d1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

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)