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

fix segmentation fault when adding multiple executors simultaneously #1632

Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions rclcpp/src/rclcpp/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ Context::shutdown(const std::string & reason)
rclcpp::Context::OnShutdownCallback
Context::on_shutdown(OnShutdownCallback callback)
{
std::lock_guard<std::mutex> lock(on_shutdown_callbacks_mutex_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not only adding mutex here, but also all access to on_shutdown_callbacks_?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not possible without changing the API as the reference is returned

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should deprecate in the future those getters, returning a reference to an internal container isn't great.

I think that the mutex should also be taken here before calling all the callbacks:

  {
    std::lock_guard<std::mutex> lock(on_shutdown_callbacks_mutex_);
    for (const auto & callback : on_shutdown_callbacks_) {
      callback();
    }
  }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should deprecate in the future those getters, returning a reference to an internal container isn't great.

In the general case, I don't agree with this, but in this particular case, I agree.

We could return a copy (since they should be trivially copyable) and lock that operation.

Or we could just drop the accessors. I think I added them just because having hidden state like that can bite you later if you need access to it and have to add an API to get it, or work around it by keeping track of them outside this class (duplicating the effort). So I'd lean towards returning a copy and protecting that with the mutex.

on_shutdown_callbacks_.push_back(callback);
return callback;
}
Expand Down
1 change: 1 addition & 0 deletions rclcpp/test/rclcpp/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,7 @@ TEST_F(TestNode, get_publishers_subscriptions_info_by_topic) {
};
rclcpp::QoS qos = rclcpp::QoS(qos_initialization, rmw_qos_profile_default);
auto publisher = node->create_publisher<test_msgs::msg::BasicTypes>(topic_name, qos);
std::this_thread::sleep_for(std::chrono::milliseconds(30));
// List should have one item
auto publisher_list = node->get_publishers_info_by_topic(fq_topic_name);
ASSERT_EQ(publisher_list.size(), (size_t)1);
Expand Down