-- Servers should not crash on accept.

Summary:
- Continue running if there is an accept issue

Reviewed By: slee


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664988 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/concurrency/Exception.h b/lib/cpp/src/concurrency/Exception.h
index 67eb153..79a7a29 100644
--- a/lib/cpp/src/concurrency/Exception.h
+++ b/lib/cpp/src/concurrency/Exception.h
@@ -2,18 +2,19 @@
 #define _THRIFT_CONCURRENCY_EXCEPTION_H_ 1
 
 #include <exception>
+#include <Thrift.h>
 
 namespace facebook { namespace thrift { namespace concurrency { 
 
-class NoSuchTaskException : public std::exception {};
+class NoSuchTaskException : public facebook::thrift::TException {};
 
-class UncancellableTaskException : public std::exception {};
+class UncancellableTaskException : public facebook::thrift::TException {};
 
-class InvalidArgumentException : public std::exception {};
+class InvalidArgumentException : public facebook::thrift::TException {};
 
-class IllegalStateException : public std::exception {};
+class IllegalStateException : public facebook::thrift::TException {};
 
-class TimedOutException : public std::exception {};
+class TimedOutException : public facebook::thrift::TException {};
 
 }}} // facebook::thrift::concurrency
 
diff --git a/lib/cpp/src/server/TSimpleServer.cpp b/lib/cpp/src/server/TSimpleServer.cpp
index 1e7e7fb..f7e2673 100644
--- a/lib/cpp/src/server/TSimpleServer.cpp
+++ b/lib/cpp/src/server/TSimpleServer.cpp
@@ -28,8 +28,8 @@
   }
 
   // Fetch client from server
-  try {
-    while (true) {
+  while (true) {
+    try {
       client = serverTransport_->accept();
       inputTransport = inputTransportFactory_->getTransport(client);
       outputTransport = outputTransportFactory_->getTransport(client);
@@ -49,12 +49,26 @@
       }
       inputTransport->close();
       outputTransport->close();
+      client->close();    
+    } catch (TTransportException& ttx) {
+      inputTransport->close();
+      outputTransport->close();
       client->close();
+      cerr << "TServerTransport died on accept: " << ttx.what() << endl;
+      continue;
+    } catch (TException& tx) {
+      inputTransport->close();
+      outputTransport->close();
+      client->close();
+      cerr << "Some kind of accept exception: " << tx.what() << endl;
+      continue;
+    } catch (string s) {
+      inputTransport->close();
+      outputTransport->close();
+      client->close();
+      cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
+      break;
     }
-  } catch (TTransportException& ttx) {
-    cerr << "TServerTransport died on accept: " << ttx.what() << endl;
-  } catch (TException& tx) {
-    cerr << "Some kind of accept exception: " << tx.what() << endl;
   }
 
   // TODO(mcslee): Could this be a timeout case? Or always the real thing?
diff --git a/lib/cpp/src/server/TThreadPoolServer.cpp b/lib/cpp/src/server/TThreadPoolServer.cpp
index 8514d18..ce1b59e 100644
--- a/lib/cpp/src/server/TThreadPoolServer.cpp
+++ b/lib/cpp/src/server/TThreadPoolServer.cpp
@@ -102,8 +102,26 @@
       outputProtocol = outputProtocolFactory_->getProtocol(outputTransport);
 
       // Add to threadmanager pool
-      threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, inputProtocol, outputProtocol)));
+      threadManager_->add(shared_ptr<TThreadPoolServer::Task>(new TThreadPoolServer::Task(processor_, 
+                                                                                          inputProtocol, 
+                                                                                          outputProtocol)));
     } catch (TTransportException& ttx) {
+      inputTransport->close();
+      outputTransport->close();
+      client->close();
+      cerr << "TThreadPoolServer: TServerTransport died on accept: " << ttx.what() << endl;
+      continue;
+    } catch (TException& tx) {
+      inputTransport->close();
+      outputTransport->close();
+      client->close();
+      cerr << "TThreadPoolServer: Caught TException: " << tx.what() << endl;
+      continue;
+    } catch (string s) {
+      inputTransport->close();
+      outputTransport->close();
+      client->close();
+      cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
       break;
     }
   }