This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit cdedf8ada8668dd08f6e60aca57ce9f63722d094
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Mon Aug 3 21:25:53 2026 +0800

    sched/hrtimer: Fix stale remaining time after cancel at core layer
    
    hrtimer_gettime() can return a stale remaining time for a cancelled
    timer, because the expired time is left unchanged after the timer is
    disarmed.  Reset expired to hrtimer_last_expired() (a past time) in
    hrtimer_cancel(), so that hrtimer_gettime() returns zero for any
    cancelled timer regardless of the caller.
    
    Assisted-by: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 sched/hrtimer/hrtimer_cancel.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/sched/hrtimer/hrtimer_cancel.c b/sched/hrtimer/hrtimer_cancel.c
index 1d5747365dd..e19cc6d869c 100644
--- a/sched/hrtimer/hrtimer_cancel.c
+++ b/sched/hrtimer/hrtimer_cancel.c
@@ -87,6 +87,12 @@ int hrtimer_cancel(FAR hrtimer_t *hrtimer)
 
   ret = hrtimer_cancel_running(hrtimer);
 
+  /* Update the expired time to ensure the cancelled timer will return 0
+   * in hrtimer_gettime.
+   */
+
+  hrtimer->expired = hrtimer_last_expired();
+
   if (hrtimer_is_pending(hrtimer))
     {
       uint64_t prev_expired = hrtimer_next_expired();

Reply via email to