Github user tpcwang commented on a diff in the pull request: https://github.com/apache/thrift/pull/980#discussion_r59305327 --- Diff: lib/cpp/src/thrift/server/TThreadedServer.cpp --- @@ -90,31 +95,71 @@ TThreadedServer::~TThreadedServer() { } void TThreadedServer::serve() { + threadFactory_->setDetached(false); TServerFramework::serve(); - // Drain all clients - no more will arrive - try { - Synchronized s(clientsMonitor_); - while (getConcurrentClientCount() > 0) { - clientsMonitor_.wait(); - } - } catch (TException& tx) { - string errStr = string("TThreadedServer: Exception joining workers: ") + tx.what(); - GlobalOutput(errStr.c_str()); + // Ensure post-condition of no active clients + Synchronized s(clientMonitor_); + while (!activeClientMap_.empty()) { + clientMonitor_.wait(); + } + + drainDeadClients(); +} + +void TThreadedServer::drainDeadClients() { + // we're in a monitor here + while (!deadClientMap_.empty()) { + ClientMap::iterator it = deadClientMap_.begin(); + it->second->join(); + deadClientMap_.erase(it); } } void TThreadedServer::onClientConnected(const shared_ptr<TConnectedClient>& pClient) { - threadFactory_->newThread(pClient)->start(); + Synchronized sync(clientMonitor_); + drainDeadClients(); --- End diff -- What do you think about draining in onClientDisconnected instead of onClientConnected (note to take care of draining before adding the thread that initiated onClientDisconnected to the list of dead clients)? I think it might be better to make onClientConnected as fast as possible because the serve thread is the caller?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---