Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled exception in WebSocket #279

Open
ilsnk opened this issue Dec 24, 2023 · 12 comments
Open

Unhandled exception in WebSocket #279

ilsnk opened this issue Dec 24, 2023 · 12 comments

Comments

@ilsnk
Copy link

ilsnk commented Dec 24, 2023

At random time we have this:

Unhandled exception. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'length')
   at System.Runtime.CompilerServices.RuntimeHelpers.GetSubArray[T](T[] array, Range range)
   at NetCoreServer.WebSocket.PrepareReceiveFrame(Byte[] buffer, Int64 offset, Int64 size) in /src/websocket-server/NetCoreServer/WebSocket.cs:line 323
   at NetCoreServer.TcpSession.ProcessReceive(SocketAsyncEventArgs e) in /src/websocket-server/NetCoreServer/TcpSession.cs:line 624
   at NetCoreServer.TcpSession.OnAsyncCompleted(Object sender, SocketAsyncEventArgs e) in /src/websocket-server/NetCoreServer/TcpSession.cs:line 567
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Net.Sockets.SocketAsyncEngine.System.Threading.IThreadPoolWorkItem.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
@ilsnk
Copy link
Author

ilsnk commented Dec 24, 2023

We use latest library version

@selmanahatli
Copy link

I have same problem with 7.0.0 version. Is there any progress about this issue?
Or could yo fix that in a different way? @ilsnk

@suleymanbyzt
Copy link

Same problem any update ?

@ilsnk
Copy link
Author

ilsnk commented Mar 1, 2024

We have not yet had time to independently study this problem. We are still getting this error.

@suleymanbyzt
Copy link

I still keep getting this error. How can I handle this? @chronoxor

Unhandled exception. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'length') at NetCoreServer.WebSocket.PrepareReceiveFrame(Byte[] buffer, Int64 offset, Int64 size) at NetCoreServer.TcpSession.ProcessReceive(SocketAsyncEventArgs e) at NetCoreServer.TcpSession.OnAsyncCompleted(Object sender, SocketAsyncEventArgs e) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

thank you for your help in advance

@MrVitaly
Copy link

@ilsnk , @selmanahatli, @suleymanbyzt, do you have a stable way to reproduce it?

@chronoxor
Copy link
Owner

It'll great if we can reproduce it and try to fix. I cannot reproduce this on my side. And we have WebSockets server on production running for months without issues.

@ilsnk
Copy link
Author

ilsnk commented Mar 24, 2024

Maybe we are not using the server implementation correctly?
We have NetCoreServer acting as a socket, sending/receiving messages through our own abstract handlers.

@ilsnk
Copy link
Author

ilsnk commented Mar 24, 2024

We can assume that the problem may be in this code.
Processing goes ProcessReceive is executed by asynchronous callback, lock is executed in OnRecieve. But since the callback (ProcessReceive) is executed from asynchronous callback code, by executing Resize we ignore the lock object

TspSession.cs:602,
WebSocket.cs:322

What do you think?

image_2024-03-24_03-08-27

@chronoxor
Copy link
Owner

Still not reproduced on my side.

For each session receive buffer resize _receiveBuffer.Reserve(size + 1); should be called after OnReceived(_receiveBuffer.Data, 0, size) -> WebSocket.PrepareReceiveFrame(buffer, offset, size) -> lock (WsReceiveLock) { ... } is executed, so the buffer should be still valid in PrepareReceiveFrame.

@daigs
Copy link

daigs commented Mar 29, 2024

今天我也出现这个bug了,导致我的服务程序直接结束进程了.
我是刚开机时(服务程序加入了开机自动启动),马上打开浏览器通过网页请求我的服务出现问题的,
当时在网页上点击按钮请求服务时卡主了一会儿,然后我就使劲点了几下按钮继续请求,然后就弹出异常窗口后服务程序挂掉了...
然后我就重启我的服务程序,但是无论如何也出现不了这种异常了...
我重写了OnReceived如何用try...catch捕获异常,防止我的服务程序挂掉
image
image

@ilsnk
Copy link
Author

ilsnk commented Apr 21, 2024

Perhaps due to an incorrect shift of payload bytes? I'm not sure what exactly the crash error is coming from, but is it worth rechecking this code?

payload = ((WsReceiveFrameBuffer[2] << 56) | (WsReceiveFrameBuffer[3] << 48) | (WsReceiveFrameBuffer[4] << 40) | (WsReceiveFrameBuffer[5] << 32) | (WsReceiveFrameBuffer[6] << 24) | (WsReceiveFrameBuffer[7] << 16) | (WsReceiveFrameBuffer[8] << 8) | (WsReceiveFrameBuffer[9] << 0));

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants