Fix loaned message ownership transfer by returning loans via shared_ptr custom deleter - #3280
Open
Aaravanand00 wants to merge 1 commit into
Open
Aaravanand00 wants to merge 1 commit into
Aaravanand00 wants to merge 1 commit into
Conversation
Signed-off-by: Aaravanand <aaravanand@gmail.com>
Aaravanand00
force-pushed
the
fix-loaned-message-ownership
branch
from
September 20, 2026 08:13
5a81776 to
fa15b1a
Compare
Author
|
@fujitatomoya ptal... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.