From 428582c1d291a9370f05543c4efbdc1d6d6d55a8 Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Mon, 9 Nov 2026 18:05:15 -0600 Subject: [PATCH] Keep the service introspection clock alive in Client and Service rcl keeps a pointer to the rcl_clock_t that the service introspection publisher was created with, and uses it until introspection is turned off or the client or service is finalized. Client and Service didn't keep the clock alive, so destroying it first (e.g. destroying the node while a client or service is still held) was a heap-use-after-free in rcl_service_event_publisher_fini, found with AddressSanitizer. Keep the clock in ClientBase/ServiceBase, declared before the rcl handle so that it is destroyed after it, for as long as rcl uses it. Signed-off-by: Michael Carroll --- rclcpp/include/rclcpp/client.hpp | 16 ++++++ rclcpp/include/rclcpp/service.hpp | 16 ++++++ .../rclcpp/test_service_introspection.cpp | 50 +++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/rclcpp/include/rclcpp/client.hpp b/rclcpp/include/rclcpp/client.hpp index 491be8c23a..bf7696872d 100644 --- a/rclcpp/include/rclcpp/client.hpp +++ b/rclcpp/include/rclcpp/client.hpp @@ -331,6 +331,8 @@ class ClientBase // destroyed first. Otherwise, the rmw client callback // would point briefly to a destroyed function. std::function on_new_response_callback_{nullptr}; + // Used by rcl until introspection is turned off, so declare it before client_handle_. + rclcpp::Clock::SharedPtr introspection_clock_; // Declare client_handle_ after callback std::shared_ptr client_handle_; @@ -748,6 +750,13 @@ class Client : public ClientBase rcl_publisher_options_t pub_opts = rcl_publisher_get_default_options(); pub_opts.qos = qos_service_event_pub.get_rmw_qos_profile(); + // rcl keeps the clock its introspection publisher was created with. + const bool creates_publisher = !introspection_clock_ && + RCL_SERVICE_INTROSPECTION_OFF != introspection_state; + if (creates_publisher) { + introspection_clock_ = clock; + } + rcl_ret_t ret = rcl_client_configure_service_introspection( client_handle_.get(), node_handle_.get(), @@ -756,6 +765,13 @@ class Client : public ClientBase pub_opts, introspection_state); + // rcl destroys the publisher when introspection is turned off or fails to start. + if (RCL_SERVICE_INTROSPECTION_OFF == introspection_state || + (creates_publisher && RCL_RET_OK != ret)) + { + introspection_clock_.reset(); + } + if (RCL_RET_OK != ret) { rclcpp::exceptions::throw_from_rcl_error(ret, "failed to configure client introspection"); } diff --git a/rclcpp/include/rclcpp/service.hpp b/rclcpp/include/rclcpp/service.hpp index 387f33763c..9980c1527a 100644 --- a/rclcpp/include/rclcpp/service.hpp +++ b/rclcpp/include/rclcpp/service.hpp @@ -218,6 +218,8 @@ class ServiceBase // destroyed first. Otherwise, the rmw service callback // would point briefly to a destroyed function. std::function on_new_request_callback_{nullptr}; + // Used by rcl until introspection is turned off, so declare it before service_handle_. + rclcpp::Clock::SharedPtr introspection_clock_; // Declare service_handle_ after callback std::shared_ptr service_handle_; bool owns_rcl_handle_ = true; @@ -464,6 +466,13 @@ class Service rcl_publisher_options_t pub_opts = rcl_publisher_get_default_options(); pub_opts.qos = qos_service_event_pub.get_rmw_qos_profile(); + // rcl keeps the clock its introspection publisher was created with. + const bool creates_publisher = !introspection_clock_ && + RCL_SERVICE_INTROSPECTION_OFF != introspection_state; + if (creates_publisher) { + introspection_clock_ = clock; + } + rcl_ret_t ret = rcl_service_configure_service_introspection( service_handle_.get(), node_handle_.get(), @@ -472,6 +481,13 @@ class Service pub_opts, introspection_state); + // rcl destroys the publisher when introspection is turned off or fails to start. + if (RCL_SERVICE_INTROSPECTION_OFF == introspection_state || + (creates_publisher && RCL_RET_OK != ret)) + { + introspection_clock_.reset(); + } + if (RCL_RET_OK != ret) { rclcpp::exceptions::throw_from_rcl_error(ret, "failed to configure service introspection"); } diff --git a/rclcpp/test/rclcpp/test_service_introspection.cpp b/rclcpp/test/rclcpp/test_service_introspection.cpp index 6d56141031..8a66d78a05 100644 --- a/rclcpp/test/rclcpp/test_service_introspection.cpp +++ b/rclcpp/test/rclcpp/test_service_introspection.cpp @@ -431,3 +431,53 @@ TEST_F(TestServiceIntrospection, service_introspection_enable_disable_event_cont } } } + +TEST_F(TestServiceIntrospection, clock_destroyed_before_client_and_service) +{ + auto clock = std::make_shared(RCL_ROS_TIME); + std::weak_ptr weak_clock = clock; + client->configure_introspection( + clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS); + service->configure_introspection( + clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS); + clock.reset(); + EXPECT_FALSE(weak_clock.expired()); + + // rcl keeps using the first clock. + client->configure_introspection( + std::make_shared(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_METADATA); + service->configure_introspection( + std::make_shared(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_METADATA); + EXPECT_FALSE(weak_clock.expired()); + + client.reset(); + service.reset(); + EXPECT_TRUE(weak_clock.expired()); +} + +TEST_F(TestServiceIntrospection, clock_released_when_introspection_turned_off) +{ + auto clock = std::make_shared(RCL_ROS_TIME); + std::weak_ptr weak_clock = clock; + client->configure_introspection( + clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS); + service->configure_introspection( + clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS); + clock.reset(); + + client->configure_introspection( + std::make_shared(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_OFF); + service->configure_introspection( + std::make_shared(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_OFF); + EXPECT_TRUE(weak_clock.expired()); + + // Re-enabling uses the new clock. + auto new_clock = std::make_shared(RCL_ROS_TIME); + std::weak_ptr weak_new_clock = new_clock; + client->configure_introspection( + new_clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS); + service->configure_introspection( + new_clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_CONTENTS); + new_clock.reset(); + EXPECT_FALSE(weak_new_clock.expired()); +}