Merge pull request #1225 from strawd/bug22307
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Dispatcher / ChannelDispatcher.cs
index 690e996d903200a36233dbecebc0f31de7e409ff..65b5c8cf38ae6913b2482b54609ecc8ee45064e4 100644 (file)
@@ -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
 {
@@ -444,7 +447,7 @@ namespace System.ServiceModel.Dispatcher
                        {
                                channels.Remove (ch); // zonbie, if exists
                                var ich = ch as ISessionChannel<IInputSession>;
-                               List<IChannel> l;
+                               
                                if (ich != null && ich.Session != null)
                                        sessions.Remove (ich.Session);
                        }
@@ -452,8 +455,11 @@ 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));
@@ -581,10 +587,14 @@ namespace System.ServiceModel.Dispatcher
                        {
                                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)
@@ -598,7 +608,10 @@ namespace System.ServiceModel.Dispatcher
                                        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 {