THRIFT-1412 Thrift Transport classes should manage the lifetime of objects implementing IDisposable by implementing IDisposable themselves
Patch: Joshua Garvin

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1325013 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/csharp/src/Server/TSimpleServer.cs b/lib/csharp/src/Server/TSimpleServer.cs
index c6fd99e..1099fc1 100644
--- a/lib/csharp/src/Server/TSimpleServer.cs
+++ b/lib/csharp/src/Server/TSimpleServer.cs
@@ -95,39 +95,35 @@
 				TProtocol outputProtocol = null;
 				try
 				{
-					client = serverTransport.Accept();
-					if (client != null)
-					{
-						inputTransport = inputTransportFactory.GetTransport(client);
-						outputTransport = outputTransportFactory.GetTransport(client);
-						inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
-						outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
-						while (processor.Process(inputProtocol, outputProtocol)) { }
-					}
-				}
-				catch (TTransportException ttx)
-				{
-					// Client died, just move on
-					if (stop)
-					{
-						logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
-					}
-				}
-				catch (Exception x)
-				{
-					logDelegate(x.ToString());
-				}
-
-				if (inputTransport != null)
-				{
-					inputTransport.Close();
-				}
-
-				if (outputTransport != null)
-				{
-					outputTransport.Close();
-				}
-			}
+          using(client = serverTransport.Accept())
+          {
+            if (client != null)
+            {
+              using(inputTransport = inputTransportFactory.GetTransport(client))
+              {
+                using (outputTransport = outputTransportFactory.GetTransport(client))
+                {
+                  inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
+                  outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
+                  while (processor.Process(inputProtocol, outputProtocol)) { }
+                }
+              }
+            }
+          }
+        }
+        catch (TTransportException ttx)
+        {
+          // Client died, just move on
+          if (stop)
+          {
+            logDelegate("TSimpleServer was shutting down, caught " + ttx.GetType().Name);
+          }
+        }
+        catch (Exception x)
+        {
+          logDelegate(x.ToString());
+        }
+      }
 
 			if (stop)
 			{
diff --git a/lib/csharp/src/Server/TThreadPoolServer.cs b/lib/csharp/src/Server/TThreadPoolServer.cs
index cf8354e..b257fd8 100644
--- a/lib/csharp/src/Server/TThreadPoolServer.cs
+++ b/lib/csharp/src/Server/TThreadPoolServer.cs
@@ -75,16 +75,20 @@
 			:base(processor, serverTransport, inputTransportFactory, outputTransportFactory,
 				  inputProtocolFactory, outputProtocolFactory, logDel)
 		{
-			if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads))
-			{
-				throw new Exception("Error: could not SetMinThreads in ThreadPool");
-			}
-			if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads))
-			{
-				throw new Exception("Error: could not SetMaxThreads in ThreadPool");
+      lock (typeof(TThreadPoolServer))
+      {
+        if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads))
+        {
+          throw new Exception("Error: could not SetMinThreads in ThreadPool");
+        }
+        if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads))
+        {
+          throw new Exception("Error: could not SetMaxThreads in ThreadPool");
+        }
 			}
 		}
 
+
 		/// <summary>
 		/// Use new ThreadPool thread for each new client connection
 		/// </summary>
diff --git a/lib/csharp/src/Server/TThreadedServer.cs b/lib/csharp/src/Server/TThreadedServer.cs
index f2be073..8e73bb7 100644
--- a/lib/csharp/src/Server/TThreadedServer.cs
+++ b/lib/csharp/src/Server/TThreadedServer.cs
@@ -185,14 +185,18 @@
 			TProtocol outputProtocol = null;
 			try
 			{
-				inputTransport = inputTransportFactory.GetTransport(client);
-				outputTransport = outputTransportFactory.GetTransport(client);
-				inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
-				outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
-				while (processor.Process(inputProtocol, outputProtocol))
-				{
-					//keep processing requests until client disconnects
-				}
+        using (inputTransport = inputTransportFactory.GetTransport(client))
+        {
+          using (outputTransport = outputTransportFactory.GetTransport(client))
+          {
+            inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
+            outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
+            while (processor.Process(inputProtocol, outputProtocol))
+            {
+              //keep processing requests until client disconnects
+            }
+          }
+        }
 			}
 			catch (TTransportException)
 			{
@@ -202,15 +206,6 @@
 				logDelegate("Error: " + x);
 			}
 
-			if (inputTransport != null)
-			{
-				inputTransport.Close();
-			}
-			if (outputTransport != null)
-			{
-				outputTransport.Close();
-			}
-
 			lock (clientLock)
 			{
 				clientThreads.Remove(Thread.CurrentThread);