k_sleep() returns incorrect remaining time 12 days after booting #79863
Labels
area: Kernel
bug
The issue is a bug, or the PR is fixing a bug
priority: low
Low impact/importance bug
Describe the bug
k_sleep()
returns zero even if the thread is woken up byk_wakeup()
.The issue occurs exactly when the kernel tick count jumps over 2^32 (4,294,967,296).
In our product system,
CONFIG_SYS_CLOCK_TICKS_PER_SEC=4096
, it occurs 12 days (exactly 1,048,576 seconds) after booting.In the testing system,
CONFIG_SYS_CLOCK_TICKS_PER_SEC=50000
, it occurs 24 hours after booting.To Reproduce
k_wakeup()
every 10 ms.k_sleep()
with longer delay.k_sleep()
returns non-zero value.k_sleep()
returns zero even if the interrupt fires in time.Expected behavior
If the sensor is healthy and its interrupt fires periodically,
k_sleep()
returns non-zero value.Impact
I can workaround the issue to ignore
k_sleep()
's return value and to calculate elapsed time in the sensor thread.Logs and console output
Environment (please complete the following information):
Additional context
The cause of the problem is
z_tick_sleep()
implementation inkernel/sched.c
.https://github.com/zephyrproject-rtos/zephyr/blob/main/kernel/sched.c#L1173
uint32_t expected_wakeup_ticks
keeps the tick count in 32bit format that the thread wakes up without disturbing.But the expression below results incorrect, when:
expected_wakeup_ticks
has wrapped around (ex. 10)sys_clock_tick_get_32()
is nearly wrapping around (ex. 4,294,967,280)k_ticks_t
is defined as a 64bit integer typeI tried to handle integer wraparound properly, and it works :)
The text was updated successfully, but these errors were encountered: