websocket-sharp icon indicating copy to clipboard operation
websocket-sharp copied to clipboard

WebSocket becomes unresponsive after 24 to 48 hours, no exception occurs.

Open roschler opened this issue 8 years ago • 3 comments

My app uses websocket-sharp for a Slack bot that is "always on". It works well, but after about 24 to 48 hours, the bot just stops responding. I have try/catch blocks on each of the WebSocket event handlers (OnError, OnMessage, etc.), that log all Exceptions. However, when the app goes into it's "zombie" state, I don't see any error messages in the log. I have the app currently running in the Visual Studio 2013 IDE, and there are not errors in the Output window either.

If I try to disconnect (close the socket) from the app when it's in this state, I get the following error:

/2/2016 9:16:28 AM|Fatal|<>c__DisplayClass17.<startReceiving>b__16:1711|System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond at System.Net.Sockets.Socket.EndReceive(IAsyncResult asyncResult) at System.Net.Sockets.NetworkStream.EndRead(IAsyncResult asyncResult) --- End of inner exception stack trace --- at System.Net.Security._SslStream.EndRead(IAsyncResult asyncResult) at System.Net.Security.SslStream.EndRead(IAsyncResult asyncResult) at WebSocketSharp.Ext.<>c__DisplayClass9.<ReadBytesAsync>b__8(IAsyncResult ar) in c:\Visual Studio 2012\Projects\Windows Forms\WebSocketSharp\websocket-sharp\Ext.cs:line 648

At this point I have to restart the app.

Is there some kind of a proactive operation I can do on my end to check if the WebSocket is actually functional or not? Something I could put in an async loop?

Also, is there a way to reinitialize the base websocket-sharp library in case the problem is some kind of a corruption deep within the library? I'd prefer not to have to have the app self-close and restart when this happens.

roschler avatar Mar 02 '16 15:03 roschler

Same issue

jaydensmith avatar Oct 13 '16 07:10 jaydensmith

@jaydensmith Have the same issue. When the internet is lost, OnClose is not called as expected.

The best way to fix this use case for me has been to check for network availability using NetworkChange.NetworkAvailabilityChanged. You'll know when Internet is lost and when it's back.

NetworkChange.NetworkAvailabilityChanged += (sender, args) =>
{
  if (args.IsAvailable && _isRunning)
  {
    LogHelper.LogMessage("Internet is back, reconnecting...");

    Stop();
    Start();
  }
};

I don't really care when I loose Internet because this is a Windows service but you could certainly check it using !args.IsAvailable

eXon avatar Oct 24 '16 03:10 eXon

@jaydensmith Have the same issue. When the internet is lost, OnClose is not called as expected.

The best way to fix this use case for me has been to check for network availability using NetworkChange.NetworkAvailabilityChanged. You'll know when Internet is lost and when it's back.

NetworkChange.NetworkAvailabilityChanged += (sender, args) =>
{
  if (args.IsAvailable && _isRunning)
  {
    LogHelper.LogMessage("Internet is back, reconnecting...");

    Stop();
    Start();
  }
};

I don't really care when I loose Internet because this is a Windows service but you could certainly check it using !args.IsAvailable

i just use localhost and get the same unresponsive

ashfrog avatar Jul 19 '22 03:07 ashfrog