sta / websocket-sharp

A C# implementation of the WebSocket protocol client and server
http://sta.github.io/websocket-sharp
MIT License
5.73k stars 1.66k forks source link

StackOverflowException on .Net 6.0 #702

Open michaelmeling opened 2 years ago

michaelmeling commented 2 years ago

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.

Craig-J commented 2 years ago

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.

michaelmeling commented 2 years ago

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.

Craig-J commented 2 years ago

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.

michaelmeling commented 2 years ago

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 ?