Skip to content

Commit

Permalink
Maintain weak references to GoalHandles in ActionClient
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Perron <[email protected]>
  • Loading branch information
jacobperron committed Feb 2, 2019
1 parent 102de88 commit e2df735
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions rclpy/rclpy/action/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import threading
import time
import uuid
import weakref

from action_msgs.msg import GoalStatus
from action_msgs.srv import CancelGoal
Expand Down Expand Up @@ -159,7 +160,7 @@ def __init__(

self._is_ready = False

# key: UUID in bytes, value: ClientGoalHandle
# key: UUID in bytes, value: weak reference to ClientGoalHandle
self._goal_handles = {}
# key: goal request sequence_number, value: Future for goal response
self._pending_goal_requests = {}
Expand Down Expand Up @@ -272,7 +273,7 @@ async def execute(self, taken_data):
if goal_uuid in self._goal_handles:
raise RuntimeError(
'Two goals were accepted with the same ID ({})'.format(goal_handle))
self._goal_handles[goal_uuid] = goal_handle
self._goal_handles[goal_uuid] = weakref.ref(goal_handle)

self._pending_goal_requests[sequence_number].set_result(goal_handle)

Expand All @@ -298,11 +299,16 @@ async def execute(self, taken_data):
status = status_msg.status

if goal_uuid in self._goal_handles:
self._goal_handles[goal_uuid]._status = status
# Remove "done" goals from the list
if (GoalStatus.STATUS_SUCCEEDED == status or
GoalStatus.STATUS_CANCELED == status or
GoalStatus.STATUS_ABORTED == status):
goal_handle = self._goal_handles[goal_uuid]()
if goal_handle is not None:
goal_handle._status = status
# Remove "done" goals from the list
if (GoalStatus.STATUS_SUCCEEDED == status or
GoalStatus.STATUS_CANCELED == status or
GoalStatus.STATUS_ABORTED == status):
del self._goal_handles[goal_uuid]
else:
# Weak reference is None
del self._goal_handles[goal_uuid]

def get_num_entities(self):
Expand Down

0 comments on commit e2df735

Please sign in to comment.