Skip to content

Commit 41a99b6

Browse files
authored
Guard against making multiple result requests for a goal handle (#808)
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 <[email protected]>
1 parent 871c375 commit 41a99b6

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

rclcpp_action/include/rclcpp_action/client.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,7 @@ class Client : public ClientBase
410410
// This will override any previously registered callback
411411
goal_handle->set_result_callback(result_callback);
412412
}
413-
// If the user chose to ignore the result before, then ask the server for the result now.
414-
if (!goal_handle->is_result_aware()) {
415-
this->make_result_aware(goal_handle);
416-
}
413+
this->make_result_aware(goal_handle);
417414
return goal_handle->async_result();
418415
}
419416

@@ -595,6 +592,10 @@ class Client : public ClientBase
595592
void
596593
make_result_aware(typename GoalHandle::SharedPtr goal_handle)
597594
{
595+
// Avoid making more than one request
596+
if (goal_handle->set_result_awareness(true)) {
597+
return;
598+
}
598599
using GoalResultRequest = typename ActionT::Impl::GetResultService::Request;
599600
auto goal_result_request = std::make_shared<GoalResultRequest>();
600601
goal_result_request->goal_id.uuid = goal_handle->get_goal_id();
@@ -614,7 +615,6 @@ class Client : public ClientBase
614615
std::lock_guard<std::mutex> lock(goal_handles_mutex_);
615616
goal_handles_.erase(goal_handle->get_goal_id());
616617
});
617-
goal_handle->set_result_awareness(true);
618618
}
619619

620620
/// \internal

rclcpp_action/include/rclcpp_action/client_goal_handle.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ class ClientGoalHandle
134134
typename ClientGoalHandle<ActionT>::SharedPtr shared_this,
135135
typename std::shared_ptr<const Feedback> feedback_message);
136136

137-
void
137+
/// Returns the previous value of awareness
138+
bool
138139
set_result_awareness(bool awareness);
139140

140141
void

rclcpp_action/include/rclcpp_action/client_goal_handle_impl.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ ClientGoalHandle<ActionT>::is_result_aware()
127127
}
128128

129129
template<typename ActionT>
130-
void
130+
bool
131131
ClientGoalHandle<ActionT>::set_result_awareness(bool awareness)
132132
{
133133
std::lock_guard<std::mutex> guard(handle_mutex_);
134+
bool previous = is_result_aware_;
134135
is_result_aware_ = awareness;
136+
return previous;
135137
}
136138

137139
template<typename ActionT>

0 commit comments

Comments
 (0)