WebSocket4Net icon indicating copy to clipboard operation
WebSocket4Net copied to clipboard

Websocket hangs on certain proxies

Open ste-art opened this issue 8 years ago • 2 comments

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

ste-art avatar Aug 03 '16 11:08 ste-art

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

kerryjiang avatar Jan 15 '17 02:01 kerryjiang

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

bogdandanielb avatar Oct 18 '17 14:10 bogdandanielb