← Back to Pull requests

Add const reference service callback signatures #3269

Closed
unknown closed this pull request without merging on Oct 07, 2026 13:51 UTC
View on GitHub
unknown unknown commented on Sep 13, 2026 06:29 UTC
Author

Problem

AnySubscriptionCallback accepts const MessageT & callbacks, but AnyServiceCallback only accepts std::shared_ptr signatures. Every service callback therefore has to take shared_ptrs (the response one is copied per call) even when the handler answers immediately and never needs to extend the request/response lifetime. This is inconsistent with subscriptions.

Design

Follows the approach suggested in the issue: the request and response keep arriving as shared_ptr internally; dispatch() dereferences them for the user callback, exactly like AnySubscriptionCallback does for const MessageT &. The existing shared_ptr overloads are unchanged.

Fix

rclcpp/include/rclcpp/any_service_callback.hpp

  • New variant alternatives ConstRefCallback = void (const Request &, Response &) and ConstRefWithRequestHeaderCallback = void (const rmw_request_id_t &, const Request &, Response &), appended after the existing alternatives.

  • Both set() overloads get matching function_traits::same_arguments branches so std::bind results resolve like the shared_ptr shapes do.

  • dispatch() calls cb(request, response) / cb(request_header, request, *response) and returns the response as before.
  • The new alternatives cannot be ambiguous with the existing ones in the std::variant converting assignment: a callable taking const Request & is not invocable with a shared_ptr<Request> and vice versa, so at most one std::function alternative is viable for any callback.

    How tested

    rclcpp/test/rclcpp/test_any_service_callback.cpp: four new cases (const-ref without/with request header, response mutation propagated to the returned shared_ptr, std::bind for both shapes). rclcpp/test/rclcpp/test_service.cpp: create_service<> with a const-ref callback, exercised end to end through a client.

    Before the change, the new callback shapes fail to compile:

    any_service_callback.hpp:102:17: error: no match for 'operator=' (operand types are
    'std::variant<std::monostate, std::function<void(std::shared_ptr<...Request>, std::shared_ptr<...Response>)>, ...>'
    and '<lambda(const ...Request&, ...Response&)>')
    

    After the change the header compiles and all cases pass. I could not run colcon test locally (no ROS 2 workspace), so the new gtest cases were mirrored in a standalone gtest harness that compiles the unmodified any_service_callback.hpp / function_traits.hpp against stub rmw/tracetools headers: 10/10 pass with -Wall -Wextra on GCC 13, including the pre-existing callback shapes. cpplint with the ament filters reports no issues. Please rely on CI for the full build and for ament_uncrustify.

    Links

  • https://github.com/ros2/rclcpp/issues/3078
  • This change was prepared with an AI agent operated by KR-Ravindra, who reviewed and tested it.

    Diff: 354 lines (+265 / -89) across 4 files Reviewer: KR-Ravindra Review duration: 35002m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 354 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3269"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 16.8% · Safe to merge
    16.8%
    Repository