rclcpp icon indicating copy to clipboard operation
rclcpp copied to clipboard

Program exits with code -11 when using async_send_request to set parameters in ROS 2 C++ client

Open huweiATgithub opened this issue 1 year ago • 1 comments

Bug report

Required Info:

  • Operating System:
    • Ubuntu 22.04
  • Installation type:
    • binary (apt)
  • Version or commit hash:
    • humble
  • DDS implementation:
    • default
  • Client library (if applicable):
    • rclcpp

Steps to reproduce issue

I am encountering a segmentation fault (exit code -11) when using the async_send_request method to set parameters in a ROS 2 C++ client. Below is the relevant code snippet:

const auto set_parameters_req = std::make_shared<rcl_interfaces::srv::SetParameters::Request>();
auto set_parameters_resp = set_parameters_client_->async_send_request(set_parameters_req);
EXPECT_EQ(rclcpp::FutureReturnCode::SUCCESS, rclcpp::spin_until_future_complete(node_, set_parameters_resp));

Actual behavior

When executing the above code, the program exits with code -11 at the spin_until_future_complete call.

Additional information

The direct use of ros2 service call from the command line works correctly, and its implementation in Python at ros2cli GitHub resembles the C++ code I am using.

An alternative approach, using a promise and callback with async_send_request, does work as shown below. This approach is inspired by the implementation in rclcpp::SyncParametersClient:

auto promise_result = std::make_shared<std::promise<std::vector<rcl_interfaces::msg::SetParametersResult>>>();
set_parameters_client_->async_send_request(
  set_parameters_req,
  [promise_result, future_result](rclcpp::Client<rcl_interfaces::srv::SetParameters>::SharedFuture cb_f) {
    promise_result->set_value(cb_f.get()->results);
  }
);
rclcpp::spin_until_future_complete(test_node_, future_result);

Questions:

  • Why does the first approach result in a segmentation fault while the second approach and the Python version work correctly?
  • Are there any general rules or guidelines for using async_send_request in ROS 2 C++ clients to avoid such issues?

huweiATgithub avatar May 20 '24 10:05 huweiATgithub

@huweiATgithub

i know the there are many test cases something like this, https://github.com/ros2/rclcpp/blob/343b29b617b163ad72b9fe3f6441dd4ed3d3af09/rclcpp/test/rclcpp/test_logger_service.cpp#L72-L75

and all tests do pass.

can you share more details like stack trace information, reproducible colcon package?

fujitatomoya avatar May 20 '24 18:05 fujitatomoya