From c597116f02aaa3ccb33d67d24fba60c2f2030684 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 29 May 2026 23:10:02 -0700 Subject: [PATCH] [hal] HALSIM_StepTiming: Avoid underflow (#8933) Don't advance time if the only active timer is in the past; just wake up any notifiers. --- hal/src/main/native/sim/MockHooks.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hal/src/main/native/sim/MockHooks.cpp b/hal/src/main/native/sim/MockHooks.cpp index 34600effb4..f94bfd8766 100644 --- a/hal/src/main/native/sim/MockHooks.cpp +++ b/hal/src/main/native/sim/MockHooks.cpp @@ -139,12 +139,20 @@ void HALSIM_StepTiming(uint64_t delta) { while (delta > 0) { uint64_t curTime = HAL_GetMonotonicTime(); uint64_t nextTimeout = HALSIM_GetNextNotifierTimeout(); - uint64_t step = (std::min)(delta, nextTimeout - curTime); + // If a notifier is already due, process it at the current simulated time + // instead of underflowing nextTimeout - curTime. + uint64_t step = + nextTimeout <= curTime ? 0 : (std::min)(delta, nextTimeout - curTime); StepTiming(step); delta -= step; WakeupWaitNotifiers(); + + // Guard against notifiers that keep rearming at or before the same time. + if (step == 0 && HALSIM_GetNextNotifierTimeout() <= curTime) { + break; + } } }