Skip to content

Commit 7f60010

Browse files
fix: Prevent an exception in UTP when host disconnects (#1447)
The code was invoking the disconnect transport event callback on the manager before doing some checks on UTP's NetworkConnection. But on a client, that callback ends up calling Shutdown on the transport. This disposes of the driver so the checks that follow end up throwing an exception. The fix was to reorder the checks before the callback. Also fix the logic that checked if there was a connection failure. It was checking if the state of the connection was Connecting, but that's never the case after a Disconnect event (the state will always be Disconnected).
1 parent e90791b commit 7f60010

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

com.unity.netcode.adapter.utp/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this package will be documented in this file. The format
1010
### Fixed
1111

1212
- Fixed packet overflow errors when sending payloads too close to the MTU (was mostly visible when using Relay).
13+
- Don't throw an exception when the host disconnects (issue 1439 on GitHub).
1314

1415
## [1.0.0-pre.3] - 2021-10-22
1516

com.unity.netcode.adapter.utp/Runtime/UnityTransport.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,20 +435,22 @@ private bool ProcessEvent()
435435
}
436436
case TransportNetworkEvent.Type.Disconnect:
437437
{
438-
InvokeOnTransportEvent(NetcodeNetworkEvent.Disconnect,
439-
ParseClientId(networkConnection),
440-
default(ArraySegment<byte>),
441-
Time.realtimeSinceStartup);
442-
443438
if (m_ServerConnection.IsCreated)
444439
{
445440
m_ServerConnection = default;
446-
if (m_Driver.GetConnectionState(m_ServerConnection) == NetworkConnection.State.Connecting)
441+
442+
var reason = reader.ReadByte();
443+
if (reason == (byte)Networking.Transport.Error.DisconnectReason.MaxConnectionAttempts)
447444
{
448445
Debug.LogError("Client failed to connect to server");
449446
}
450447
}
451448

449+
InvokeOnTransportEvent(NetcodeNetworkEvent.Disconnect,
450+
ParseClientId(networkConnection),
451+
default(ArraySegment<byte>),
452+
Time.realtimeSinceStartup);
453+
452454
m_State = State.Disconnected;
453455
return true;
454456
}

0 commit comments

Comments
 (0)