RevenantX / LiteNetLib

Lite reliable UDP library for Mono and .NET
https://revenantx.github.io/LiteNetLib/index.html
MIT License
3k stars 489 forks source link

Irregular error with NetPacketProcessor on sending nested with null value #517

Closed berta2 closed 1 year ago

berta2 commented 1 year ago

Hi RevenantX,

first of all thanks for your great work, I have already implemented some projects with LiteNetLib.

Now I have encountered a problem that I can not explain.

I have an Employee class that contains a Person class and a Department class. All of them are INetSerializeable. If I put them in an EmployeePacket class and add a Person class to the Employee but not Department class, the send operation sometimes works and sometimes not.

Error message:

System.ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name" @ Deserialize() on Department class -> DepartmentId = reader.GetInt();

My question now is, why does this sometimes work 10 times in a row without error message? Sometimes it goes wrong after the first time. And sometimes it does not work at the first send.

If it would never work, it would be clear that you can not send a null value. But exactly this seems to work. Why not always?

Here I have an working example for you: https://github.com/berta2/LiteNetLibTest

Library version: 0.9.5.2

Framework: .NET 6

OS: Windows

RevenantX commented 1 year ago

@berta2 cannot reproduce in your project. Maybe you somewhere using multithreading?

RevenantX commented 1 year ago

@berta2 any updates?

Flavelius commented 1 year ago

I had a similar error that occured with higher frequency packets, where only some failed, in my case it seemed to be for the (Try)GetString() method. I tried replacing the null check in NetDataWriter.Put(string) with string.IsNullorEmpty(x), because the ones that i pass in can only ever be string.Empty. This seemed to fix the error for some reason, but it could just have been a placebo.

RevenantX commented 1 year ago

@berta2 try latest version. There can be fix.

RevenantX commented 1 year ago

Duplicate of #516