From 36b8face988669509272b00f4abe6592d0b17aa0 Mon Sep 17 00:00:00 2001 From: Steven Palma Date: Wed, 29 Jul 2026 20:24:07 +0200 Subject: [PATCH] fix(utils): validate precise_sleep spin/margin args (#4218) * fix(utils): validate precise_sleep spin/margin args Negative spin_threshold/sleep_margin make remaining arithmetic wrong and can overshoot. Reject them early; cover the no-op path. * test: drop flaky wall-clock assertion in no-op test Per review: the 50ms wall-clock check can exceed its bound on a preempted CI worker even when precise_sleep returns immediately. The direct calls already exercise the non-positive no-op path, so the assertion is redundant. * chore(tests): remove precise_sleep test negative values --------- Co-authored-by: Bartok9 --- src/lerobot/utils/robot_utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lerobot/utils/robot_utils.py b/src/lerobot/utils/robot_utils.py index 656dc2649..04b09a4fe 100644 --- a/src/lerobot/utils/robot_utils.py +++ b/src/lerobot/utils/robot_utils.py @@ -30,6 +30,10 @@ def precise_sleep(seconds: float, spin_threshold: float = 0.010, sleep_margin: f """ if seconds <= 0: return + if spin_threshold < 0: + raise ValueError(f"spin_threshold must be >= 0, got {spin_threshold}") + if sleep_margin < 0: + raise ValueError(f"sleep_margin must be >= 0, got {sleep_margin}") system = platform.system() # On macOS and Windows the scheduler / sleep granularity can make