-
Notifications
You must be signed in to change notification settings - Fork 570
Fix loaned message ownership transfer by returning loans via shared_ptr custom deleter #3280
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
base: rolling
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -390,17 +390,32 @@ class Subscription : public SubscriptionBase | |||||||||||||||
| void * loaned_message, | ||||||||||||||||
| const rclcpp::MessageInfo & message_info) override | ||||||||||||||||
| { | ||||||||||||||||
| auto typed_message = static_cast<ROSMessageType *>(loaned_message); | ||||||||||||||||
|
|
||||||||||||||||
| auto sub_handle = this->get_subscription_handle(); | ||||||||||||||||
| auto loan_mutex = this->loaned_message_mutex_; | ||||||||||||||||
| auto sptr = std::shared_ptr<ROSMessageType>( | ||||||||||||||||
| typed_message, [sub_handle, loan_mutex](ROSMessageType * msg) { | ||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we have a lifetime issue with However, the comments in rclcpp/rclcpp/include/rclcpp/subscription_base.hpp Lines 583 to 589 in 249154a
subscription_handle_ to be destroyed before on_new_message_callback so we don't end up in a situation where the callback can fire on a destroyed subscription_handle_. That is now violated because the subscription_handle_ can outlive the SubscriptionBase. We can probably get away with explicitly clearing the on_new_message_callback in the SubscriptionBase destructor to make sure this doesn't happen.
|
||||||||||||||||
| if (msg) { | ||||||||||||||||
| std::lock_guard<std::mutex> lock(*loan_mutex); | ||||||||||||||||
| rcl_ret_t ret = rcl_return_loaned_message_from_subscription( | ||||||||||||||||
| sub_handle.get(), static_cast<void *>(msg)); | ||||||||||||||||
| if (RCL_RET_OK != ret) { | ||||||||||||||||
| RCLCPP_ERROR( | ||||||||||||||||
| rclcpp::get_logger("rclcpp"), | ||||||||||||||||
| "rcl_return_loaned_message_from_subscription() failed: %s", | ||||||||||||||||
| rcl_get_error_string().str); | ||||||||||||||||
| rcl_reset_error(); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| if (matches_any_intra_process_publishers(&message_info.get_rmw_message_info().publisher_gid)) { | ||||||||||||||||
| // In this case, the message will be delivered via intra process and | ||||||||||||||||
| // we should ignore this copy of the message. | ||||||||||||||||
| return; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| auto typed_message = static_cast<ROSMessageType *>(loaned_message); | ||||||||||||||||
| // message is loaned, so we have to make sure that the deleter does not deallocate the message | ||||||||||||||||
| auto sptr = std::shared_ptr<ROSMessageType>( | ||||||||||||||||
| typed_message, [](ROSMessageType * msg) {(void) msg;}); | ||||||||||||||||
|
|
||||||||||||||||
| std::chrono::time_point<std::chrono::system_clock> now; | ||||||||||||||||
| if (subscription_topic_statistics_) { | ||||||||||||||||
| // get current time before executing callback to | ||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -650,8 +650,16 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase> | |
| take_dynamic_message( | ||
| rclcpp::dynamic_typesupport::DynamicMessage & message_out, | ||
| rclcpp::MessageInfo & message_info_out); | ||
|
|
||
| RCLCPP_PUBLIC | ||
| std::shared_ptr<std::mutex> | ||
| get_loaned_message_mutex() const | ||
| { | ||
| return loaned_message_mutex_; | ||
| } | ||
|
Comment on lines
+654
to
+659
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of exposing implementations details like this, particularly around locks. It just makes it much harder to see how the locking is used and expected to be used. I'm going to suggest that we change the implementation to a |
||
| // =============================================================================================== | ||
|
|
||
|
|
||
| protected: | ||
| template<typename EventCallbackT> | ||
| void | ||
|
|
@@ -688,6 +696,9 @@ class SubscriptionBase : public std::enable_shared_from_this<SubscriptionBase> | |
| std::shared_ptr<rcl_node_t> node_handle_; | ||
|
|
||
| std::recursive_mutex on_new_message_callback_mutex_; | ||
|
|
||
| /// Mutex to protect rcl_take_loaned_message and rcl_return_loaned_message_from_subscription | ||
| std::shared_ptr<std::mutex> loaned_message_mutex_; | ||
| // It is important to declare on_new_message_callback_ before | ||
| // subscription_handle_, so on destruction the subscription is | ||
| // destroyed first. Otherwise, the rmw subscription callback | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.