Make sure exceptinos in servers are caught

Summary: There was potential for a write exception in close() because it might call flush() internally

Reviewed By: dreiss


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665430 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/src/server/TThreadPoolServer.cpp b/lib/cpp/src/server/TThreadPoolServer.cpp
index b268c45..55a73d3 100644
--- a/lib/cpp/src/server/TThreadPoolServer.cpp
+++ b/lib/cpp/src/server/TThreadPoolServer.cpp
@@ -51,18 +51,33 @@
     } catch (TTransportException& ttx) {
       // This is reasonably expected, client didn't send a full request so just
       // ignore him
-      //cerr << "TThreadPoolServer client died: " << ttx.what() << endl;
+      // string errStr = string("TThreadPoolServer client died: ") + ttx.what();
+      // GlobalOutput(errStr.c_str());
     } catch (TException& x) {
-      cerr << "TThreadPoolServer exception: " << x.what() << endl;
+      string errStr = string("TThreadPoolServer exception: ") + x.what();
+      GlobalOutput(errStr.c_str());
     } catch (std::exception &x) {
-      cerr << "TThreadPoolServer, std::exception: " << x.what() << endl;
+      string errStr = string("TThreadPoolServer, std::exception: ") + x.what();
+      GlobalOutput(errStr.c_str());
     }
 
     if (eventHandler != NULL) {
       eventHandler->clientEnd(input_, output_);
     }
-    input_->getTransport()->close();
-    output_->getTransport()->close();
+
+    try {
+      input_->getTransport()->close();
+    } catch (TTransportException& ttx) {
+      string errStr = string("TThreadPoolServer input close failed: ") + ttx.what();
+      GlobalOutput(errStr.c_str());
+    }
+    try {
+      output_->getTransport()->close();
+    } catch (TTransportException& ttx) {
+      string errStr = string("TThreadPoolServer output close failed: ") + ttx.what();
+      GlobalOutput(errStr.c_str());
+    }
+
   }
 
  private:
@@ -108,7 +123,8 @@
     // Start the server listening
     serverTransport_->listen();
   } catch (TTransportException& ttx) {
-    cerr << "TThreadPoolServer::run() listen(): " << ttx.what() << endl;
+    string errStr = string("TThreadPoolServer::run() listen(): ") + ttx.what();
+    GlobalOutput(errStr.c_str());
     return;
   }
 
@@ -142,20 +158,23 @@
       if (outputTransport != NULL) { outputTransport->close(); }
       if (client != NULL) { client->close(); }
       if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
-        cerr << "TThreadPoolServer: TServerTransport died on accept: " << ttx.what() << endl;
+        string errStr = string("TThreadPoolServer: TServerTransport died on accept: ") + ttx.what();
+        GlobalOutput(errStr.c_str());
       }
       continue;
     } catch (TException& tx) {
       if (inputTransport != NULL) { inputTransport->close(); }
       if (outputTransport != NULL) { outputTransport->close(); }
       if (client != NULL) { client->close(); }
-      cerr << "TThreadPoolServer: Caught TException: " << tx.what() << endl;
+      string errStr = string("TThreadPoolServer: Caught TException: ") + tx.what();
+      GlobalOutput(errStr.c_str());
       continue;
     } catch (string s) {
       if (inputTransport != NULL) { inputTransport->close(); }
       if (outputTransport != NULL) { outputTransport->close(); }
       if (client != NULL) { client->close(); }
-      cerr << "TThreadPoolServer: Unknown exception: " << s << endl;
+      string errStr = "TThreadPoolServer: Unknown exception: " + s;
+      GlobalOutput(errStr.c_str());
       break;
     }
   }
@@ -166,7 +185,8 @@
       serverTransport_->close();
       threadManager_->join();
     } catch (TException &tx) {
-      cerr << "TThreadPoolServer: Exception shutting down: " << tx.what() << endl;
+      string errStr = string("TThreadPoolServer: Exception shutting down: ") + tx.what();
+      GlobalOutput(errStr.c_str());
     }
     stop_ = false;
   }
diff --git a/lib/cpp/src/server/TThreadedServer.cpp b/lib/cpp/src/server/TThreadedServer.cpp
index d07e9da..a08fd54 100644
--- a/lib/cpp/src/server/TThreadedServer.cpp
+++ b/lib/cpp/src/server/TThreadedServer.cpp
@@ -51,17 +51,30 @@
         }
       }
     } catch (TTransportException& ttx) {
-      cerr << "TThreadedServer client died: " << ttx.what() << endl;
+      string errStr = string("TThreadedServer client died: ") + ttx.what();
+      GlobalOutput(errStr.c_str());
     } catch (TException& x) {
-      cerr << "TThreadedServer exception: " << x.what() << endl;
+      string errStr = string("TThreadedServer exception: ") + x.what();
+      GlobalOutput(errStr.c_str());
     } catch (...) {
-      cerr << "TThreadedServer uncaught exception." << endl;
+      GlobalOutput("TThreadedServer uncaught exception.");
     }
     if (eventHandler != NULL) {
       eventHandler->clientEnd(input_, output_);
     }
-    input_->getTransport()->close();
-    output_->getTransport()->close();
+
+    try {
+      input_->getTransport()->close();
+    } catch (TTransportException& ttx) {
+      string errStr = string("TThreadedServer input close failed: ") + ttx.what();
+      GlobalOutput(errStr.c_str());
+    }
+    try {
+      output_->getTransport()->close();
+    } catch (TTransportException& ttx) {
+      string errStr = string("TThreadedServer output close failed: ") + ttx.what();
+      GlobalOutput(errStr.c_str());
+    }
 
     // Remove this task from parent bookkeeping
     {
@@ -107,7 +120,8 @@
     // Start the server listening
     serverTransport_->listen();
   } catch (TTransportException& ttx) {
-    cerr << "TThreadedServer::run() listen(): " << ttx.what() << endl;
+    string errStr = string("TThreadedServer::run() listen(): ") +ttx.what();
+    GlobalOutput(errStr.c_str());
     return;
   }
 
@@ -160,20 +174,23 @@
       if (outputTransport != NULL) { outputTransport->close(); }
       if (client != NULL) { client->close(); }
       if (!stop_ || ttx.getType() != TTransportException::INTERRUPTED) {
-        cerr << "TThreadedServer: TServerTransport died on accept: " << ttx.what() << endl;
+        string errStr = string("TThreadedServer: TServerTransport died on accept: ") + ttx.what();
+        GlobalOutput(errStr.c_str());
       }
       continue;
     } catch (TException& tx) {
       if (inputTransport != NULL) { inputTransport->close(); }
       if (outputTransport != NULL) { outputTransport->close(); }
       if (client != NULL) { client->close(); }
-      cerr << "TThreadedServer: Caught TException: " << tx.what() << endl;
+      string errStr = string("TThreadedServer: Caught TException: ") + tx.what();
+      GlobalOutput(errStr.c_str());
       continue;
     } catch (string s) {
       if (inputTransport != NULL) { inputTransport->close(); }
       if (outputTransport != NULL) { outputTransport->close(); }
       if (client != NULL) { client->close(); }
-      cerr << "TThreadedServer: Unknown exception: " << s << endl;
+      string errStr = "TThreadedServer: Unknown exception: " + s;
+      GlobalOutput(errStr.c_str());
       break;
     }
   }
@@ -183,7 +200,8 @@
     try {
       serverTransport_->close();
     } catch (TException &tx) {
-      cerr << "TThreadedServer: Exception shutting down: " << tx.what() << endl;
+      string errStr = string("TThreadedServer: Exception shutting down: ") + tx.what();
+      GlobalOutput(errStr.c_str());
     }
     try {
       Synchronized s(tasksMonitor_);
@@ -191,7 +209,8 @@
         tasksMonitor_.wait();
       }
     } catch (TException &tx) {
-      cerr << "TThreadedServer: Exception joining workers: " << tx.what() << endl;
+      string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what();
+      GlobalOutput(errStr.c_str());
     }
     stop_ = false;
   }