Facepunch / sbox-sdf

Library providing marching cubes / squares mesh generation
MIT License
15 stars 7 forks source link

`BaseNetworkable.OnCallRemoteProcedure` memory leak #20

Closed PolSpock closed 1 year ago

PolSpock commented 1 year ago

Hello,

Main topic

As i explained on this issue https://github.com/sboxgame/issues/issues/4109, i'm using the Facepunch sbox-sdf library for my game, and in the past, i used sbox-csg.

The common point between the 2 that i'm encountering * OUT OF MEMORY! /+/ attempted allocation size: 5000055 error on my sbox-server.exe running on windows dedicated server.

I've decided to investigate myself to spot the error, and i have spotted a crashing out of memory error caused by BaseNetworkable.OnCallRemoteProcedure

So here's the code i'm talking about: https://github.com/Facepunch/sbox-sdf/blob/main/libsdf/code/SdfWorld.Entity.cs#L114C32-L114C32

In fact, the more time passes by, the more you will increase your memory. And, arrived in a specific usage of memory, which I cannot explain how much and why, your server will run ***** OUT OF MEMORY! *****

You can easily try the issue on my sandbox derived game: https://github.com/PolSpock/sbox-memory-leak/tree/main where you just have to spawn some OnCallRemoteProcedureEntity.cs with the ingame Toolgun code/tools/OnCallRemoteProcedureEntitySpawner.cs

What I've tried

So I've decided on a temporary solution to switch the SDF library OnCallRemoteProcedure usage to the more stable INetworkSerializer API. Yes, the memory will still increase but it doesn't crash your server with of memory exception. And once again I cannot explain why.

However, since the SDF update for leaving the default Entity behavior, i haven't been able to [Net] the ISdfWorldImpl

I have tried so many Network shit on SdfWorld.cs but still unsuccessfully.

The only working behavior I have been able to do is to [Net] a Sdf2DWorld entity, but this creates some replication issues between Client/Server. In fact, the client will not follow correctly some shared behaviour. For example, when i spawn it, it will create 2 separate entities both Server and Client, and when i delete one, only the serverside is deleted. Etc.

So if you could please bring me some answers i will be happy 🙏

Regards

Metapyziks commented 1 year ago

I should have time to look into this on Wednesday, thanks for reporting!

Metapyziks commented 1 year ago

Should be good now, but please give me a poke if you still have problems with it.

PolSpock commented 1 year ago

No server crashes anymore! I am so happy, but at the same time, i feel so embarrassed that i couldn't find it myself 😳 some gaps in my knowledge of .NET 😅

Metapyziks commented 1 year ago

Your reproduction steps did 90% of the work for me, saving me so much time. I think you can be proud of that!