question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BitReader.ReadByteArray() throws OverflowException when handling connection request

See original GitHub issue

When a client is started and the server reacts to handle the connection request, it is throwing an OverflowException. See the exception stack trace below.

OverflowException: Arithmetic operation resulted in an overflow.
MLAPI.Serialization.BitReader.ReadByteArray (System.Byte[] readTo, System.Int64 knownLength) (at Assets/Third-Party/MLAPI/Serialization/BitReader.cs:781)
MLAPI.Messaging.InternalMessageHandler.HandleConnectionRequest (System.UInt64 clientId, System.IO.Stream stream) (at Assets/Third-Party/MLAPI/Messaging/InternalMessageHandler.cs:190)
MLAPI.NetworkingManager.HandleIncomingData (System.UInt64 clientId, System.String channelName, System.ArraySegment`1[T] data, System.Single receiveTime, System.Boolean allowBuffer) (at Assets/Third-Party/MLAPI/Core/NetworkingManager.cs:933)
MLAPI.NetworkingManager.HandleRawTransportPoll (MLAPI.Transports.NetEventType eventType, System.UInt64 clientId, System.String channelName, System.ArraySegment`1[T] payload, System.Single receiveTime) (at Assets/Third-Party/MLAPI/Core/NetworkingManager.cs:870)
MLAPI.NetworkingManager.Update () (at Assets/Third-Party/MLAPI/Core/NetworkingManager.cs:651)

Hooking up the debugger, I observed “knownLength” to be -1 after line 780, which I’m assuming is not expected ever. Feeding -1 into the array constructor produces the exception above. image

Repro Steps

  1. Download the Bobble.zip Unity project
  2. Unzip and open the project in Unity 2019.4.12f1
  3. File > Build Settings… > Build a. Copy “AssetBundles” folder from root of Bobble project folder to build output folder b. Run Bobble.exe
  4. Open “Lobby” scene in Unity by navigating to Assets > Scenes > Lobby a. Enter play mode in the Editor
  5. At this point, you should have an instance of the Lobby scene running in the Editor and the built version a. Click “Start Host” in the Editor lobby b. Click “Join” in the Built lobby [BUG: see OverflowException in Editor console]

Expected Behavior I expect the client connection request data to be read without error.

Environment

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

12reactions
LukeStampflicommented, Mar 1, 2021

Someone encountered this on Discord as well: https://discord.com/channels/449263083769036810/563033158480691211/815881188094640158

It looks like this exception (knownLength = -1) happens if the client has connection approval deactivated in the NetworkingManager while the server has it activated. On the server side MLAPI will try to read the length of the connection approval array but fail because the client didn’t write this into the stream at all.

Rebuilding the client/server after activating Connection Approval in the NetworkingManager should fix this issue.

I’m closing this under the assumption that the above helps resolving this issue. If that’s not the case please re-open it.

0reactions
NoelStephensUnitycommented, Feb 12, 2021

Hi Furic, I am currently looking into this bug and was hoping to get some more information regarding this issue. Looking at your Bobble.zip file, it appears that when you are joining as a client via the following method: public void Join( string ipAddress, int port ) (NetworkLobby.cs) that the NetworkingManager.Singleton.NetworkConfig.ConnectionData is never being populated with the “approval data/token” required to authenticate the connection request.

MLAPI Documentation provides you with a basic example of how this might look, but I will run through your two methods used to start the client and the host and how it might look if you followed the MLAPI documentation example:

Client Side - within your public void Join( string ipAddress, int port ) method

NetworkingManager.Singleton.NetworkConfig.ConnectionData = System.Text.Encoding.ASCII.GetBytes("room password");
NetworkingManager.Singleton.StartClient();

Host/Server Side - within your void ConnectionApprovalCallback( byte[] data, ulong id, ConnectionApprovedDelegate callback ) method On the receiving side of things (i.e. Server), it would look like there is no code to check the passed in byte array value:

        void ConnectionApprovalCallback( byte[] data, ulong id, ConnectionApprovedDelegate callback )
        {
            bool isAllowed =
                this.Clients.Count < this.Configuration.Capacity    // ensure we are not at capacity
                && !this.IsGameStarted;                             // ensure the game has not started already

            // TODO: disallow if client token is not valid
            
            // Invoke the MLAPI connection approval callback to submit our decision.
            //
            callback(
                createPlayerObject: false,
                playerPrefabHash: null,
                approved: isAllowed,
                position: null,
                rotation: null );
        }

Not knowing what kind of authorization/token information you will be populating the ConnectionData with, I am providing you with the “other end” of the authorization process that you would place just under the //TODO: disallow if client token is not valid:

            // TODO: disallow if client token is not valid
            String ExampleConnectionData = System.Text.Encoding.ASCII.GetString( data );
            if(ExampleConnectionData != "room password")
            {
                isAllowed = false;
            }

Let me know if this solves your issue with connection authorization, and if it does not then if you could provide me with further information regarding where the NetworkingManager.Singleton.NetworkConfig.ConnectionData is being populated that would greatly help me further assist you.

Cheers, Noel

Read more comments on GitHub >

github_iconTop Results From Across the Web

BitReader.ReadByteArray() throws OverflowException ...
When a client is started and the server reacts to handle the connection request, it is throwing an OverflowException.
Read more >
Why is allocating a byte array throwing an Overflow ...
I am receiving an error on my C# Console Application. I am trying to do some code but its throwing an exception whenever...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found