Skip to content

fix: Client-Side NetworkEvent.Disconnect can throw an exception in UnityTransport #1884

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
## Unreleased

### Added

### Changed

### Removed

### Fixed
- Fixed client throwing an exception if it has messages in the outbound queue when processing the NetworkEvent.Disconnect event and is using UTP. (#1884)

## [1.0.0-pre.7] - 2022-04-06

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,11 @@ private void HandleRawTransportPoll(NetworkEvent networkEvent, ulong clientId, A
}
else
{
Shutdown();
// We must pass true here and not process any sends messages
// as we are no longer connected and thus there is no one to
// send any messages to and this will cause an exception within
// UnityTransport as the client ID is no longer valid.
Shutdown(true);
}
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_TransportDisconnect.End();
Expand Down
57 changes: 57 additions & 0 deletions testproject/Assets/Tests/Runtime/ServerDisconnectsClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode;
using Unity.Netcode.TestHelpers.Runtime;


namespace TestProject.RuntimeTests
{
public class ServerDisconnectsClientTest : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;
protected override void OnCreatePlayerPrefab()
{
m_PlayerPrefab.AddComponent<ClientSendRpcUponDisconnect>();
base.OnCreatePlayerPrefab();
}

[UnityTest]
public IEnumerator ServerDisconnectsClient()
{
m_ServerNetworkManager.DisconnectClient(m_ClientNetworkManagers[0].LocalClientId);
yield return WaitForConditionOrTimeOut(() => !m_ClientNetworkManagers[0].IsConnectedClient && !m_ClientNetworkManagers[0].IsListening);
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for client to disconnect!");
}
public class ClientSendRpcUponDisconnect : NetworkBehaviour
{
public override void OnNetworkSpawn()
{
if (!IsServer)
{
NetworkManager.OnClientDisconnectCallback += NetworkManager_OnClientDisconnectCallback;
}
base.OnNetworkSpawn();
}

[ServerRpc(RequireOwnership = false)]
public void ClientToServerRpc()
{
Debug.Log($"Received {nameof(ClientToServerRpc)}");
}

private void NetworkManager_OnClientDisconnectCallback(ulong obj)
{
NetworkManager.OnClientDisconnectCallback -= NetworkManager_OnClientDisconnectCallback;
// To simulate that there were already messages to be sent this frame in the send queue,
// we just send a few packets to the server even though we are considered disconnected
// at this point.
for (int i = 0; i < 5; i++)
{
ClientToServerRpc();
}
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.