From 1a9ff22fbb5d7488f6c3bfb9b2a4f5ada9bf98e5 Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Mon, 14 Dec 2020 14:13:55 +0000 Subject: [PATCH 1/3] Don't throw an exception if timer canceled while sleeping. It is a valid situation that may happen sometimes in threading, so just quietly clean ourselves up and don't throw an exception. Signed-off-by: Chris Lalancette --- rclpy/rclpy/timer.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rclpy/rclpy/timer.py b/rclpy/rclpy/timer.py index 01917126a..a1a58678e 100644 --- a/rclpy/rclpy/timer.py +++ b/rclpy/rclpy/timer.py @@ -131,7 +131,6 @@ def _postsleep(self): self._event.clear() if self._is_shutdown: self.destroy() - raise ROSInterruptException() def sleep(self): """ From 0e75d8767dd45f8d9c174e1ddb26dda02e1d403f Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Mon, 14 Dec 2020 20:09:03 +0000 Subject: [PATCH 2/3] Make sure to catch the ROSInterruptException when calling rate.sleep. This ensures that we don't get an exception in the extra thread, which as of pytest 6.2.0 also causes an exception in the main program. Signed-off-by: Chris Lalancette --- rclpy/rclpy/timer.py | 1 + rclpy/test/test_rate.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/rclpy/rclpy/timer.py b/rclpy/rclpy/timer.py index a1a58678e..01917126a 100644 --- a/rclpy/rclpy/timer.py +++ b/rclpy/rclpy/timer.py @@ -131,6 +131,7 @@ def _postsleep(self): self._event.clear() if self._is_shutdown: self.destroy() + raise ROSInterruptException() def sleep(self): """ diff --git a/rclpy/test/test_rate.py b/rclpy/test/test_rate.py index 97ff3d20b..f0c83bc28 100644 --- a/rclpy/test/test_rate.py +++ b/rclpy/test/test_rate.py @@ -111,6 +111,15 @@ def test_destroy_wakes_rate(self): self._thread.join() +def sleep_check_exception(rate): + try: + rate.sleep() + except ROSInterruptException: + # rate.sleep() can raise ROSInterruptException if the context is + # shutdown while it is sleeping. Just ignore it here. + pass + + def test_shutdown_wakes_rate(): context = rclpy.context.Context() rclpy.init(context=context) @@ -120,7 +129,7 @@ def test_shutdown_wakes_rate(): rate = node.create_rate(0.0000001) - _thread = threading.Thread(target=rate.sleep, daemon=True) + _thread = threading.Thread(target=sleep_check_exception, args=rate, daemon=True) _thread.start() executor.shutdown() node.destroy_node() From e2ba3ba6889189f46c2de23a2754e87c5d7007bb Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Mon, 14 Dec 2020 20:41:42 +0000 Subject: [PATCH 3/3] Fixes from review/CI. Signed-off-by: Chris Lalancette --- rclpy/test/test_rate.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rclpy/test/test_rate.py b/rclpy/test/test_rate.py index f0c83bc28..9f2b854ef 100644 --- a/rclpy/test/test_rate.py +++ b/rclpy/test/test_rate.py @@ -17,6 +17,7 @@ import pytest import rclpy +from rclpy.exceptions import ROSInterruptException from rclpy.executors import SingleThreadedExecutor # Hz @@ -129,7 +130,7 @@ def test_shutdown_wakes_rate(): rate = node.create_rate(0.0000001) - _thread = threading.Thread(target=sleep_check_exception, args=rate, daemon=True) + _thread = threading.Thread(target=sleep_check_exception, args=(rate,), daemon=True) _thread.start() executor.shutdown() node.destroy_node()