Skip to content

Commit

Permalink
Merge pull request #123 from mauropasse/mauro/galactic-fix-data-race-…
Browse files Browse the repository at this point in the history
…on-params-event

Galactic: Fix data race on parameter event
  • Loading branch information
alsora authored Oct 16, 2023
2 parents 31030fb + 7bab107 commit dffd84c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions rclcpp/include/rclcpp/time_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class TimeSource

private:
// Preserve the node reference
std::mutex node_base_lock_;
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
Expand Down
10 changes: 10 additions & 0 deletions rclcpp/src/rclcpp/time_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void TimeSource::attachNode(
rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface,
rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface)
{
std::lock_guard<std::mutex> guard(node_base_lock_);
node_base_ = node_base_interface;
node_topics_ = node_topics_interface;
node_graph_ = node_graph_interface;
Expand Down Expand Up @@ -134,6 +135,7 @@ void TimeSource::attachNode(

void TimeSource::detachNode()
{
std::lock_guard<std::mutex> guard(node_base_lock_);
this->ros_time_active_ = false;
destroy_clock_sub();
parameter_subscription_.reset();
Expand Down Expand Up @@ -296,6 +298,14 @@ void TimeSource::destroy_clock_sub()

void TimeSource::on_parameter_event(const rcl_interfaces::msg::ParameterEvent::SharedPtr event)
{
std::lock_guard<std::mutex> guard(node_base_lock_);

if (node_base_ == nullptr) {
// Do nothing if node_base_ is nullptr because it means the TimeSource is now
// without an attached node
return;
}

// Filter out events on 'use_sim_time' parameter instances in other nodes.
if (event->node != node_base_->get_fully_qualified_name()) {
return;
Expand Down

0 comments on commit dffd84c

Please sign in to comment.