Merge pull request #1225 from strawd/bug22307
authorMiguel de Icaza <miguel@gnome.org>
Wed, 23 Sep 2015 19:34:55 +0000 (15:34 -0400)
committerMiguel de Icaza <miguel@gnome.org>
Wed, 23 Sep 2015 19:34:55 +0000 (15:34 -0400)
Fix for bug 22307 - Intermittent IndexOutOfRangeException on closing connection

1  2 
mcs/class/System.ServiceModel/System.ServiceModel.Dispatcher/ChannelDispatcher.cs

index c005094275c5a9c5020dc612fd811eeb8fa8af98,10834fc0ae81deafcb1f22b0a54bd2ab3f12b939..65b5c8cf38ae6913b2482b54609ecc8ee45064e4
@@@ -35,9 -35,6 +35,9 @@@ using System.Threading
  using System.Transactions;
  using System.ServiceModel;
  using System.ServiceModel.Description;
 +using System.Net.Sockets;
 +using System.Xml;
 +using System.IO;
  
  namespace System.ServiceModel.Dispatcher
  {
                        public void CloseInput ()
                        {
                                foreach (var ch in channels.ToArray ()) {
-                                       if (ch.State == CommunicationState.Closed)
-                                               RemoveChannel (ch);
+                                       if (ch.State == CommunicationState.Closed) {
+                                               lock (channels) {
+                                                       RemoveChannel (ch);
+                                               }
+                                       }
                                        else {
                                                try {
                                                        ch.Close (close_timeout - (DateTime.Now - close_started));
                        {
                                Message msg;
                                var input = (IInputChannel) result.AsyncState;
 -                              if (input.EndTryReceive (result, out msg))
 -                                      ProcessInput (input, msg);
 -                              else
 +                              try {
 +                                      if (input.EndTryReceive (result, out msg))
 +                                              ProcessInput (input, msg);
 +                                      else
 +                                              input.Close ();
 +                              } catch (ObjectDisposedException) {
                                        input.Close ();
 +                              }
                        }
  
                        void ProcessRequest (IReplyChannel reply, RequestContext rc)
                                        if (ProcessErrorWithHandlers (reply, ex, out res))
                                                return;
  
 -                                      rc.Reply (res);
 +                                      if ((!(ex is SocketException)) && 
 +                                          (!(ex is XmlException)) &&
 +                                          (!(ex is IOException)))
 +                                              rc.Reply (res);
                                        
                                        reply.Close (owner.DefaultCloseTimeout); // close the channel
                                } finally {