kerryjiang / WebSocket4Net

A popular .NET WebSocket Client
Apache License 2.0
764 stars 273 forks source link

Websocket hangs on certain proxies #54

Open ste-art opened 8 years ago

ste-art commented 8 years ago

Websocket can hang if used with some proxies.
After calling WebSocket.Open no events (OnWsClosed, OnWsOpened, OnWsError) are raised and some internal thread starts to generate high CPU usage.

More info: ProxyConnectorBase.AsyncEventArgsCompleted receives an empty buffer - SocketAsyncEventArgs.Buffer is filled with zeros, Offset and BytesTransferred are 0. It calls HttpConnectProxy.ProcessReceive where e.Buffer.SearchMark returns -1 and executes this code:

            if (result < 0)
            {
                int total = e.Offset + e.BytesTransferred;

                if(total >= m_ReceiveBufferSize)
                {
                    OnException("receive buffer size has been exceeded");
                    return;
                }

                e.SetBuffer(total, m_ReceiveBufferSize - total);
                StartReceive(context.Socket, e);
                return;
            }

total becomes 0 too, then StartReceive is called, that raises ProxyConnectorBase.AsyncEventArgsCompleted with empty buffer again and this loop continues infinitely.

I suggest a fix:

                if (e.BytesTransferred == 0)
                {
                    OnException("receive buffer is empty");
                    return;
                }

But I'm not sure could BytesTransferred be zero in other healthy TCP connections.

Proxies to debug the issue with (there are plenty of them in varios online free proxies lists): 111.13.136.46:80 139.162.182.113:80 202.43.147.226:1080

kerryjiang commented 7 years ago

The situation "e.BytesTransferred == 0" means the connection was closed.

bogdandanielb commented 6 years ago

I'm experiencing the same CPU intensive processing threads left behind on non-working proxies even though the WebSocket is properly disposed. Was this issue addressed in the latest release?

image

Thanks, Daniel