Skip to content

Commit 96025ac

Browse files
fix: Restarting UnityTransport after it failed to start [MTT-3452] (#2220)
* Add test for transport restart failure * Fix restart after transport failure * Add CHANGELOG entry * Add PR number to CHANGELOG entry
1 parent 58becb5 commit 96025ac

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2121

2222
### Fixed
2323

24+
- Fixed issue #1924 where `UnityTransport` would fail to restart after a first failure (even if what caused the initial failure was addressed). (#2220)
2425
- Fixed issue where `NetworkTransform.SetStateServerRpc` and `NetworkTransform.SetStateClientRpc` were not honoring local vs world space settings when applying the position and rotation. (#2203)
2526
- Fixed ILPP `TypeLoadException` on WebGL on MacOS Editor and potentially other platforms. (#2199)
2627
- Implicit conversion of NetworkObjectReference to GameObject will now return null instead of throwing an exception if the referenced object could not be found (i.e., was already despawned) (#2158)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,9 +1292,9 @@ public override bool StartClient()
12921292
}
12931293

12941294
var succeeded = ClientBindAndConnect();
1295-
if (!succeeded)
1295+
if (!succeeded && m_Driver.IsCreated)
12961296
{
1297-
Shutdown();
1297+
m_Driver.Dispose();
12981298
}
12991299
return succeeded;
13001300
}
@@ -1319,16 +1319,16 @@ public override bool StartServer()
13191319
{
13201320
case ProtocolType.UnityTransport:
13211321
succeeded = ServerBindAndListen(ConnectionData.ListenEndPoint);
1322-
if (!succeeded)
1322+
if (!succeeded && m_Driver.IsCreated)
13231323
{
1324-
Shutdown();
1324+
m_Driver.Dispose();
13251325
}
13261326
return succeeded;
13271327
case ProtocolType.RelayUnityTransport:
13281328
succeeded = StartRelayServer();
1329-
if (!succeeded)
1329+
if (!succeeded && m_Driver.IsCreated)
13301330
{
1331-
Shutdown();
1331+
m_Driver.Dispose();
13321332
}
13331333
return succeeded;
13341334
default:

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using NUnit.Framework;
22
using Unity.Netcode.Transports.UTP;
33
using UnityEngine;
4+
using UnityEngine.TestTools;
45

56
namespace Unity.Netcode.EditorTests
67
{
78
public class UnityTransportTests
89
{
910
// Check that starting a server doesn't immediately result in faulted tasks.
1011
[Test]
11-
public void BasicInitServer()
12+
public void UnityTransport_BasicInitServer()
1213
{
1314
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
1415
transport.Initialize();
@@ -20,7 +21,7 @@ public void BasicInitServer()
2021

2122
// Check that starting a client doesn't immediately result in faulted tasks.
2223
[Test]
23-
public void BasicInitClient()
24+
public void UnityTransport_BasicInitClient()
2425
{
2526
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
2627
transport.Initialize();
@@ -32,7 +33,7 @@ public void BasicInitClient()
3233

3334
// Check that we can't restart a server.
3435
[Test]
35-
public void NoRestartServer()
36+
public void UnityTransport_NoRestartServer()
3637
{
3738
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
3839
transport.Initialize();
@@ -45,7 +46,7 @@ public void NoRestartServer()
4546

4647
// Check that we can't restart a client.
4748
[Test]
48-
public void NoRestartClient()
49+
public void UnityTransport_NoRestartClient()
4950
{
5051
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
5152
transport.Initialize();
@@ -58,7 +59,7 @@ public void NoRestartClient()
5859

5960
// Check that we can't start both a server and client on the same transport.
6061
[Test]
61-
public void NotBothServerAndClient()
62+
public void UnityTransport_NotBothServerAndClient()
6263
{
6364
UnityTransport transport;
6465

@@ -80,5 +81,24 @@ public void NotBothServerAndClient()
8081

8182
transport.Shutdown();
8283
}
84+
85+
// Check that restarting after failure succeeds.
86+
[Test]
87+
public void UnityTransport_RestartSucceedsAfterFailure()
88+
{
89+
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
90+
transport.Initialize();
91+
92+
transport.SetConnectionData("127.0.0.", 4242);
93+
Assert.False(transport.StartServer());
94+
95+
LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242.");
96+
LogAssert.Expect(LogType.Error, "Server failed to bind");
97+
98+
transport.SetConnectionData("127.0.0.1", 4242);
99+
Assert.True(transport.StartServer());
100+
101+
transport.Shutdown();
102+
}
83103
}
84104
}

0 commit comments

Comments
 (0)