Skip to content

Commit f0283d4

Browse files
Janosch MachowinskiJanosch Machowinski
authored andcommitted
feat: De-register ClientGoalHandles in a thread safe way.
Signed-off-by: Janosch Machowinski <[email protected]>
1 parent b3b35e3 commit f0283d4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

rclcpp_action/include/rclcpp_action/client.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ class ClientBase : public rclcpp::Waitable
333333
* - calling user callbacks.
334334
*/
335335
template<typename ActionT>
336-
class Client : public ClientBase
336+
class Client : public ClientBase, public std::enable_shared_from_this<Client<ActionT>>
337337
{
338338
public:
339339
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(Client<ActionT>)
@@ -446,7 +446,9 @@ class Client : public ClientBase
446446
goal_info.stamp = goal_response->stamp;
447447
// Do not use std::make_shared as friendship cannot be forwarded.
448448
std::shared_ptr<GoalHandle> goal_handle(
449-
new GoalHandle(goal_info, options.feedback_callback, options.result_callback));
449+
new GoalHandle(
450+
this->shared_from_this(), goal_info, options.feedback_callback,
451+
options.result_callback));
450452
{
451453
std::lock_guard<std::mutex> guard(goal_handles_mutex_);
452454
goal_handles_[goal_handle->get_goal_id()] = goal_handle;
@@ -563,9 +565,13 @@ class Client : public ClientBase
563565
}
564566

565567
void drop_goal_handle(typename GoalHandle::SharedPtr goal_handle)
568+
{
569+
drop_goal_handle(goal_handle->get_goal_id());
570+
}
571+
572+
void drop_goal_handle(const GoalUUID & goal_id)
566573
{
567574
std::lock_guard<std::mutex> guard(goal_handles_mutex_);
568-
const GoalUUID & goal_id = goal_handle->get_goal_id();
569575
if (goal_handles_.count(goal_id) == 0) {
570576
// someone else already deleted the entry
571577
// e.g. the result callback

rclcpp_action/include/rclcpp_action/client_goal_handle.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ class ClientGoalHandle
107107
friend class Client<ActionT>;
108108

109109
ClientGoalHandle(
110+
const typename Client<ActionT>::SharedPtr & client,
110111
const GoalInfo & info,
111112
FeedbackCallback feedback_callback,
112113
ResultCallback result_callback);
@@ -163,6 +164,8 @@ class ClientGoalHandle
163164
ResultCallback result_callback_{nullptr};
164165
int8_t status_{GoalStatus::STATUS_ACCEPTED};
165166

167+
typename std::weak_ptr<Client<ActionT>> client_weak_ptr_;
168+
166169
std::mutex handle_mutex_;
167170
};
168171
} // namespace rclcpp_action

rclcpp_action/include/rclcpp_action/client_goal_handle_impl.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@ namespace rclcpp_action
2828

2929
template<typename ActionT>
3030
ClientGoalHandle<ActionT>::ClientGoalHandle(
31+
const typename Client<ActionT>::SharedPtr & client,
3132
const GoalInfo & info, FeedbackCallback feedback_callback, ResultCallback result_callback)
3233
: info_(info),
3334
result_future_(result_promise_.get_future()),
3435
feedback_callback_(feedback_callback),
35-
result_callback_(result_callback)
36+
result_callback_(result_callback),
37+
client_weak_ptr_(client)
3638
{
3739
}
3840

3941
template<typename ActionT>
4042
ClientGoalHandle<ActionT>::~ClientGoalHandle()
4143
{
44+
typename Client<ActionT>::SharedPtr client = client_weak_ptr_.lock();
45+
if (client) {
46+
client->drop_goal_handle(get_goal_id());
47+
}
4248
}
4349

4450
template<typename ActionT>

0 commit comments

Comments
 (0)