Skip to content

destroying_publisher in rclpy fails when used with Cyclone DDS  #762

Closed

Description

@mitsudome-r

Bug report

Required Info:

  • Operating System:
    • Ubuntu 20.04
  • Installation type:
    • binaries
  • Version or commit hash:
    • foxy
  • DDS implementation:
    • Cyclone DDS
  • Client library (if applicable):
    • rclpy

Steps to reproduce issue

Run following commands with Cyclone DDS

# clone sample python code with destroying publisher
mkdir  -p ~/test_ws/src
cd ~/test_ws/src
git clone --single-branch --branch destroy_publisher https://github.com/mitsudome-r/examples

# build
cd ~/test_ws
colcon build

# run sample node
ros2 run examples_rclpy_minimal_publisher publisher_local_function

Expected behavior

Node runs, and you don't see the /topic in the publisher when you check with ros2 node info /minimal_publisher.

Actual behavior

Node shuts down with the following error

[INFO] [1617372037.108382091] [minimal_publisher]: Publishing: "Hello World: 0"
Traceback (most recent call last):
  File "/home/mitsudome-r/test_ws/install/examples_rclpy_minimal_publisher/lib/examples_rclpy_minimal_publisher/publisher_local_function", line 11, in <module>
    load_entry_point('examples-rclpy-minimal-publisher==0.9.4', 'console_scripts', 'publisher_local_function')()
  File "/home/mitsudome-r/test_ws/install/examples_rclpy_minimal_publisher/lib/python3.8/site-packages/examples_rclpy_minimal_publisher/publisher_local_function.py", line 35, in main
    rclpy.spin(node)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/__init__.py", line 191, in spin
    executor.spin_once()
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 703, in spin_once
    handler, entity, node = self.wait_for_ready_callbacks(timeout_sec=timeout_sec)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 689, in wait_for_ready_callbacks
    return next(self._cb_iter)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/executors.py", line 583, in _wait_for_ready_callbacks
    waitable.add_to_wait_set(wait_set)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/qos_event.py", line 177, in add_to_wait_set
    with self._event_handle as event_capsule:
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/handle.py", line 124, in __enter__
    return self._get_capsule()
  File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/handle.py", line 106, in _get_capsule
    raise InvalidHandle('Tried to use a handle that has been destroyed.')
rclpy.handle.InvalidHandle: Tried to use a handle that has been destroyed.

Additional information

Relevant code is located here within the forked repo: https://github.com/mitsudome-r/examples/blob/master/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_local_function.py

This does not occur with FastRTPS and only occurs when I switch to Cyclone DDS so I'm doubting it is something to do with rmw_cyclonedds implementation.

Activity

  1. eboasson commented on Apr 2, 2021

    @eboasson

    Hi @mitsudome-r, the problem is that with Cyclone rclpy adds a publisher event to set of waitables for the wait set that spin eventually uses, but it doesn't remove it from that set when the publisher is destroyed. With Fast-RTPS it never adds such an event, and so there is nothing to remove and all goes well.

    If you would like to see this for yourself, I patched ros2/rclpy/rclpy/rclpy/node.py like this:

    --- /Users/erik/ros2_ws/src/ros2/rclpy/rclpy/rclpy/node.py	2021-04-02 20:23:43.000000000 +0200
    +++ /Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/node.py	2021-04-02 20:25:53.000000000 +0200
    @@ -1276,7 +1276,9 @@
             self.__publishers.append(publisher)
             self._wake_executor()
     
    +        print('created publisher')
             for event_callback in publisher.event_handlers:
    +            print('add waitable')
                 self.add_waitable(event_callback)
     
             return publisher
    @@ -1522,9 +1524,12 @@
     
             :return: ``True`` if successful, ``False`` otherwise.
             """
    +        print('destroy_publisher')
             if publisher in self.__publishers:
    +            print('for real')
                 self.__publishers.remove(publisher)
                 for event_handler in publisher.event_handlers:
    +                print('remove waitable')
                     self.__waitables.remove(event_handler)
                 try:
                     publisher.destroy()

    and ros2/rclpy/rclpy/rclpy/qos_event.py:

    --- /Users/erik/ros2_ws/src/ros2/rclpy/rclpy/rclpy/qos_event.py	2021-04-01 09:05:35.000000000 +0200
    +++ /Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/qos_event.py	2021-04-02 20:36:27.000000000 +0200
    @@ -331,6 +331,7 @@
                         parent_handle=publisher_handle))
     
                 except UnsupportedEventTypeError:
    +                print('not installing handler')
                     pass
     
             return event_handlers

    and then the difference between the outputs is:

    Cyclone

    created publisher
    add waitable
    created publisher
    add waitable
    [INFO] [1617387933.277357373] [minimal_publisher]: Publishing: "Hello World: 0"
    Traceback (most recent call last):
      File "/Users/erik/test_ws/src/examples/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_local_function.py", line 41, in <module>
        main()
      File "/Users/erik/test_ws/src/examples/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_local_function.py", line 35, in main
        rclpy.spin(node)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/__init__.py", line 196, in spin
        executor.spin_once()
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/executors.py", line 694, in spin_once
        handler, entity, node = self.wait_for_ready_callbacks(timeout_sec=timeout_sec)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/executors.py", line 680, in wait_for_ready_callbacks
        return next(self._cb_iter)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/executors.py", line 574, in _wait_for_ready_callbacks
        waitable.add_to_wait_set(wait_set)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/qos_event.py", line 167, in add_to_wait_set
        with self._event_handle as event_capsule:
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/handle.py", line 122, in __enter__
        return self._get_capsule()
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/handle.py", line 104, in _get_capsule
        raise InvalidHandle('Tried to use a handle that has been destroyed.')
    rclpy._rclpy_pybind11.InvalidHandle: Tried to use a handle that has been destroyed.
    

    As you can see, the publisher.destroy() operation in your reproducer never got executed. So that's where things go awry.

    FastRTPS

    not installing handler
    created publisher
    not installing handler
    created publisher
    [INFO] [1617387908.767713479] [minimal_publisher]: Publishing: "Hello World: 0"
    ^CTraceback (most recent call last):
      File "/Users/erik/test_ws/src/examples/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_local_function.py", line 41, in <module>
        main()
      File "/Users/erik/test_ws/src/examples/rclpy/topics/minimal_publisher/examples_rclpy_minimal_publisher/publisher_local_function.py", line 35, in main
        rclpy.spin(node)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/__init__.py", line 196, in spin
        executor.spin_once()
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/executors.py", line 694, in spin_once
        handler, entity, node = self.wait_for_ready_callbacks(timeout_sec=timeout_sec)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/executors.py", line 680, in wait_for_ready_callbacks
        return next(self._cb_iter)
      File "/Users/erik/ros2_ws/install/rclpy/lib/python3.9/site-packages/rclpy/executors.py", line 577, in _wait_for_ready_callbacks
        _rclpy.rclpy_wait(wait_set, timeout_nsec)
    KeyboardInterrupt
    

    This never adds a waitable in create_publisher. The reason is that you are getting the default set of event handlers, which is the "incompatible QoS" notification if it is supported by the RMW layer. And where Cyclone implements that one, FastRTPS doesn't.

    I don't know how to move this to rclpy, but that's where this problem belongs.

  2. sloretz commented on Apr 7, 2021

    @sloretz
    Contributor

    @mitsudome-r would you mind checking if #761 fixes this issue? It sounds like it might from @eboasson's investigation.

  3. clalancette commented on Apr 22, 2021

    @clalancette
    Contributor

    I'm going to go ahead and assume that #761 fixed this issue, and close it. If that is not the case please feel free to reopen.

  4. suurjaak commented on Feb 9, 2022

    @suurjaak
    Contributor

    #761 targets Galactic only. Is this going to get fixed in Foxy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions