Fix loaned message ownership transfer by returning loans via shared_ptr custom deleter - #3280
Aaravanand00 wants to merge 1 commit into
Conversation
5a81776 to
fa15b1a
Compare
|
@fujitatomoya ptal... |
fujitatomoya
left a comment
There was a problem hiding this comment.
@Aaravanand00 thakns for working on the PR. can you see my review comments that need to be addressed.
@jmachowinski can you have a look at this? looks like this is what you are asking for?
583939e to
d2050a2
Compare
Signed-off-by: Aaravanand <aaravanand@gmail.com>
d2050a2 to
c3b20c0
Compare
|
@fujitatomoya Done I have moved the shared_ptr construction to the very top of handle_loaned_message before the intra-process check to fix the leak. I also removed the invalid handle_loaned_message test from test_subscription.cpp as it causes UB with a loaning RMW... |
clalancette
left a comment
There was a problem hiding this comment.
First of all, thank you for going in and fixing this long-standing bug, it is much appreciated.
I found two potential issues, please take a look and tell me what you thin.
| RCLCPP_PUBLIC | ||
| std::shared_ptr<std::mutex> | ||
| get_loaned_message_mutex() const | ||
| { | ||
| return loaned_message_mutex_; | ||
| } |
There was a problem hiding this comment.
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 take_loaned_message API here on SubscriptionBase which holds the lock internally, removing the need for this.
| 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) { |
There was a problem hiding this comment.
I think we have a lifetime issue with sub_handle now if a user holds onto the message shared pointer. The deleter captures both sub_handle and loan_mutex shared pointers, so if SubscriptionBase gets destructed, those are both still valid until the user drops it. That is fine.
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.
Description
This PR implements the proper transfer of ownership between the RMW and the rcl layer for loaned messages, resolving a safety gap where loaned memory could be overwritten by the middleware if a user stored the
shared_ptrfor asynchronous processing.Previously,
rclcpp::Executor::execute_subscriptionunconditionally returned the loaned message immediately after the callback execution. With this change, thercl_return_loaned_message_from_subscriptionis now tied to the destructor of theshared_ptrprovided to the user.Subscription::handle_loaned_messagewhich captures thercl_subscription_tand safely returns the loaned message when the reference count drops to zero.std::mutex(loaned_message_mutex_) inSubscriptionBaseto ensure thread-safe execution ofrcl_returnandrcl_take.rcl_returnfromexecutor.cpp.RCLCPP_WARN_ONCEaboutshared_ptrcallbacks being unsafe for loaned messages.Fixes #3119
Is this user-facing behavior change?
Yes, advanced users relying on zero-copy transport can now safely retain a
std::shared_ptrto the loaned message. The underlying RMW memory is not returned to the pool until all references to the pointer are released.Did you use Generative AI?
Yes, I used Antigravity (Google DeepMind agentic coding assistant) to help implement the
shared_ptrcustom deleter pattern and the thread-safe mutex logic acrosssubscription_base.hpp/cpp,subscription.hpp, andexecutor.cpp.Additional Information
This fix is based on the feedback from
@jmachowinskito properly transfer ownership back and forth instead of introducing a newis_data_validAPI. It also addresses theTODO(clalancette)insubscription_base.cppabout handling this with a custom deleter and proper locking.