pusher / pusher-websocket-dotnet

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

NullReferenceException when disposing websocket #118

Closed MarkoPielic closed 2 years ago

MarkoPielic commented 2 years ago

I'm getting logs from users where pusher .net client is causing my C# application to crash. Is there any thing I can do to prevent this? I cannot reproduce the issue. The stack trace is below.

System.NullReferenceException: Object reference not set to an instance of an object. at PusherClient.Connection.DisposeWebsocket() at PusherClient.Connection.RecreateWebSocket() at PusherClient.Connection.WebsocketAutoReconnect(Object sender, EventArgs e) at WebSocket4Net.WebSocket.FireClosed() at WebSocket4Net.WebSocket.OnClosed() at SuperSocket.ClientEngine.ClientSession.OnClosed() at SuperSocket.ClientEngine.AuthenticatedStreamTcpSession.Close() at WebSocket4Net.Command.Close.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)

benjamin-tang-pusher commented 2 years ago

Do you ever disconnect with the DisconnectAsync() method?

MarkoPielic commented 2 years ago

@benjamin-tang-pusher Yes I do in certain cases:

  1. When devices goes offline
  2. When user locks his computer
  3. When user puts his computer to sleep

I have found that if I don't do that I just get a lot messages in the log from Pusher.Error, Pusher.ConnectionStateChanged, Pusher.Connected and Pusher.Disconnected handlers.

benjamin-tang-pusher commented 2 years ago

After some experimenting, I'm confident this is related to disconnecting as DisposeWebsocket() gets called here: https://github.com/pusher/pusher-websocket-dotnet/blob/a721e925517ba0f0dc31b33374cc437c929a6b32/PusherClient/Connection.cs#L107

Is is possible to find out which one of your cases this error occurs on?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.