Skip to content

Commit 8177ba7

Browse files
authored
Ignore shutdown-time errors in the camera follower and joint demo (#46)
The camera follower and the joint demo could crash while shutting down, after a launch test had already passed: a TF lookup raised a time error once SIGINT invalidated the context, and the joint demo only caught KeyboardInterrupt. Both now ignore these errors, but only after the context has shut down; any other error, or the same one while ROS is running, still fails.
1 parent b4ffb6d commit 8177ba7

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎waybionic_bringup/scripts/camera_follower.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def main():
7676
if (rclpy.ok() or not str(error).startswith(
7777
"Unable to convert call argument '0' to Python object")):
7878
raise
79+
except TypeError as error:
80+
# tf2 can no longer read the lookup time once SIGINT has invalidated the context.
81+
if rclpy.ok() or not str(error).startswith('time must have sec and nanosec'):
82+
raise
7983
finally:
8084
node.destroy_node()
8185
rclpy.try_shutdown()

‎waybionic_bringup/scripts/joint_demo.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus, KeyValue
88
import rclpy
9+
from rclpy._rclpy_pybind11 import RCLError
10+
from rclpy.executors import ExternalShutdownException
911
from rclpy.node import Node
1012
from rclpy.qos import DurabilityPolicy, QoSProfile
1113
from rclpy.time import Time
@@ -252,8 +254,21 @@ def main():
252254
node = JointDemo()
253255
try:
254256
rclpy.spin(node)
255-
except KeyboardInterrupt:
257+
except (KeyboardInterrupt, ExternalShutdownException):
256258
pass
259+
except RCLError as error:
260+
# SIGINT can shut down the context before spin recreates its wait set.
261+
if rclpy.ok() or 'the given context is not valid' not in str(error):
262+
raise
263+
except RuntimeError as error:
264+
# Jazzy's binding error when a subscription's context is invalidated during SIGINT.
265+
if (rclpy.ok() or not str(error).startswith(
266+
"Unable to convert call argument '0' to Python object")):
267+
raise
268+
except TypeError as error:
269+
# tf2 can no longer read the lookup time once SIGINT has invalidated the context.
270+
if rclpy.ok() or not str(error).startswith('time must have sec and nanosec'):
271+
raise
257272
finally:
258273
node.destroy_node()
259274
rclpy.try_shutdown()

‎waybionic_teleop/test/test_sim_arm_drives_node.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
_camera_spec = importlib.util.spec_from_file_location('waybionic_camera_shutdown', CAMERA_PATH)
3030
camera_module = importlib.util.module_from_spec(_camera_spec)
3131
_camera_spec.loader.exec_module(camera_module)
32+
_demo_spec = importlib.util.spec_from_file_location(
33+
'waybionic_joint_demo_shutdown', CAMERA_PATH.with_name('joint_demo.py'))
34+
joint_demo_module = importlib.util.module_from_spec(_demo_spec)
35+
_demo_spec.loader.exec_module(joint_demo_module)
3236

3337
POSE = {'joint_1': 0.8, 'joint_2': -0.8, 'joint_3': 0.8,
3438
'joint_4': 0.8, 'joint_5': 0.25, 'tool_grip': 0.8}
@@ -43,6 +47,7 @@
4347
(sim_arm_drives_node, 'SimArmDrives'),
4448
(joy_udp_receiver, 'JoyUdpReceiver'),
4549
(camera_module, 'CameraFollower'),
50+
(joint_demo_module, 'JointDemo'),
4651
]
4752

4853

@@ -591,3 +596,27 @@ def test_binding_error_only_ignored_after_context_shutdown(module, class_name, m
591596
module.main()
592597
assert spin.call_count == 3
593598
assert factory.return_value.destroy_node.call_count == 3
599+
600+
601+
@pytest.mark.parametrize('module, class_name', [
602+
(camera_module, 'CameraFollower'), (joint_demo_module, 'JointDemo')])
603+
def test_tf_time_error_only_ignored_after_context_shutdown(module, class_name, monkeypatch):
604+
factory = MagicMock()
605+
error = TypeError('time must have sec and nanosec, or nanoseconds.')
606+
spin = MagicMock(side_effect=error)
607+
monkeypatch.setattr(module, class_name, factory)
608+
monkeypatch.setattr(module.rclpy, 'init', MagicMock())
609+
monkeypatch.setattr(module.rclpy, 'try_shutdown', MagicMock())
610+
monkeypatch.setattr(module.rclpy, 'spin', spin)
611+
monkeypatch.setattr(module.rclpy, 'ok', lambda: False)
612+
module.main()
613+
614+
spin.side_effect = TypeError('unrelated type error')
615+
with pytest.raises(TypeError, match='unrelated type error'):
616+
module.main()
617+
monkeypatch.setattr(module.rclpy, 'ok', lambda: True)
618+
spin.side_effect = error
619+
with pytest.raises(TypeError, match='time must have sec and nanosec'):
620+
module.main()
621+
assert spin.call_count == 3
622+
assert factory.return_value.destroy_node.call_count == 3

0 commit comments

Comments
 (0)