Avoid killing the thread if socket already null
authorGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Mon, 1 Nov 2010 22:52:58 +0000 (18:52 -0400)
committerGonzalo Paniagua Javier <gonzalo.mono@gmail.com>
Mon, 1 Nov 2010 22:54:14 +0000 (18:54 -0400)
In some weird cases socket is null when we try to close it.

mcs/class/System/System.Net/HttpConnection.cs

index e331e341f87412c0b1ca07dcdbb51cf61115364f..c4921284bba37860f673e9154b4775d19b1fe592 100644 (file)
@@ -135,7 +135,7 @@ namespace System.Net {
                                timer.Change (s_timeout, Timeout.Infinite);
                                stream.BeginRead (buffer, 0, BufferSize, OnRead, this);
                        } catch {
-                               sock.Close (); // stream disposed
+                               CloseSocket ();
                        }
                }
 
@@ -184,14 +184,14 @@ namespace System.Net {
                                if (ms != null && ms.Length > 0)
                                        SendError ();
                                if (sock != null)
-                                       sock.Close ();
+                                       CloseSocket ();
                                return;
                        }
 
                        if (nread == 0) {
                                //if (ms.Length > 0)
                                //      SendError (); // Why bother?
-                               sock.Close ();
+                               CloseSocket ();
                                return;
                        }
 
@@ -360,6 +360,19 @@ namespace System.Net {
                        Close (false);
                }
 
+               void CloseSocket ()
+               {
+                       if (sock == null)
+                               return;
+
+                       try {
+                               sock.Close ();
+                       } catch {
+                       } finally {
+                               sock = null;
+                       }
+               }
+
                internal void Close (bool force_close)
                {
                        if (sock != null) {
@@ -402,10 +415,12 @@ namespace System.Net {
                                Socket s = sock;
                                sock = null;
                                try {
-                                       s.Shutdown (SocketShutdown.Both);
+                                       if (s != null)
+                                               s.Shutdown (SocketShutdown.Both);
                                } catch {
                                } finally {
-                                       s.Close ();
+                                       if (s != null)
+                                               s.Close ();
                                }
                                Unbind ();
                                return;