Fixed deadlock in WebConnectionGroup.Close()
authorHellBrick <hellbrick@webkontrol.ru>
Thu, 26 Mar 2015 08:30:33 +0000 (11:30 +0300)
committerHellBrick <hellbrick@webkontrol.ru>
Thu, 26 Mar 2015 08:48:30 +0000 (11:48 +0300)
The fix is similar to the one used in the TryRecycle() method: we defer connection closing until after we've left the lock. The original bug: https://bugzilla.xamarin.com/show_bug.cgi?id=27348

mcs/class/System/System.Net/WebConnectionGroup.cs

index 5e142154f913704bd614466144dbe5da559aa014..3936ee428981997dd3f5700a05f8727d20ce6680 100644 (file)
@@ -66,6 +66,8 @@ namespace System.Net
 
                public void Close ()
                {
+                       List<WebConnection> connectionsToClose = null;
+
                        //TODO: what do we do with the queue? Empty it out and abort the requests?
                        //TODO: abort requests or wait for them to finish
                        lock (sPoint) {
@@ -76,7 +78,17 @@ namespace System.Net
                                        var node = iter;
                                        iter = iter.Next;
 
+                                       // Closing connections inside the lock leads to a deadlock.
+                                       if (connectionsToClose == null)
+                                               connectionsToClose = new List<WebConnection>();
+
+                                       connectionsToClose.Add (cnc);
                                        connections.Remove (node);
+                               }
+                       }
+
+                       if (connectionsToClose != null) {
+                               foreach (var cnc in connectionsToClose) {
                                        cnc.Close (false);
                                        OnConnectionClosed ();
                                }