Repository navigation
Fix timers ignoring cancellation - #625
Merged
Merged
Conversation
After waking up, `ready()` cancels the task it is no longer waiting for and awaits it, catching its `CancelledError`. If the task running `ready()` is itself cancelled while it awaits there, the `except` clause catches that cancellation too. After a reset, `ready()` then goes back to sleeping until the next tick, so a `select()` or service being stopped can hang for a whole timer interval. After a tick, the cancellation is lost and `ready()` returns as if nothing happened. Wait for the cancelled tasks with `asyncio.wait()` instead. It doesn't raise the tasks' exceptions, but it still raises `CancelledError` when the task running `ready()` is cancelled. The new test cancels `ready()` at several loop iterations after a reset and after a tick, so it doesn't depend on exactly when the cleanup happens. Without the fix, it fails for 3 and 4 iterations in both cases. The deprecated `Timer` is an alias of `MonotonicTimer`, so it's fixed too. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
llucax
marked this pull request as ready for review
November 24, 2026 16:58
llucax
requested review from
florian-wagner-frequenz
and removed request for
a team
November 24, 2026 16:58
llucax
enabled auto-merge
November 24, 2026 16:58
llucax
marked this pull request as draft
November 24, 2026 17:05
auto-merge was automatically disabled
November 24, 2026 17:05
Pull request was converted to draft
llucax
marked this pull request as ready for review
November 24, 2026 17:07
llucax
enabled auto-merge
November 24, 2026 17:07
Marenz
approved these changes
Nov 24, 2026
Marenz
left a comment
Contributor
There was a problem hiding this comment.
🤖 The fix is right: awaiting each cancelled helper and catching CancelledError also swallowed a cancellation of ready() itself, and asyncio.wait(pending) keeps the outer one.
Non-blocking, pre-existing: if ready() is cancelled while it waits in the first asyncio.wait(...), the two helper tasks (the sleep and _reset_event.wait()) are left pending: the sleep up to one interval, the event wait until the next reset. A try/finally that cancels both would clean that up. Happy to see it in a follow-up rather than here.
Contributor
Author
|
Maybe create a new issue? |
llucax
added a commit
that referenced
this pull request
Nov 25, 2026
Adds a summary and removes the template comments and the empty sections, so v1.13.1 can be tagged. The only change in this release is #625.
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.
Cancelling a task waiting in
MonotonicTimer.ready()right after the timer was reset or ticked could be ignored, because the cleanup of the taskready()stopped waiting for also caught the caller's ownCancelledError. The commit message has the details.Fixes #624.