Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid reference cycle between Node and ParameterService #490

Merged
merged 4 commits into from
Jan 7, 2020
Merged
Changes from 3 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
28 changes: 21 additions & 7 deletions rclpy/rclpy/parameter_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import weakref

from rcl_interfaces.msg import ParameterDescriptor
from rcl_interfaces.msg import SetParametersResult
from rcl_interfaces.srv import DescribeParameters, GetParameters, GetParameterTypes
Expand All @@ -25,7 +27,7 @@
class ParameterService:

def __init__(self, node):
self._node = node
self._node_weak_ref = weakref.ref(node)
nodename = node.get_name()

describe_parameters_service_name = \
Expand Down Expand Up @@ -64,28 +66,32 @@ def __init__(self, node):
)

def _describe_parameters_callback(self, request, response):
node = self._get_node()
for name in request.names:
try:
descriptor = self._node.describe_parameter(name)
descriptor = node.describe_parameter(name)
except ParameterNotDeclaredException:
descriptor = ParameterDescriptor()
response.descriptors.append(descriptor)
return response

def _get_parameters_callback(self, request, response):
node = self._get_node()
for name in request.names:
p = self._node.get_parameter_or(name)
p = node.get_parameter_or(name)
response.values.append(p.get_parameter_value())
return response

def _get_parameter_types_callback(self, request, response):
node = self._get_node()
for name in request.names:
response.types.append(self._node.get_parameter_or(name).type_)
response.types.append(node.get_parameter_or(name).type_)
return response

def _list_parameters_callback(self, request, response):
names_with_prefixes = []
for name in self._node._parameters.keys():
node = self._get_node()
for name in node._parameters.keys():
if PARAMETER_SEPARATOR_STRING in name:
names_with_prefixes.append(name)
continue
Expand Down Expand Up @@ -125,10 +131,11 @@ def _list_parameters_callback(self, request, response):
return response

def _set_parameters_callback(self, request, response):
node = self._get_node()
for p in request.parameters:
param = Parameter.from_parameter_msg(p)
try:
result = self._node.set_parameters_atomically([param])
result = node.set_parameters_atomically([param])
except ParameterNotDeclaredException as e:
result = SetParametersResult(
successful=False,
Expand All @@ -138,12 +145,19 @@ def _set_parameters_callback(self, request, response):
return response

def _set_parameters_atomically_callback(self, request, response):
node = self._get_node()
try:
response.result = self._node.set_parameters_atomically([
response.result = node.set_parameters_atomically([
Parameter.from_parameter_msg(p) for p in request.parameters])
except ParameterNotDeclaredException as e:
response.result = SetParametersResult(
successful=False,
reason=str(e)
)
return response

def _get_node(self):
node = self._node_weak_ref()
if node is None:
raise RuntimeError('Expected valid node weak reference')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a ValueError instead (or what is the reason to choose RuntimeError)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, ValueError is used when an argument passed by the user is not of the appropiate value. There's not "argument" involved here, just an attribute of the object being invalid.

RuntimeError is the choice when the error doesn't fall in any of the other categories (link), and I don't think there is a more appropiate standard error.
I also can create a custom error if that sounds better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mostly wondering because RuntimeError is treated differently by the command line tools - they suppress the stacktrace and only show the exception string. Not sure if that is intended in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like ReferenceError is what we want here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It sounds like ReferenceError is what we want here.

Sounds like the right option! Updated the PR.

I was mostly wondering because RuntimeError is treated differently by the command line tools - they suppress the stacktrace and only show the exception string. Not sure if that is intended in this case.

A little off-topic:
I have observed this, and find that behavior strange. Why do the command line tools do that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some kind of error messages don't warrant to show a stacktrace to the user. And in those cases we use RuntimeError.

return node