fix(RosActionNode): swallow UnknownGoalHandleError in cancelGoal by falfab · Pull Request #129 · BehaviorTree/BehaviorTree.ROS2 · GitHub
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
9 changes: 9 additions & 0 deletions behaviortree_ros2/CMakeLists.txt
37 changes: 26 additions & 11 deletions behaviortree_ros2/include/behaviortree_ros2/bt_action_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "behaviortree_cpp/action_node.h"
#include "behaviortree_cpp/bt_factory.h"
#include "rclcpp_action/rclcpp_action.hpp"
#include "rclcpp_action/exceptions.hpp"

#include "behaviortree_ros2/ros_node_params.hpp"

Expand Down Expand Up @@ -558,21 +559,35 @@ inline void RosActionNode<T>::cancelGoal()

auto& action_client = client_instance_->action_client;

auto future_result = action_client->async_get_result(goal_handle_);
auto future_cancel = action_client->async_cancel_goal(goal_handle_);
try
{
auto future_result = action_client->async_get_result(goal_handle_);
auto future_cancel = action_client->async_cancel_goal(goal_handle_);

constexpr auto SUCCESS = rclcpp::FutureReturnCode::SUCCESS;
constexpr auto SUCCESS = rclcpp::FutureReturnCode::SUCCESS;

if(executor.spin_until_future_complete(future_cancel, server_timeout_) != SUCCESS)
{
RCLCPP_ERROR(logger(), "Failed to cancel action server for [%s]",
action_name_.c_str());
}
if(executor.spin_until_future_complete(future_cancel, server_timeout_) != SUCCESS)
{
RCLCPP_ERROR(logger(), "Failed to cancel action server for [%s]",
action_name_.c_str());
}

if(executor.spin_until_future_complete(future_result, server_timeout_) != SUCCESS)
if(executor.spin_until_future_complete(future_result, server_timeout_) != SUCCESS)
{
RCLCPP_ERROR(logger(), "Failed to get result call failed :( for [%s]",
action_name_.c_str());
}
}
catch(const rclcpp_action::exceptions::UnknownGoalHandleError& e)
{
RCLCPP_ERROR(logger(), "Failed to get result call failed :( for [%s]",
action_name_.c_str());
// The action server's terminal result arrived and the rclcpp_action
// client's result callback has already erased the goal from its
// internal registry before we reached async_get_result /
// async_cancel_goal. This is a benign race: the goal has already
// reached a terminal state, which is exactly what cancelGoal is
// trying to achieve.
RCLCPP_DEBUG(logger(), "Goal already terminal at cancel time for [%s]: %s",
action_name_.c_str(), e.what());
}
}

Expand Down
2 changes: 2 additions & 0 deletions behaviortree_ros2/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<depend>generate_parameter_library</depend>
<depend>btcpp_ros2_interfaces</depend>

<test_depend>ament_cmake_gtest</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
Expand Down
221 changes: 221 additions & 0 deletions behaviortree_ros2/test/test_cancel_goal_race.cpp