Skip to content
Closed
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
16 changes: 16 additions & 0 deletions rclcpp/include/rclcpp/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ class ClientBase
// destroyed first. Otherwise, the rmw client callback
// would point briefly to a destroyed function.
std::function<void(size_t)> 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<rcl_client_t> client_handle_;

Expand Down Expand Up @@ -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(),
Expand All @@ -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");
}
Expand Down
16 changes: 16 additions & 0 deletions rclcpp/include/rclcpp/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ class ServiceBase
// destroyed first. Otherwise, the rmw service callback
// would point briefly to a destroyed function.
std::function<void(size_t)> 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<rcl_service_t> service_handle_;
bool owns_rcl_handle_ = true;
Expand Down Expand Up @@ -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(),
Expand All @@ -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");
}
Expand Down
50 changes: 50 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,53 @@ TEST_F(TestServiceIntrospection, service_introspection_enable_disable_event_cont
}
}
}

TEST_F(TestServiceIntrospection, clock_destroyed_before_client_and_service)
{
auto clock = std::make_shared<rclcpp::Clock>(RCL_ROS_TIME);
std::weak_ptr<rclcpp::Clock> 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::Clock>(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_METADATA);
service->configure_introspection(
std::make_shared<rclcpp::Clock>(), 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<rclcpp::Clock>(RCL_ROS_TIME);
std::weak_ptr<rclcpp::Clock> 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::Clock>(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_OFF);
service->configure_introspection(
std::make_shared<rclcpp::Clock>(), rclcpp::ServicesQoS(), RCL_SERVICE_INTROSPECTION_OFF);
EXPECT_TRUE(weak_clock.expired());

// Re-enabling uses the new clock.
auto new_clock = std::make_shared<rclcpp::Clock>(RCL_ROS_TIME);
std::weak_ptr<rclcpp::Clock> 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());
}
Loading