Merge pull request #1156 from felfert/master
authorMartin Baulig <martin.baulig@xamarin.com>
Thu, 22 Jan 2015 16:21:24 +0000 (17:21 +0100)
committerMartin Baulig <martin.baulig@xamarin.com>
Thu, 22 Jan 2015 16:21:24 +0000 (17:21 +0100)
Fixed Bug #21135 - Deadlock in WebConnectionStream

1  2 
mcs/class/System/System.Net/WebConnectionStream.cs

index 8ef558d72fd997d284c76319ee04b76159d0b7e6,72977c4ce70726e8dde5e717da86047343e34111..87f60363da8a4306b543420ccf085a142b764d2e
@@@ -234,7 -234,8 +234,8 @@@ namespace System.Ne
                                return;
                        }
  
-                       pending.WaitOne ();
+                       if (!pending.WaitOne (ReadTimeout))
+                               throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
                        lock (locker) {
                                if (totalRead >= contentLength)
                                        return;
                        if (result.EndCalled)
                                return;
  
+                       if (sendChunked) {
+                               lock (locker) {
+                                       pendingWrites--;
+                                       if (pendingWrites <= 0)
+                                               pending.Set ();
+                               }
+                       }
                        result.EndCalled = true;
                        if (result.AsyncWriteAll) {
                                result.WaitUntilComplete ();
  
                        if (result.GotException)
                                throw result.Exception;
-                       if (sendChunked) {
-                               lock (locker) {
-                                       pendingWrites--;
-                                       if (pendingWrites == 0)
-                                               pending.Set ();
-                               }
-                       }
                }
                
                public override void Write (byte [] buffer, int offset, int size)
                        disposed = true;
                }
  
 +              internal bool GetResponseOnClose {
 +                      get; set;
 +              }
 +
                public override void Close ()
                {
 +                      if (GetResponseOnClose) {
 +                              if (disposed)
 +                                      return;
 +                              disposed = true;
 +                              var response = (HttpWebResponse)request.GetResponse ();
 +                              response.ReadAll ();
 +                              response.Close ();
 +                              return;
 +                      }
 +
                        if (sendChunked) {
                                if (disposed)
                                        return;
                                disposed = true;
-                               pending.WaitOne ();
+                               if (!pending.WaitOne (WriteTimeout)) {
+                                       throw new WebException ("The operation has timed out.", WebExceptionStatus.Timeout);
+                               }
                                byte [] chunk = Encoding.ASCII.GetBytes ("0\r\n\r\n");
                                string err_msg = null;
                                cnc.Write (request, chunk, 0, chunk.Length, ref err_msg);