kerryjiang / WebSocket4Net

A popular .NET WebSocket Client
752 stars 272 forks source link

Getting System.ArgumentException: Destination array was not long enough. when re-opening a stream #117

Open bohao-cao opened 6 years ago

bohao-cao commented 6 years ago

Environment: Dot net core application running in a docker container.

Issue

  1. Web socket connection is broken
  2. Try to reset the socket by following https://github.com/kerryjiang/WebSocket4Net/issues/100
  3. Get SocketException from webSocket's error handler. Error:System.Net.Sockets.SocketException (0x80004005): Transport endpoint is not connected
  4. Get close event from webSocket's close handler.
  5. Get unhandled Exception. Stack trace:

Unhandled Exception: System.ArgumentException: Destination array was not long enough. Check the destination index, length, and the array's lower bounds. Parameter name: destinationArray at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable) at WebSocket4Net.Common.ArraySegmentList1.CopyTo(T[] to, Int32 srcIndex, Int32 toIndex, Int32 length) at WebSocket4Net.WebSocketCommandInfo..ctor(IList1 frames) at WebSocket4Net.Protocol.DraftHybi10DataReader.GetCommandInfo(Byte[] readBuffer, Int32 offset, Int32 length, Int32& left) at WebSocket4Net.WebSocket.OnDataReceived(Byte[] data, Int32 offset, Int32 length) at SuperSocket.ClientEngine.AsyncTcpSession.ProcessReceive(SocketAsyncEventArgs e) at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e) at System.Net.Sockets.SocketAsyncEventArgs.ExecutionCallback(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncSuccess(Int32 bytesTransferred, SocketFlags flags) at System.Net.Sockets.SocketAsyncEventArgs.CompletionCallback(Int32 bytesTransferred, SocketFlags flags, SocketError socketError) at System.Net.Sockets.SocketAsyncEventArgs.TransferCompletionCallbackCore(Int32 bytesTransferred, Byte[] socketAddress, Int32 socketAddressSize, SocketFlags receivedFlags, SocketError socketError) at System.Net.Sockets.SocketAsyncContext.ReceiveOperation.InvokeCallback() at System.Net.Sockets.SocketAsyncContext.AsyncOperation.<>c.b__14_0(Object o) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadPoolWorkQueue.Dispatch()

kerryjiang commented 6 years ago

Can it be reproduced outside docker?

bohao-cao commented 6 years ago

This issue happened only once under stress test.

bohao-cao commented 6 years ago

ArraySegmentList.CopyTo(T[] to, int srcIndex, int toIndex, int length) ln 492 throws. A naive example will throw the same argument exception. int[] a = { 1, 2, 3}; int[] b = { 9,10,11}; Array.Copy(a,0,b,1,3);

kerryjiang commented 6 years ago

Ok, I'll check this issue soon. That would be great, if you provide some test cases.