Fix loaned message ownership transfer by returning loans via shared_ptr custom deleter #3280
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.37) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 239 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.328
Real PR Conversation Timeline
1 comments
"Review active on #3280"
general
weight: 0.35
clalancette
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.