pusher / pusher-websocket-dotnet

Pusher Channels Client Library for .NET
MIT License
112 stars 113 forks source link

System.InvalidOperationException (Connection.websocket_Opened) #91

Closed jogyl closed 3 years ago

jogyl commented 4 years ago

I am using C# PusherClient 1.1.1 (Nuget) in a Xamarin mobile application and I am getting multiple crashes during testing in this library. It seems as there is some error handling missing and that the lib is sensitive to the communication challanges of mobile networks. Here is the stack trace:

TaskCompletionSource`1[TResult].SetResult (TResult result) Connection.websocket_Opened (System.Object sender, System.EventArgs e) WebSocket.OnHandshaked () Handshake.ExecuteCommand (WebSocket4Net.WebSocket session, WebSocket4Net.WebSocketCommandInfo commandInfo) WebSocket.ExecuteCommand (WebSocket4Net.WebSocketCommandInfo commandInfo) WebSocket.OnDataReceived (System.Byte[] data, System.Int32 offset, System.Int32 length) WebSocket.client_DataReceived (System.Object sender, SuperSocket.ClientEngine.DataEventArgs e) ClientSession.OnDataReceived (System.Byte[] data, System.Int32 offset, System.Int32 length) AuthenticatedStreamTcpSession+d12.MoveNext () AsyncMethodBuilderCore+<>c.b6_1 (System.Object state) QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) IThreadPoolWorkItem.ExecuteWorkItem () ThreadPoolWorkQueue.Dispatch () _ThreadPoolWaitCallback.PerformWaitCallback ()

damdo commented 4 years ago

Hey @jogyl thank you for reporting this. We recently closed various bugs around the Disconnection/Connection/Re-Connection logic ( #94 #95 ).

Could you please try the latest version of the library (PusherClient v1.1.2) and let us know if you are still experiencing this? Thank you.

jogyl commented 4 years ago

@damdo will do, just added it to a new deployment. Guess it will take a some time to see real results (as lack of crashes in AppCenter) but I will get back to you.

damdo commented 4 years ago

Hey @jogyl, did the newest release (PusherClient v1.1.2) fix your issue?

MarkoPielic commented 4 years ago

Hi @damdo , I am using PusherClient v1.1.2 and I am getting the same issue:

An attempt was made to transition a task to a final state when it had already completed. at System.Threading.Tasks.TaskCompletionSource`1.SetResult(TResult result) at PusherClient.Connection.websocket_Opened(Object sender, EventArgs e) at WebSocket4Net.WebSocket.OnHandshaked() at WebSocket4Net.Command.Handshake.ExecuteCommand(WebSocket session, WebSocketCommandInfo commandInfo) at WebSocket4Net.WebSocket.OnDataReceived(Byte[] data, Int32 offset, Int32 length) at SuperSocket.ClientEngine.AuthenticatedStreamTcpSession.OnDataRead(IAsyncResult result) at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken) at System.Net.Security._SslStream.ProcessFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ReadFrameCallback(AsyncProtocolRequest asyncRequest) at System.Net.AsyncProtocolRequest.CompleteRequest(Int32 result) at System.Net.FixedSizeReader.CheckCompletionBeforeNextRead(Int32 bytes) at System.Net.FixedSizeReader.ReadCallback(IAsyncResult transportResult) at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.ContextAwareResult.Complete(IntPtr userToken) at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken) at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped nativeOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped pOVERLAP)

nelsonvega commented 4 years ago

I'm getting the same exception

chetanshingala commented 4 years ago

We are facing same issue. It looks like it is triggering racing condition some where. So changing SetResult to TrySetResult in websocket_Opened and websocket_Closed may resolve this issue.

Stack traces from appcenter

TaskCompletionSource`1[TResult].SetResult (TResult result) Connection.websocket_Opened (System.Object sender, System.EventArgs e) WebSocket.OnHandshaked () Handshake.ExecuteCommand (WebSocket4Net.WebSocket session, WebSocket4Net.WebSocketCommandInfo commandInfo) WebSocket.ExecuteCommand (WebSocket4Net.WebSocketCommandInfo commandInfo) WebSocket.OnDataReceived (System.Byte[] data, System.Int32 offset, System.Int32 length) WebSocket.client_DataReceived (System.Object sender, SuperSocket.ClientEngine.DataEventArgs e) ClientSession.OnDataReceived (System.Byte[] data, System.Int32 offset, System.Int32 length) AuthenticatedStreamTcpSession.ReadAsync () AsyncMethodBuilderCore+<>c.b__7_1 (System.Object state) QueueUserWorkItemCallback.WaitCallback_Context (System.Object state) ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) IThreadPoolWorkItem.ExecuteWorkItem () ThreadPoolWorkQueue.Dispatch () _ThreadPoolWaitCallback.PerformWaitCallback ()

jogyl commented 3 years ago

Hi @damdo,

I am sorry for not getting back on the 1.1.2 release and the issue reported. Unfortunately the same error is present in this release also. Unhandled exceptions are being thrown from async handlers from websocket_Opened event, SocketAsyncEventCompleted event and even down (originating from?) SendHandshake in WebSocket4Net.

This error seems to be due to an ongoing async call or callback that missfire when the network connection is lost. I am using PusherClient in a Xamarin app on mobile devices that move around in buildings/basements with poor network coverage. Another unfortunate side affect part from the exception is that PusherClient sometimes do not recover from the error and stops working.

The stack traces posted above gives good clues so I am not posting yet another...

elverkilde commented 3 years ago

This issue was fixed in #104 which was released as version 2.0.0. Please be aware that this is a major release, with several breaking changes. We have written a migration guide here:

https://github.com/pusher/pusher-websocket-dotnet#migrating-from-version-1-to-version-2