← Back to Pull requests

Fix loaned message ownership transfer by returning loans via shared_ptr custom deleter #3280

Open
unknown opened this pull request with 239 lines diff across 2 files
View on GitHub
unknown unknown commented on Sep 20, 2026 08:12 UTC
Author

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_ptr for asynchronous processing.

Previously, rclcpp::Executor::execute_subscription unconditionally returned the loaned message immediately after the callback execution. With this change, the rcl_return_loaned_message_from_subscription is now tied to the destructor of the shared_ptr provided to the user.

  • Created a custom deleter inside Subscription::handle_loaned_message which captures the rcl_subscription_t and safely returns the loaned message when the reference count drops to zero.

  • Added a std::mutex (loaned_message_mutex_) in SubscriptionBase to ensure thread-safe execution of rcl_return and rcl_take.

  • Removed the immediate rcl_return from executor.cpp.

  • Removed the obsolete RCLCPP_WARN_ONCE about shared_ptr callbacks 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_ptr to 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_ptr custom deleter pattern and the thread-safe mutex logic across subscription_base.hpp/cpp, subscription.hpp, and executor.cpp.

    Additional Information

    This fix is based on the feedback from @jmachowinski to properly transfer ownership back and forth instead of introducing a new is_data_valid API. It also addresses the TODO(clalancette) in subscription_base.cpp about handling this with a custom deleter and proper locking.

    Diff: 239 lines (+179 / -60) across 2 files Reviewer: clalancette Review duration: 25888m
    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
    0.37
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 37.2% (MEDIUM) · Higher = larger blast radius
    37.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.9% · Safe to merge
    19.9%
    Repository