sys/ztimer: fix re-scheduling of timers#20924
Merged
maribu merged 1 commit intoRIOT-OS:masterfrom Oct 19, 2024
Merged
Conversation
If the timer at the head of a ztimer clock's timer list is re-scheduled (ztimer_set() called on an already set timer) and the timer is no longer at the head after being re-scheduled, clock-ops->set() is never called from inside ztimer_set(), and the underlying timer is left with an ISR scheduled to expire at the timer's old time. The intended behavior is that the clock's lower level timer should always be set to expire at the time of the clocks head timer. This patch changes ztimer_set() to call _ztimer_update(), which sets the lower level timer according to the current list of timers, rather than setting the timer directly inside of ztimer_set().
This was referenced Oct 18, 2024
maribu
approved these changes
Oct 18, 2024
Contributor
|
That fix looks pretty straightforward! Please also add a test case that reproduced the bug. |
Member
|
Backport provided in #20928 |
maribu
added a commit
to maribu/RIOT
that referenced
this pull request
Dec 30, 2024
This reverts commit e3d0068. With RIOT-OS#20924 merged, this should no longer be needed.
Member
Mostly yes. However, the timeout in there can also trigger early even when ztimer does not trigger early: The GNRC SOCK implementation uses an mbox to wait for a network message to be received. However, mbox has no timeout. Instead, ztimer will put a message to the mbox when it is triggered. If the mbox message related to a received network message is put into the mbox just before the timeout message, the next one to fetch something from the mbox will get the stale timeout message from the mbox directly. |
Member
maribu
added a commit
to maribu/RIOT
that referenced
this pull request
Jan 10, 2025
This reverts commit e3d0068, which added a work around for two bugs: - ztimer triggering too early (fixed in RIOT-OS#20924) - gnrc_sock_recv() returning when an old "timeout" message is still in the message queue (fixed in RIOT-OS#21113) With those bugs fixed, the work around should not longer be needed.
dprigoshij
pushed a commit
to dprigoshij/RIOT
that referenced
this pull request
Mar 24, 2025
This reverts commit e3d0068, which added a work around for two bugs: - ztimer triggering too early (fixed in RIOT-OS#20924) - gnrc_sock_recv() returning when an old "timeout" message is still in the message queue (fixed in RIOT-OS#21113) With those bugs fixed, the work around should not longer be needed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contribution description
If the timer at the head of a ztimer clock's timer list is re-scheduled (
ztimer_set()called on an already set timer) and the timer is no longer at the head after being re-scheduled,clock-ops->set()is never called from insideztimer_set(), and the underlying timer is left with an ISR scheduled to expire at the timer's old time. The intended behavior is that the clock's lower level timer should always be set to expire at the time of the clocks head timer.This patch changes
ztimer_set()to call_ztimer_update(), which sets the lower level timer according to the current list of timers, rather than setting the timer directly inside ofztimer_set().This is a fix we might consider back porting. As far as I can tell this bug has always existed in
ztimer.Testing procedure
make -C examples/hello-world/ all termTestbench patch
Sample of testbench's expected (good) behavior
Sample of testbench's behavior without this fix
Issues/PRs references
This bug is possible the cause of the early timeout worked around in this #19965. Perhaps that workaround could be removed?