Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Close a node from inside Node class

I want to shutdown a node inside the own node class in case a certain condition is fullfilled (like receive a message or a certain variable gets a certain value).

import rclpy
from rclpy.node import Node
from std_msgs.msg import Float32MultiArray

class ProcessNode(Node):

    def __init__(self, number_spot=0):
        super().__init__('process_node')
        self._cur_loc_subscriber = self.create_subscription(String, 'request_user_ip', self.listener_callback)


    def listener_callback(self):
            self.destroy_node()
            rclpy.shutdown()

def main(args=None):
    rclpy.init(args=args)
    minimal_action_server = ProcessNode()
    rclpy.spin(minimal_action_server)
    minimal_action_server.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()

If I run this code I get the error:

RuntimeError: Failed to shutdown: rcl_shutdown already called on the given context, at /tmp/binarydeb/ros-crystal-rcl-0.6.5/src/rcl/init.c:164

How can I close a node from inside the class while keeping rclpy.shutdown() in the main in case I want to terminate from terminal?