Skip to content

Commit

Permalink
Guard against making multiple result requests for a goal handle (#808)
Browse files Browse the repository at this point in the history
This fixes a runtime error caused by a race condition when making consecutive requests for the
result.
Specifically, this happens if the user provides a result callback when sending a goal and then
calls async_get_result shortly after.

Resolves #783

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron authored Aug 7, 2019
1 parent 871c375 commit 41a99b6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
10 changes: 5 additions & 5 deletions rclcpp_action/include/rclcpp_action/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,7 @@ class Client : public ClientBase
// This will override any previously registered callback
goal_handle->set_result_callback(result_callback);
}
// If the user chose to ignore the result before, then ask the server for the result now.
if (!goal_handle->is_result_aware()) {
this->make_result_aware(goal_handle);
}
this->make_result_aware(goal_handle);
return goal_handle->async_result();
}

Expand Down Expand Up @@ -595,6 +592,10 @@ class Client : public ClientBase
void
make_result_aware(typename GoalHandle::SharedPtr goal_handle)
{
// Avoid making more than one request
if (goal_handle->set_result_awareness(true)) {
return;
}
using GoalResultRequest = typename ActionT::Impl::GetResultService::Request;
auto goal_result_request = std::make_shared<GoalResultRequest>();
goal_result_request->goal_id.uuid = goal_handle->get_goal_id();
Expand All @@ -614,7 +615,6 @@ class Client : public ClientBase
std::lock_guard<std::mutex> lock(goal_handles_mutex_);
goal_handles_.erase(goal_handle->get_goal_id());
});
goal_handle->set_result_awareness(true);
}

/// \internal
Expand Down
3 changes: 2 additions & 1 deletion rclcpp_action/include/rclcpp_action/client_goal_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ class ClientGoalHandle
typename ClientGoalHandle<ActionT>::SharedPtr shared_this,
typename std::shared_ptr<const Feedback> feedback_message);

void
/// Returns the previous value of awareness
bool
set_result_awareness(bool awareness);

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ ClientGoalHandle<ActionT>::is_result_aware()
}

template<typename ActionT>
void
bool
ClientGoalHandle<ActionT>::set_result_awareness(bool awareness)
{
std::lock_guard<std::mutex> guard(handle_mutex_);
bool previous = is_result_aware_;
is_result_aware_ = awareness;
return previous;
}

template<typename ActionT>
Expand Down

0 comments on commit 41a99b6

Please sign in to comment.