From 94935d433b36961d1856ff9e6fd04f3c16d5d74d Mon Sep 17 00:00:00 2001 From: Adel Fakih Date: Fri, 3 Mar 2017 09:44:28 -0500 Subject: [PATCH 1/2] Avoid deleting XmlRpcClient's while they are being still in use on another thread * Acquire clients_mutex_ before deleting the clients * Remove the timed wait for the clients to become not in use * Only delete and erase clients that are not in use * Clients that would still be in use would delete themselves on release --- clients/roscpp/src/libros/xmlrpc_manager.cpp | 36 ++++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/clients/roscpp/src/libros/xmlrpc_manager.cpp b/clients/roscpp/src/libros/xmlrpc_manager.cpp index 272315b3fa..7b4c0f1437 100644 --- a/clients/roscpp/src/libros/xmlrpc_manager.cpp +++ b/clients/roscpp/src/libros/xmlrpc_manager.cpp @@ -155,21 +155,25 @@ void XMLRPCManager::shutdown() server_.close(); // kill the last few clients that were started in the shutdown process - for (V_CachedXmlRpcClient::iterator i = clients_.begin(); - i != clients_.end(); ++i) { - for (int wait_count = 0; i->in_use_ && wait_count < 10; wait_count++) + boost::mutex::scoped_lock lock(clients_mutex_); + + for (V_CachedXmlRpcClient::iterator i = clients_.begin(); + i != clients_.end();) { - ROSCPP_LOG_DEBUG("waiting for xmlrpc connection to finish..."); - ros::WallDuration(0.01).sleep(); + if (!i->in_use_) + { + i->client_->close(); + delete i->client_; + i = clients_.erase(i); + } + else + { + ++i; + } } - - i->client_->close(); - delete i->client_; } - clients_.clear(); - boost::mutex::scoped_lock lock(functions_mutex_); functions_.clear(); @@ -369,7 +373,17 @@ void XMLRPCManager::releaseXMLRPCClient(XmlRpcClient *c) { if (c == i->client_) { - i->in_use_ = false; + if (shutting_down_) + { + // if we are shutting down we won't be re-using the client + i->client_->close(); + delete i->client_; + clients_.erase(i); + } + else + { + i->in_use_ = false; + } break; } } From 624b09b5f6799c60bb744a10bd0c10790fad96e3 Mon Sep 17 00:00:00 2001 From: Adel Fakih Date: Tue, 14 Mar 2017 15:34:24 -0400 Subject: [PATCH 2/2] Wait for clients that are in use to finish in XmlRpcManager::shutdown --- clients/roscpp/src/libros/xmlrpc_manager.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clients/roscpp/src/libros/xmlrpc_manager.cpp b/clients/roscpp/src/libros/xmlrpc_manager.cpp index 7b4c0f1437..6a3829f76f 100644 --- a/clients/roscpp/src/libros/xmlrpc_manager.cpp +++ b/clients/roscpp/src/libros/xmlrpc_manager.cpp @@ -174,6 +174,13 @@ void XMLRPCManager::shutdown() } } + // Wait for the clients that are in use to finish and remove themselves from clients_ + for (int wait_count = 0; !clients_.empty() && wait_count < 10; wait_count++) + { + ROSCPP_LOG_DEBUG("waiting for xmlrpc connection to finish..."); + ros::WallDuration(0.01).sleep(); + } + boost::mutex::scoped_lock lock(functions_mutex_); functions_.clear();