Fix NREX in Linux network change notification code
authorMarek Habersack <grendel@twistedcode.net>
Thu, 3 Sep 2015 21:14:46 +0000 (23:14 +0200)
committerMarek Habersack <grendel@twistedcode.net>
Thu, 3 Sep 2015 21:19:22 +0000 (23:19 +0200)
Linux code uses a netlink socket to be notified about network interface
changes. The code is a bit racy and for that reson it can happen that
the LinuxNetworkChange.OnDataAvailable handler is called after the
socket is destroyed (from a timer event) and socket stored in an
instance variable is already null. This is a race most probably
introduced by the new threadpool or TPL code as it started to happen in
several places (mostly BCL tests on Android, but it's *not* specific to
Android).
The workaround here serves as a quick band-aid to be able to move on,
but a more thorough review of the socket code will be required.

mcs/class/System/System.Net.NetworkInformation/NetworkChange.cs

index 3b06a4e79da6baccdc69864570818889f784868a..357217659964fea2aabcd59beb116fe8b5866d08 100644 (file)
@@ -442,8 +442,10 @@ namespace System.Net.NetworkInformation {
 
                unsafe void OnDataAvailable (object sender, SocketAsyncEventArgs args)
                {
+                       if (nl_sock == null) // Recent changes in Mono cause MaybeCloseSocket to be called before OnDataAvailable
+                               return;
                        EventType type;
-                       fixed (byte *ptr = args.Buffer) {       
+                       fixed (byte *ptr = args.Buffer) {
                                type = ReadEvents (nl_sock.Handle, new IntPtr (ptr), args.BytesTransferred, 8192);
                        }
                        nl_sock.ReceiveAsync (nl_args);