Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rclcpp/include/rclcpp/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ class ClientBase
std::shared_ptr<rclcpp::Context> 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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/include/rclcpp/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ class ServiceBase

std::shared_ptr<rcl_node_t> 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
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions rclcpp/test/rclcpp/test_service_introspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rclcpp::Clock>(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();
}
Loading