NullReferenceException when app paused while websocket is connecting

Mar 12, 2013 at 2:59 PM
Hello, I've encountered a strange error when my application is paused while the websocket is still connecting. Below is the stack-trace:
$exception  {System.NullReferenceException: NullReferenceException
   at System.Net.Sockets.Socket.ResetHandle()
   at System.Net.Sockets.SocketNotificationCallback.Paused(XPauseType pauseType)
   at System.Net.Sockets.SocketNotificationManager.Paused(XPauseType pauseType)
   at MS.Internal.Interop.SocketNotificationCallbackProxy.Paused(XPauseType pauseType)
   at MS.Internal.Interop.NotificationService.NotificationPaused(XPauseType pauseType)
   at MS.Internal.FrameworkCallbacks.NotificationPaused(UInt32 pauseType)}  System.Exception {System.NullReferenceException}
This error occurs only if the websocket is still connecting or when the remote hosts returns "No connection could be made because the target machine actively refused it" (then the websocket stays in Connecting state). When the connection is successful then this error does not occur.

What bothers me is that there's no reference to my code (nor WebSocket4Net code) in the stack-trace, yet the error occurs only if I try to connect the websocket. I connect the websocket in my App.xaml.cs:
private void OnWebSocketMessageReceived(object sender, MessageReceivedEventArgs eventArguments) {
  System.Diagnostics.Debug.WriteLine("[OnWebSocketMessageReceived] Message: " + eventArguments.Message);
}

private void OnWebSocketError(object sender, SuperSocket.ClientEngine.ErrorEventArgs eventArguments) {
  System.Diagnostics.Debug.WriteLine("[OnWebSocketError] Error: " + eventArguments.Exception.Message);
}

private void InitializeWebSocket() {
  if (websocket != null) {
    return;
  }
  websocket = new WebSocket("ws://myhost:myport");
  websocket.MessageReceived += OnWebSocketMessageReceived;
  websocket.Error += OnWebSocketError;
  websocket.Open();
}
Coordinator
Mar 12, 2013 at 3:12 PM
Are you using the latest source code in the repository?
If not, try it.
From: [email removed]
Sent: Tuesday, March 12, 2013 10:59 PM
To: [email removed]
Subject: NullReferenceException when app paused while websocket is connecting [websocket4net:436322]

From: goshki

Hello, I've encountered a strange error when my application is paused while the websocket is still connecting. Below is the stack-trace:
$exception  {System.NullReferenceException: NullReferenceException
   at System.Net.Sockets.Socket.ResetHandle()
   at System.Net.Sockets.SocketNotificationCallback.Paused(XPauseType pauseType)
   at System.Net.Sockets.SocketNotificationManager.Paused(XPauseType pauseType)
   at MS.Internal.Interop.SocketNotificationCallbackProxy.Paused(XPauseType pauseType)
   at MS.Internal.Interop.NotificationService.NotificationPaused(XPauseType pauseType)
   at MS.Internal.FrameworkCallbacks.NotificationPaused(UInt32 pauseType)}  System.Exception {System.NullReferenceException}
This error occurs only if the websocket is still connecting or when the remote hosts returns "No connection could be made because the target machine actively refused it" (then the websocket stays in Connecting state). When the connection is successful then this error does not occur.

What bothers me is that there's no reference to my code (nor WebSocket4Net code) in the stack-trace, yet the error occurs only if I try to connect the websocket. I connect the websocket in my App.xaml.cs:
private void OnWebSocketMessageReceived(object sender, MessageReceivedEventArgs eventArguments) {
  System.Diagnostics.Debug.WriteLine("[OnWebSocketMessageReceived] Message: " + eventArguments.Message);
}

private void OnWebSocketError(object sender, SuperSocket.ClientEngine.ErrorEventArgs eventArguments) {
  System.Diagnostics.Debug.WriteLine("[OnWebSocketError] Error: " + eventArguments.Exception.Message);
}

private void InitializeWebSocket() {
  if (websocket != null) {
    return;
  }
  websocket = new WebSocket("ws://myhost:myport");
  websocket.MessageReceived += OnWebSocketMessageReceived;
  websocket.Error += OnWebSocketError;
  websocket.Open();
}
Mar 13, 2013 at 9:02 AM
kerryjiang wrote:
Are you using the latest source code in the repository? If not, try it.
Thanks for a quick reply. I've tried downloading WebSocket4Net source code from Github and incorporating it my project but there are compilation problems (it's a WP71 project). Do I have to rebuild the whole project to use the latest version from repository?

I've also noticed that the NullReferenceException is thrown only on WP8 phone. WP78 phone does not throw this exception when the application is deactivated.
Coordinator
Mar 13, 2013 at 9:23 AM
There are WP8 project/solution files existing for WebSocket4Net. Do you use them?
Mar 13, 2013 at 9:52 AM
I have to use WP71 version because the project is targeted for both WP7 and WP8 phones. I've rebuilt WebSocket4Net from source (latest version from Github) and I've attached the DLL in my project (it's using the sl40-windowsphone71 version). Unfortunately the problem is still there.
Coordinator
Mar 13, 2013 at 12:10 PM
You can have a try on wp8 assembly!

Sent from my Windows Phone

From: goshki
Sent: 3/13/2013 17:52
To: kerry-jiang@hotmail.com
Subject: Re: NullReferenceException when app paused while websocket is connecting [websocket4net:436322]

From: goshki

I have to use WP71 version because the project is targeted for both WP7 and WP8 phones. I've rebuilt WebSocket4Net from source (latest version from Github) and I've attached the DLL in my project (it's using the sl40-windowsphone71 version). Unfortunately the problem is still there.
Mar 13, 2013 at 12:24 PM
kerryjiang wrote:
You can have a try on wp8 assembly!
I've just tried it but it's not working either. Still the same NullReferenceException error when the application is deactivated. I've activated "Show threads in source" and it says that the exception is thrown in application's main thread.
Coordinator
Mar 13, 2013 at 12:35 PM
Could you give me a screenshot of that exception? It would be best if you can give me the full stacktrace of that exception.

Sent from my Windows Phone

From: goshki
Sent: 3/13/2013 20:24
To: kerry-jiang@hotmail.com
Subject: Re: NullReferenceException when app paused while websocket is connecting [websocket4net:436322]

From: goshki

kerryjiang wrote:
You can have a try on wp8 assembly!
I've just tried it but it's not working either. Still the same NullReferenceException error when the application is deactivated. I've activated "Show threads in source" and it says that the exception is thrown in application's main thread.
Mar 13, 2013 at 12:55 PM
Yes, it's exactly the same exception as the one I've provided earlier:
$exception  {System.NullReferenceException: NullReferenceException
   at System.Net.Sockets.Socket.ResetHandle()
   at System.Net.Sockets.SocketNotificationCallback.Paused(XPauseType pauseType)
   at System.Net.Sockets.SocketNotificationManager.Paused(XPauseType pauseType)
   at MS.Internal.Interop.SocketNotificationCallbackProxy.Paused(XPauseType pauseType)
   at MS.Internal.Interop.NotificationService.NotificationPaused(XPauseType pauseType)
   at MS.Internal.FrameworkCallbacks.NotificationPaused(UInt32 pauseType)}  System.Exception {System.NullReferenceException}
Coordinator
Apr 22, 2013 at 9:45 AM
I have done some changes about the WebSocket4Net WP8 version, could you download the latest source from the repository?
May 15, 2013 at 7:17 AM
Edited May 15, 2013 at 8:02 AM
Hello, sorry for replying so late. I've downloaded the latest version from Github repository. I'm able to use the Silverlight WP71 version but not the Silverlight WP8 version (missing references to SuperSocket). That is, I can build the WP8 version DLL but I cannot use it in my project because it's targeted for WP71. I've tried including the Silverlight WP71 DLL built from the latest Github version and my project compiles and runs but I still get the exception when running the project on a WP8 device/emulator.
$exception  {System.NullReferenceException: NullReferenceException
   at System.Net.Sockets.Socket.ResetHandle()
   at System.Net.Sockets.SocketNotificationCallback.Paused(XPauseType pauseType)
   at System.Net.Sockets.SocketNotificationManager.Paused(XPauseType pauseType)
   at MS.Internal.Interop.SocketNotificationCallbackProxy.Paused(XPauseType pauseType)
   at MS.Internal.Interop.NotificationService.NotificationPaused(XPauseType pauseType)
   at MS.Internal.FrameworkCallbacks.NotificationPaused(UInt32 pauseType)}  System.Exception {System.NullReferenceException}
To reproduce the error I do the following:
  1. Create a WP71 project and include WebSocket4Net reference, the project tries to connect to a websocket in the App constructor.
  2. Switch the WP8 device to "Airplane mode" (no wifi/cellular connection); alternately start the WP8 emulator and simulate "No network" in Simulation Dashboard
  3. Install and run the application on the WP8 device/emulator (it's important that there should be no Internet connection so the device cannot be connected to the computer, the emulator uses computer connection as well, even with Airplane mode turned on, thus the simulated "No network" in Simulation Dashboard is required)
  4. Switch the application to background by pressing the HOME key on the device/emulator (the Windows logo key)
  5. As soon as the application switches to background an exception is thrown (the one referenced above)
If you'd like, I could provide a test project so you can test the error firsthand.
Coordinator
May 15, 2013 at 2:16 PM
Sorry, I don;t have a WP8 debug environment, or I can help you figure this issue out.
Jul 14, 2014 at 11:55 AM
Did you find out the solution to this issue? I am also facing the crash with the same stack trace. Please share the fix if you have found one.

Thanks.
Coordinator
Jul 14, 2014 at 1:38 PM
Which version of websocket4net are you using?
Jul 14, 2014 at 2:26 PM
It's 0.9.0.0 I think that was the latest one by the time we started our project.
Jul 15, 2014 at 5:19 AM
@kerryjiang Could you please help me get this resolved? It's kind of urgent for my project. Please let me know if there is any fix/workaround to this.
Coordinator
Jul 17, 2014 at 4:26 AM
I also have no idea about this exception. Could you catch this exception and ignore it?
Jul 17, 2014 at 6:00 AM
In our code, I don't think we can catch this exception anywhere (or can we?) as websocket is doing it internally while application is deactivating. So, It is always being triggered as an unhandled exception.
Coordinator
Jul 17, 2014 at 9:16 AM
Could help me check which thread the exception come from?
Mar 16, 2015 at 5:56 AM
I have fixed this issue in my own supersocket branch. It's not websocket4net issue. It's not supersocket.clientengine issue, either. I think the root cause of this crash is Windows Phone's defects. I fixed it by changing the Socket API supersocket.clientengine use. You can find the forked supersocket.clientengine source code which fixed this issue at https://github.com/fanglinliu/SuperSocket.ClientEngine. The code changes are https://github.com/fanglinliu/SuperSocket.ClientEngine/commit/e461226684d68221ecff821c6c875d0f6ee36cbf.