From 3ddee663ceffeb61173cace109d06bb10e8b6cb1 Mon Sep 17 00:00:00 2001 From: Nipa Kumar Date: Wed, 3 Aug 2016 12:05:24 -0700 Subject: [PATCH] Agent crash@ DiscoveryServiceClient::Subscribe task and are not exclusive and hence add/delete to inuse_service_list_ can happen in parallel and hence reason for the crash, protect this vector manipulation via a mutex. Note: and where most manipulation of inuse_service_list_ happen is already exclusive. Change-Id: I48a4cd4c0c85e328c65fd7908608be7b7c0e9562 Closes-Bug:1608791 --- src/discovery/client/discovery_client.cc | 31 ++++++++++++++---------- src/discovery/client/discovery_client.h | 1 + 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/discovery/client/discovery_client.cc b/src/discovery/client/discovery_client.cc index 45ae8fef523..906d22d127c 100644 --- a/src/discovery/client/discovery_client.cc +++ b/src/discovery/client/discovery_client.cc @@ -825,19 +825,22 @@ void DiscoveryServiceClient::Subscribe(std::string serviceName) { } } - if (resp->inuse_service_list_.size()) { - pugi::xml_node node_service = pugi->FindNode(serviceName); - if (!pugi->IsNull(node_service)) { - pugi->ReadNode(serviceName); //SetContext - pugi->AddChildNode("service-in-use-list", ""); - std::vector::iterator it; - for (it = resp->inuse_service_list_.begin(); - it != resp->inuse_service_list_.end(); it++) { - boost::asio::ip::tcp::endpoint ep = *it; - std::string pub_id = - resp->GetPublisherId(ep.address().to_string()); - pugi->AddChildNode("publisher-id", pub_id); - pugi->ReadNode("service-in-use-list"); + { + tbb::mutex::scoped_lock lock(mutex_); + if (resp->inuse_service_list_.size()) { + pugi::xml_node node_service = pugi->FindNode(serviceName); + if (!pugi->IsNull(node_service)) { + pugi->ReadNode(serviceName); //SetContext + pugi->AddChildNode("service-in-use-list", ""); + std::vector::iterator it; + for (it = resp->inuse_service_list_.begin(); + it != resp->inuse_service_list_.end(); it++) { + boost::asio::ip::tcp::endpoint ep = *it; + std::string pub_id = + resp->GetPublisherId(ep.address().to_string()); + pugi->AddChildNode("publisher-id", pub_id); + pugi->ReadNode("service-in-use-list"); + } } } } @@ -1235,6 +1238,7 @@ void DiscoveryServiceClient::AddSubscribeInUseServiceList( DSSubscribeResponse *resp = GetSubscribeResponse(serviceName); if (resp) { + tbb::mutex::scoped_lock lock(mutex_); resp->AddInUseServiceList(ep); } } @@ -1244,6 +1248,7 @@ void DiscoveryServiceClient::DeleteSubscribeInUseServiceList( DSSubscribeResponse *resp = GetSubscribeResponse(serviceName); if (resp) { + tbb::mutex::scoped_lock lock(mutex_); resp->DeleteInUseServiceList(ep); } } diff --git a/src/discovery/client/discovery_client.h b/src/discovery/client/discovery_client.h index 9e0d20b036b..93ea3439765 100644 --- a/src/discovery/client/discovery_client.h +++ b/src/discovery/client/discovery_client.h @@ -246,6 +246,7 @@ class DiscoveryServiceClient { std::string subscriber_name_; int heartbeat_interval_; std::string local_addr_; + tbb::mutex mutex_; }; #endif // __DISCOVERY_SERVICE_CLIENT_H__