Skip to content

Commit ce2f6d8

Browse files
committed
[#24061] Solved TSAN race conditions + removed 'bridges_mutex_'
Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent c7dd0f3 commit ce2f6d8

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

ddspipe_core/include/ddspipe_core/core/DdsPipe.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@ class DdsPipe
379379
*/
380380
void deactivate_all_topics_nts_() noexcept;
381381

382+
/**
383+
* @brief Update partitions for all bridges.
384+
*
385+
* This method must be called with \c mutex_ locked.
386+
*/
387+
void update_partitions_nts_(
388+
const std::set<std::string>& partitions_set);
389+
382390
//////////////////////////
383391
// CONFIGURATION VARIABLES
384392
//////////////////////////
@@ -459,11 +467,6 @@ class DdsPipe
459467
* @brief Internal mutex for concurrent calls
460468
*/
461469
mutable std::mutex mutex_;
462-
463-
/**
464-
* @brief Internal mutex for bridges
465-
*/
466-
mutable std::mutex bridges_mutex_;
467470
};
468471

469472
} /* namespace core */

ddspipe_core/src/cpp/communication/dds/DdsBridge.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ void DdsBridge::add_writers_to_tracks_nts_(
285285
void DdsBridge::update_partitions(
286286
const std::set<std::string>& partitions_set)
287287
{
288+
std::lock_guard<std::mutex> lock(mutex_);
289+
288290
for (const auto& track: tracks_)
289291
{
290292
track.second->update_writers_topic_partitions(topic_->partition_name);
@@ -295,6 +297,8 @@ void DdsBridge::update_partitions(
295297
void DdsBridge::update_topic_filter(
296298
const std::string& expression)
297299
{
300+
std::lock_guard<std::mutex> lock(mutex_);
301+
298302
for (const auto& track: tracks_)
299303
{
300304
track.second->update_reader_content_filter(expression);
@@ -347,6 +351,8 @@ void DdsBridge::add_partition_to_topic(
347351
std::string guid,
348352
std::string partition)
349353
{
354+
std::lock_guard<std::mutex> lock(mutex_);
355+
350356
topic_->partition_name[guid] = partition;
351357
}
352358

ddspipe_core/src/cpp/core/DdsPipe.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -303,29 +303,24 @@ void DdsPipe::discovered_endpoint_nts_(
303303
// update the track of the topic to
304304
// add the partition in the reader if it is in the filter
305305

306-
// update partitions under bridges_mutex_
306+
const auto bridge_it = bridges_.find(utils::Heritable<DdsTopic>::make_heritable(endpoint.topic));
307+
// add the specific partition of the endpoint in the bridge topic.
308+
if (bridge_it != bridges_.end())
307309
{
308-
std::lock_guard<std::mutex> lock(bridges_mutex_);
310+
std::ostringstream guid_ss;
311+
guid_ss << endpoint.guid;
309312

310-
const auto bridge_it = bridges_.find(utils::Heritable<DdsTopic>::make_heritable(endpoint.topic));
311-
// add the specific partition of the endpoint in the bridges topic.
312-
if (bridge_it != bridges_.end())
313+
const auto part_it = endpoint.specific_partitions.find(guid_ss.str());
314+
if (part_it != endpoint.specific_partitions.end())
313315
{
314-
std::ostringstream guid_ss;
315-
guid_ss << endpoint.guid;
316-
317-
const auto part_it = endpoint.specific_partitions.find(guid_ss.str());
318-
if (part_it != endpoint.specific_partitions.end())
319-
{
320-
bridge_it->second->add_partition_to_topic(guid_ss.str(), part_it->second);
321-
}
316+
bridge_it->second->add_partition_to_topic(guid_ss.str(), part_it->second);
322317
}
323318
}
324319

325320
// update readers outside the lock
326321
if (!filter_partition_.empty())
327322
{
328-
update_partitions(filter_partition_);
323+
update_partitions_nts_(filter_partition_);
329324
}
330325
}
331326
}
@@ -626,6 +621,14 @@ void DdsPipe::deactivate_all_topics_nts_() noexcept
626621

627622
void DdsPipe::update_partitions(
628623
const std::set<std::string>& partitions_set)
624+
{
625+
std::lock_guard<std::mutex> lock(mutex_);
626+
627+
update_partitions_nts_(partitions_set);
628+
}
629+
630+
void DdsPipe::update_partitions_nts_(
631+
const std::set<std::string>& partitions_set)
629632
{
630633
// TODO.
631634
// In the future it could be interesting to update the partitions
@@ -641,6 +644,8 @@ void DdsPipe::update_content_filter(
641644
const std::string& topic_name,
642645
const std::string& expression)
643646
{
647+
std::lock_guard<std::mutex> lock(mutex_);
648+
644649
for (const auto& pair : bridges_)
645650
{
646651
if (pair.first->m_topic_name == topic_name)
@@ -653,8 +658,7 @@ void DdsPipe::update_content_filter(
653658
void DdsPipe::update_filter(
654659
const std::set<std::string> filter_partition_set)
655660
{
656-
// Avoid possible datarace with partitions filter
657-
std::lock_guard<std::mutex> lock(bridges_mutex_);
661+
std::lock_guard<std::mutex> lock(mutex_);
658662

659663
filter_partition_ = std::move(filter_partition_set);
660664
}

ddspipe_participants/src/cpp/writer/dynamic_types/SchemaWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ void SchemaWriter::update_content_topic_filter(
5959
void SchemaWriter::update_topic_partitions(
6060
const std::map<std::string, std::string>& partition_name)
6161
{
62+
std::lock_guard<std::recursive_mutex> lock(mutex_);
63+
6264
topic_.partition_name = partition_name;
6365
}
6466

0 commit comments

Comments
 (0)