Skip to content

Commit e651438

Browse files
fix: Memory leak in UnityTransport after StartClient failure (#2518)
1 parent 689268a commit e651438

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

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

1313
### Fixed
1414

15+
- Fixed a memory leak in `UnityTransport` that occurred if `StartClient` failed. (#2518)
1516
- Fixed issue where a client could throw an exception if abruptly disconnected from a network session with one or more spawned `NetworkObject`(s). (#2510)
1617
- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496)
1718
- Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495)

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,24 +1382,22 @@ public override bool StartServer()
13821382
/// </summary>
13831383
public override void Shutdown()
13841384
{
1385-
if (!m_Driver.IsCreated)
1385+
if (m_Driver.IsCreated)
13861386
{
1387-
return;
1388-
}
1387+
// Flush all send queues to the network. NGO can be configured to flush its message
1388+
// queue on shutdown. But this only calls the Send() method, which doesn't actually
1389+
// get anything to the network.
1390+
foreach (var kvp in m_SendQueue)
1391+
{
1392+
SendBatchedMessages(kvp.Key, kvp.Value);
1393+
}
13891394

1390-
// Flush all send queues to the network. NGO can be configured to flush its message
1391-
// queue on shutdown. But this only calls the Send() method, which doesn't actually
1392-
// get anything to the network.
1393-
foreach (var kvp in m_SendQueue)
1394-
{
1395-
SendBatchedMessages(kvp.Key, kvp.Value);
1395+
// The above flush only puts the message in UTP internal buffers, need an update to
1396+
// actually get the messages on the wire. (Normally a flush send would be sufficient,
1397+
// but there might be disconnect messages and those require an update call.)
1398+
m_Driver.ScheduleUpdate().Complete();
13961399
}
13971400

1398-
// The above flush only puts the message in UTP internal buffers, need an update to
1399-
// actually get the messages on the wire. (Normally a flush send would be sufficient,
1400-
// but there might be disconnect messages and those require an update call.)
1401-
m_Driver.ScheduleUpdate().Complete();
1402-
14031401
DisposeInternals();
14041402

14051403
m_ReliableReceiveQueues.Clear();

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void UnityTransport_RestartSucceedsAfterFailure()
120120
Assert.False(transport.StartServer());
121121

122122
LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242.");
123-
LogAssert.Expect(LogType.Error, $"Network listen address (127.0.0.) is Invalid!");
123+
LogAssert.Expect(LogType.Error, "Network listen address (127.0.0.) is Invalid!");
124124
#if UTP_TRANSPORT_2_0_ABOVE
125125
LogAssert.Expect(LogType.Error, "Socket creation failed (error Unity.Baselib.LowLevel.Binding+Baselib_ErrorState: Invalid argument (0x01000003) <argument name stripped>");
126126
#endif
@@ -143,6 +143,22 @@ public void UnityTransport_StartServerWithoutAddresses()
143143
transport.Shutdown();
144144
}
145145

146+
// Check that StartClient returns false with bad connection data.
147+
[Test]
148+
public void UnityTransport_StartClientFailsWithBadAddress()
149+
{
150+
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
151+
transport.Initialize();
152+
153+
transport.SetConnectionData("foobar", 4242);
154+
Assert.False(transport.StartClient());
155+
156+
LogAssert.Expect(LogType.Error, "Invalid network endpoint: foobar:4242.");
157+
LogAssert.Expect(LogType.Error, "Target server network address (foobar) is Invalid!");
158+
159+
transport.Shutdown();
160+
}
161+
146162
#if UTP_TRANSPORT_2_0_ABOVE
147163
[Test]
148164
public void UnityTransport_EmptySecurityStringsShouldThrow([Values("", null)] string cert, [Values("", null)] string secret)

0 commit comments

Comments
 (0)