ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

Retrieve error message from timer callback when using MultiThreadedExecutor

asked 2022-09-16 07:18:56 -0600

dannee gravatar image

Hi,

I'm not getting proper error messages when I use a timer in combination with a MultiThreadedExecutor. Instead of crashing and displaying which line the error occurred I'm just getting The following exception was not retrieved... Here is a minimal example:

import rclpy
from rclpy.node import Node
from rclpy.executors import MultiThreadedExecutor

class TimerNode(Node):

    def __init__(self):
        super().__init__('timer_test_node')
        self.timer = self.create_timer(
            0.1, 
            self.timer_callback,
        )

    def timer_callback(self):
        print("Timer callback called")
        variable

def main(*args, **kwargs):
    rclpy.init(*args, **kwargs)
    node = TimerNode(*args, **kwargs)

    executor = MultiThreadedExecutor(num_threads=4)
    executor.add_node(node)
    executor.spin()

if __name__ == '__main__':
    main()

Instead of crashing the following lines are repeated:

Timer callback called

The following exception was never retrieved: name 'variable' is not defined

Does anyone know how retrieve the exception and display the proper error message? It crashes as it should when using the default executor. It also crashes as it should when the error is not a timer callback function, i.e when called in a normal function.

edit retag flag offensive close merge delete

Comments

The error message ...name 'variable' is not defined totally makes sense. It should be either self.variable or variable = 1234 # something. Right now, you are using this variable without defining it.

ravijoshi gravatar image ravijoshi  ( 2022-09-17 12:51:15 -0600 )edit

@ravijoshi Thanks for your comment. However I know that my variable is not defined. It's not defined in order to crash the node. My problem is with how the error is displayed. I would something like this:

  File "/home/daniel/wl_meta_ws/build/timer_test/timer_test/timer_test.py", line 16, in timer_callback variable
  NameError: name 'variable' is not defined

Since it is annoying to debug nodes when I can't see which line caused the error

dannee gravatar image dannee  ( 2022-09-18 07:45:46 -0600 )edit

Sorry, can you post a minimal reproducible example? It is difficult to understand what actually caused the error in the real case.

ravijoshi gravatar image ravijoshi  ( 2022-09-18 08:54:13 -0600 )edit

I think we misunderstand each other. My problem is not what is causing the errors. It's about how errors are displayed in the termimal when I have them. Currently errors are displayed as:

The following exception was never retrieved: name 'variable' is not defined

Instead of the normal error message like this:

File "/home/daniel/wl_meta_ws/build/timer_test/timer_test/timer_test.py", line 16, in timer_callback variable NameError: name 'variable' is not defined

The code used in the post is a minimal repriducible example of my issues with the error message format.

dannee gravatar image dannee  ( 2022-09-18 09:08:05 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-09-18 21:18:57 -0600

ravijoshi gravatar image

Based on the information provided in the comments above, I propose to use the traceback module to retrieve a stack trace. For example, in the given minimal reproducible example, it can be achieved in the following manner:

Code:

ravi@dell:~/ros_ws/src/minimal_publisher/examples_rclpy_minimal_publisher$ cat publisher_member_function.py 
import rclpy
from rclpy.node import Node
from rclpy.executors import MultiThreadedExecutor

import traceback


class TimerNode(Node):
    def __init__(self):
        super().__init__("timer_test_node")
        self.timer = self.create_timer(
            0.1,
            self.timer_callback,
        )

    def timer_callback(self):
        print("Timer callback called")
        try:
            variable
        except Exception:
            self.get_logger().err(traceback.format_exc())


def main(*args, **kwargs):
    rclpy.init(*args, **kwargs)
    node = TimerNode(*args, **kwargs)

    executor = MultiThreadedExecutor(num_threads=4)
    executor.add_node(node)
    executor.spin()


if __name__ == "__main__":
    main()

Run:

$ ros2 run examples_rclpy_minimal_publisher publisher_member_function
Timer callback called
[ERROR] [1663553499.045761051] [timer_test_node]: Traceback (most recent call last):
  File "/home/ravi/ros_ws/install/examples_rclpy_minimal_publisher/lib/python3.8/site-packages/examples_rclpy_minimal_publisher/publisher_member_function.py", line 19, in timer_callback
    variable
NameError: name 'variable' is not defined
edit flag offensive delete link more

Comments

Thank you!

dannee gravatar image dannee  ( 2022-09-19 01:52:37 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2022-09-16 07:18:56 -0600

Seen: 24 times

Last updated: Sep 18