From aff2f6d422d01d01e1314622a7ce2f5e88d70aad Mon Sep 17 00:00:00 2001 From: Bartok9 <259807879+Bartok9@users.noreply.github.com> Date: Wed, 12 Aug 2026 13:25:17 -0400 Subject: [PATCH] fix(ros2): reject non-positive waiters time_interval Prevent busy-spin when time_interval is 0 and reject negative poll gaps. --- src/rai_core/rai/communication/ros2/waiters.py | 2 ++ tests/communication/ros2/test_waiters.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/rai_core/rai/communication/ros2/waiters.py b/src/rai_core/rai/communication/ros2/waiters.py index 6effa109d..ec7530a1e 100644 --- a/src/rai_core/rai/communication/ros2/waiters.py +++ b/src/rai_core/rai/communication/ros2/waiters.py @@ -46,6 +46,8 @@ def wait_for_ros2_entities( if timeout < 0: raise ValueError("Timeout must be 0 (wait forever) or a positive value.") + if time_interval <= 0: + raise ValueError("time_interval must be positive.") start_time = time.time() while timeout == 0 or time.time() - start_time < timeout: diff --git a/tests/communication/ros2/test_waiters.py b/tests/communication/ros2/test_waiters.py index 06dc5957b..c00fdf2dc 100644 --- a/tests/communication/ros2/test_waiters.py +++ b/tests/communication/ros2/test_waiters.py @@ -168,3 +168,17 @@ def test_wait_for_ros2_entities_negative_timeout(): get_entities=lambda: [], timeout=-1, ) + + +def test_wait_for_entities_rejects_non_positive_time_interval(): + import pytest + from rai.communication.ros2 import waiters + + with pytest.raises(ValueError, match="time_interval"): + waiters.wait_for_ros2_entities( + ["/x"], lambda: [], time_interval=0, timeout=1.0 + ) + with pytest.raises(ValueError, match="time_interval"): + waiters.wait_for_ros2_entities( + ["/x"], lambda: [], time_interval=-0.5, timeout=1.0 + )