From 9d08482ced0b417b2ec694c9d4ea241680a5fdbe Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Sat, 28 Nov 2026 11:09:59 -0600 Subject: [PATCH] Keep the introspection clock alive for the lifetime of the service/client The introspection publisher stores a raw pointer to the clock's rcl handle and dereferences it when the service or client is finalized. Neither Service nor Client held a reference to the clock passed to configure_introspection(), so destroying the clock first (e.g. the node's clock when the node is destroyed before the service) left rcl_service_fini()/rcl_client_fini() reading freed memory. Found with AddressSanitizer. Signed-off-by: Michael Carroll --- rclcpp/include/rclcpp/client.hpp | 6 ++++++ rclcpp/include/rclcpp/service.hpp | 6 ++++++ rclcpp/test/rclcpp/test_service_introspection.cpp | 14 ++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/rclcpp/include/rclcpp/client.hpp b/rclcpp/include/rclcpp/client.hpp index 491be8c23a..45a88ace27 100644 --- a/rclcpp/include/rclcpp/client.hpp +++ b/rclcpp/include/rclcpp/client.hpp @@ -325,6 +325,10 @@ class ClientBase std::shared_ptr context_; rclcpp::Logger node_logger_; + // The introspection publisher keeps a raw pointer to this clock's rcl handle and uses it while + // finalizing the client. Keep the clock alive until client_handle_ (declared below) is finalized. + rclcpp::Clock::SharedPtr introspection_clock_; + std::recursive_mutex callback_mutex_; // It is important to declare on_new_response_callback_ before // client_handle_, so on destruction the client is @@ -759,6 +763,8 @@ class Client : public ClientBase if (RCL_RET_OK != ret) { rclcpp::exceptions::throw_from_rcl_error(ret, "failed to configure client introspection"); } + + introspection_clock_ = clock; } protected: diff --git a/rclcpp/include/rclcpp/service.hpp b/rclcpp/include/rclcpp/service.hpp index 3afe1d2e81..909d10fc6a 100644 --- a/rclcpp/include/rclcpp/service.hpp +++ b/rclcpp/include/rclcpp/service.hpp @@ -212,6 +212,10 @@ class ServiceBase std::shared_ptr node_handle_; + // The introspection publisher keeps a raw pointer to this clock's rcl handle and uses it while + // finalizing the service. Keep the clock alive until service_handle_ (declared below) is finalized. + rclcpp::Clock::SharedPtr introspection_clock_; + std::recursive_mutex callback_mutex_; // It is important to declare on_new_request_callback_ before // service_handle_, so on destruction the service is @@ -475,6 +479,8 @@ class Service if (RCL_RET_OK != ret) { rclcpp::exceptions::throw_from_rcl_error(ret, "failed to configure service introspection"); } + + introspection_clock_ = clock; } private: diff --git a/rclcpp/test/rclcpp/test_service_introspection.cpp b/rclcpp/test/rclcpp/test_service_introspection.cpp index 6d56141031..5e56710ff9 100644 --- a/rclcpp/test/rclcpp/test_service_introspection.cpp +++ b/rclcpp/test/rclcpp/test_service_introspection.cpp @@ -431,3 +431,17 @@ TEST_F(TestServiceIntrospection, service_introspection_enable_disable_event_cont } } } + +TEST_F(TestServiceIntrospection, introspection_keeps_clock_alive) +{ + // The introspection publisher uses the clock while the service/client are finalized, so the + // service/client must keep it alive even if the caller drops its reference first. + auto clock = std::make_shared(RCL_STEADY_TIME); + client->configure_introspection(clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_METADATA); + service->configure_introspection( + clock, rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_METADATA); + clock.reset(); + + client.reset(); + service.reset(); +}