websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

StackOverflowException on .Net 6.0

Open michaelmeling opened this issue 3 years ago • 4 comments

I kept running into StackOverflowException when compiled on .Net 6.0, but not .Net 5.0. The issue was mostly happening on fast networks with large packages (1MB). The reason for this was that websocket-sharp uses recursion when receiving packages. After 49 recursions, it ran out of stack memory.

I believe Net6.0 introduced some network optimiazations by increaseing buffers, or similar.

Not sure this is something everyone is exeprienceing, but the recursive architecture is inherently prone to this problem, so I suspect this is going to happen more and more as people switch to Net6.0.

As a possible solution, I will upload a pull request that flattens the receive function.

michaelmeling avatar Aug 01 '22 14:08 michaelmeling

I believe I'm running into this issue but it's not manifesting as a StackOverflow image

Running on .NET 6.0, haven't tried .NET 5.0.

Craig-J avatar Sep 14 '22 16:09 Craig-J

What makes you think this is the same issue. Is it because it only manifests in Net6.0?

Try switching between Net5 and Net6. I immediately had different results between them.

michaelmeling avatar Sep 15 '22 14:09 michaelmeling

The problem I'm having does not occur in a .NET 5 build. It's relatively easy to trigger in a .NET 6 by hammering the websocket with messages but .NET 5 handles it.

Craig-J avatar Sep 26 '22 10:09 Craig-J

That does sound similar to what I saw. NET6 would quickly fail when rapidly sending many large messages, whereas NET5 would stay fine. It should be noted that this was only client side. I narrowed it down to BeginRead(byte[] buffer, ...).

Have you tried the pull request https://github.com/michaelmeling/websocket-sharp ?

michaelmeling avatar Sep 27 '22 14:09 michaelmeling