Skip to content

ROS community proposes exception handling policies for rclcpp and rclpy executors to manage invalid message timestamps

A ROS Discourse discussion suggests adding configurable exception handling policies to rclcpp and rclpy executors, allowing users to define custom rules like ignoring specific message_filters exceptions during node spin operations. The proposal aims to prevent single invalid header timestamps from dropping an entire robot pipeline while maintaining backward compatibility and keeping debugging straightforward by distinguishing component-specific errors.

Article text · Original source

Problem at hand

There is this issue in the message_filters repository. The reporter states that any message that has an invalid header timestamp can drop the entire robot’s pipeline. The options that they have at the moment are, as I see it:

  1. Add custom filters to all incoming data streams that drop messages with invalid timestamps: Valid option, but a bit cumbersome.
  2. Wrap the high level node.spin in a try/catch clause. Also valid, but it is kind of strange to me. Such code may read as “Do work but keep in mind that somewhere there may arise an exception that you should ignore/reraise/log and reraise/convert to something else”. It does the job, but seems a bit awkward to me.
  3. Get sensors or write code that does not generate invalid timestamps in the first place. But this is from the realm of sci-fi and not humanly possible if you ask me.

Surely there are other ways to address this issue.

The idea

This issue got me thinking. Why not add some kind of exception handling policy entity to, maybe, rclcpp(rclpy) executor. The policy that a user may specify when constructing a Node, calling spin, or when constructing an executor. From the design standpoint it may look something like this:

(I’ll use Python-ish pseudo code for simplicity and clarity, but the same idea goes for cpp)

class MyExceptionThrowingNode(rclpy.Node):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)  # Just a formality in this example

        ...     # Some work done here to initialize node components

        self._subscriber_filter = message_filters.Subscriber(
            callback=self._throwing_callback,
            ...      # Let's assume it is initialized correctly
         )


def reraise_keyboard_interrupt(exception: Exception):
    if isinstance(exception, KeyboardInterrupt):
        raise exception


def main():
    node = MyExceptionThrowingNode(...)

    executor_with_error_handling_policy = rclpy.executors.SingleThreadedExecutor(
        ...,
        error_handling_policy=reraise_keyboard_interrupt,
    )

    exeuotor_with_error_handling_policy.add_node(node)

    try:
        executor_with_error_handling_policy.spin()
    except KeyboardInterrupt as ex:
        pass
    finally:
        exeuotor_with_error_handling_policy.shutdown()
        node.destroy_node()
        rclpy.shutdown()
        

How this can help

If we look at it from the perspective of the original issue, and if we add custom package-specific exceptions to the message_filters and ensure that this package may throw only these exceptions then, in case of the original issue reporter, the exception policy may look something like:

def ignore_message_filters_exceptions(exception: Exception):
    if not isinstance(exception, message_filters.BaseMessageFiltersException):
        raise exception

or even

def ignore_message_filters_invalid_timestamp_exceptions(exception: Exception):
    if not isinstance(exception, message_filters.NegativeTimestampException):
        raise exception

The Synchronizer filter will just ignore the invalid message as if there were no any and go on with it’s day.

Possible concerns

  1. ABI compatibility for C++
  2. Backwards compatibility. If this is added, the behavior of the unchanged user code should stay the same.
  3. This may require adding a number of custom exceptions to a number of ros packages so that the error handling policy may distinguish where does the exception come from.

Other approaches that I’ve considered but decided not to put here in details

Add the same exception_handling_policy to multiple components, such as message_filters.SimpleFilter or rclpy(cpp).Subscriber. Why not: This blurs the responsibility. By default, as I see it, the exceptions should be raised immediately. If there are a number of different-level components that may raise exceptions and may also ignore them it may become a nightmare to debug such code. Also, subscribers, services, message filters should be responsible only for there’s specific functionality. Not for the exception handling.

PS

This is more of a discussion starter. I feel interested in implementing something like this, but I wanted to discuss it and finalize some kind of a high level design before starting to write code. It would be great to have some feedback on the idea.
I’ve discussed with my colleagues and came to a realization that we have something like this already in our product but not exactly like this. At least not for Executors as I understand.

Timezone · UTC

Article dates follow your selected timezone. Briefing editions use Hong Kong time (UTC+8).