BufferPool has a null value pushed in its queue
I have multiple clients running on the same machine. After a random time, I don't receive any packets for some clients. I have put some logs to understand where it's coming from. That's the exception I get in Client.clientTick:
System.NullReferenceException: Object reference not set to an instance of an object at UnityNetcodeIO.BufferPool.GetBuffer (Int32 capacity) [0x00034] in Assets\UnityNetcodeIO\Scripts\Utils\BufferPool.cs:28 at UnityNetcodeIO.NetcodeClient+<Connect>c__AnonStorey0.<>m__1 (System.Byte[] payload, Int32 length) [0x00023] in Assets\UnityNetcodeIO\Scripts\Core\NetcodeClient.cs:359 at NetcodeIO.NET.Client.processConnectionPayload (NetcodePacketHeader header, Int32 length, NetcodeIO.NET.Utils.IO.ByteArrayReaderWriter stream) [0x00087] in Assets\Netcode.IO.NET\Public\Client.cs:551 at NetcodeIO.NET.Client.processDatagram (Datagram datagram) [0x000e9] in Assets\Netcode.IO.NET\Public\Client.cs:441 at NetcodeIO.NET.Client.Tick (Double time) [0x00058] in Assets\Netcode.IO.NET\Public\Client.cs:361 at NetcodeIO.NET.Client.clientTick (System.Object stateInfo) [0x00013] in Assets\Netcode.IO.NET\Public\Client.cs:387 UnityEngine.Debug:LogError(Object) DebugUtils:LogError(String) (at Assets/Core/Utils/DebugUtils.cs:101) NetcodeIO.NET.Client:clientTick(Object) (at Assets/Netcode.IO.NET/Public/Client.cs:396)
A null value gets pushed in BufferPool but when I added a log to ReturnBuffer, there was no null ByteBuffer that was enqueued in the pool. So I think it's a race condition between threads as BufferPool is not thread safe. My fix was to put a lock on the queue inside BufferPool when getting or returning a buffer. It seems to have fixed the issue so far but I don't know if it's efficient though.