Repository navigation
Fix loaned message ownership transfer by returning loans via shared_ptr custom deleter - #3280
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.
Signed-off-by: Aaravanand <aaravanand@gmail.com>
@clalancette I agree with your points and IMO you are absolutely right on both issues :-
I have updated the pr to address both -->
|
|
@clalancette @fujitatomoya if all looks good then merge this... |
Signed-off-by: Aarav Anand <aaravanand5749@gmail.com>
0b5b43a to
52e534e
Compare
ABI Compliance Check✅ Verdict: compatible
✅
|
clalancette
left a comment
There was a problem hiding this comment.
We are down to a few nits inline now, though the destructor change is enough for me to ask for one more round of fixes.
I will note that the loaned_message_mutex (and hence take_loaned_message) is probably unnecessary; rmw_take_loaned_message says that it is thread-safe, and the only implementation (rmw_fastrtps) makes that so. Documentation for rcl_take_loaned_message does not have that guarantee, though in practice it is thread-safe. I'd lean towards removing the mutex, but if you think we should keep it, I'm OK with that too. Just let me know which way you decide.
…or try-catch Signed-off-by: Aaravanand <aaravanand@gmail.com>
check now all looks good??? |
clalancette
left a comment
There was a problem hiding this comment.
Looks good to me now. Thank you for all of the iteration. I'll run CI on it next.
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.