diff --git a/rclcpp_action/include/rclcpp_action/server_goal_handle.hpp b/rclcpp_action/include/rclcpp_action/server_goal_handle.hpp index 33ccb64c87..b1ccb78b1e 100644 --- a/rclcpp_action/include/rclcpp_action/server_goal_handle.hpp +++ b/rclcpp_action/include/rclcpp_action/server_goal_handle.hpp @@ -151,13 +151,20 @@ class ServerGoalHandle : public ServerGoalHandleBase * If execution of a goal is deferred then `ServerGoalHandle::set_executing()` must be called * first. * - * \throws std::runtime_error If the goal is in any state besides executing. + * If the goal is in any state besides executing, a warning will be logged and the feedback + * will not be published. * * \param[in] feedback_msg the message to publish to clients. */ void publish_feedback(std::shared_ptr feedback_msg) { + if (!is_executing()) { + RCLCPP_WARN( + rclcpp::get_logger("rclcpp_action"), + "publish_feedback() called on a goal handle not in executing state, ignoring"); + return; + } auto feedback_message = std::make_shared(); feedback_message->goal_id.uuid = uuid_; feedback_message->feedback = *feedback_msg; diff --git a/rclcpp_action/test/test_server_goal_handle.cpp b/rclcpp_action/test/test_server_goal_handle.cpp index 9b4e3cbb03..822e5601ed 100644 --- a/rclcpp_action/test/test_server_goal_handle.cpp +++ b/rclcpp_action/test/test_server_goal_handle.cpp @@ -174,6 +174,28 @@ TEST_F(TestServerGoalHandle, execute) { EXPECT_THROW(handle_->execute(), rclcpp::exceptions::RCLError); } +TEST_F(TestServerGoalHandle, publish_feedback_not_executing) { + // Goal is in accepted (not executing) state, publish_feedback should warn and ignore + auto feedback = std::make_shared(); + EXPECT_NO_THROW(handle_->publish_feedback(feedback)); +} + +TEST_F(TestServerGoalHandle, publish_feedback_executing) { + // Goal is in executing state, publish_feedback should succeed + handle_->execute(); + auto feedback = std::make_shared(); + EXPECT_NO_THROW(handle_->publish_feedback(feedback)); +} + +TEST_F(TestServerGoalHandle, publish_feedback_after_succeed) { + // Goal reached terminal state, publish_feedback should warn and ignore + handle_->execute(); + auto result = std::make_shared(); + handle_->succeed(result); + auto feedback = std::make_shared(); + EXPECT_NO_THROW(handle_->publish_feedback(feedback)); +} + TEST_F(TestServerGoalHandle, rcl_action_goal_handle_get_status_error) { auto mock = mocking_utils::patch_and_return( "lib:rclcpp_action", rcl_action_goal_handle_get_status, RCL_RET_ERROR);