Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to lock the mutex protecting client_endpoints_. #492

Merged
merged 1 commit into from
Dec 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,13 @@ class ServicePubListener : public eprosima::fastrtps::PublisherListener
check_for_subscription(
const eprosima::fastrtps::rtps::GUID_t & guid)
{
// Check if the guid is still in the map
if (clients_endpoints_.find(guid) == clients_endpoints_.end()) {
// Client is gone
return client_present_t::GONE;
{
std::lock_guard<std::mutex> lock(mutex_);
// Check if the guid is still in the map
if (clients_endpoints_.find(guid) == clients_endpoints_.end()) {
// Client is gone
return client_present_t::GONE;
}
}
// Wait for subscription
if (!wait_for_subscription(guid, std::chrono::milliseconds(100))) {
Expand All @@ -137,11 +140,23 @@ class ServicePubListener : public eprosima::fastrtps::PublisherListener
return client_present_t::YES;
}

// Accesors
clients_endpoints_map_t & clients_endpoints()
void endpoint_erase_if_exists(const eprosima::fastrtps::rtps::GUID_t & endpointGuid)
{
std::lock_guard<std::mutex> lock(mutex_);
auto endpoint = clients_endpoints_.find(endpointGuid);
if (endpoint != clients_endpoints_.end()) {
clients_endpoints_.erase(endpoint->second);
clients_endpoints_.erase(endpointGuid);
}
}

void endpoint_add_reader_and_writer(
const eprosima::fastrtps::rtps::GUID_t & readerGuid,
const eprosima::fastrtps::rtps::GUID_t & writerGuid)
{
std::lock_guard<std::mutex> lock(mutex_);
return clients_endpoints_;
clients_endpoints_.emplace(readerGuid, writerGuid);
clients_endpoints_.emplace(writerGuid, readerGuid);
}

private:
Expand All @@ -168,12 +183,7 @@ class ServiceListener : public eprosima::fastrtps::SubscriberListener
{
(void) sub;
if (eprosima::fastrtps::rtps::REMOVED_MATCHING == matchingInfo.status) {
auto endpoint = info_->pub_listener_->clients_endpoints().find(
matchingInfo.remoteEndpointGuid);
if (endpoint != info_->pub_listener_->clients_endpoints().end()) {
info_->pub_listener_->clients_endpoints().erase(endpoint->second);
info_->pub_listener_->clients_endpoints().erase(matchingInfo.remoteEndpointGuid);
}
info_->pub_listener_->endpoint_erase_if_exists(matchingInfo.remoteEndpointGuid);
}
}

Expand Down Expand Up @@ -202,8 +212,7 @@ class ServiceListener : public eprosima::fastrtps::SubscriberListener
// Save both guids in the clients_endpoints map
const eprosima::fastrtps::rtps::GUID_t & writer_guid =
request.sample_info_.sample_identity.writer_guid();
info_->pub_listener_->clients_endpoints().emplace(reader_guid, writer_guid);
info_->pub_listener_->clients_endpoints().emplace(writer_guid, reader_guid);
info_->pub_listener_->endpoint_add_reader_and_writer(reader_guid, writer_guid);

std::lock_guard<std::mutex> lock(internalMutex_);

Expand Down