Skip to content

Fix timers ignoring cancellation - #625

Merged
llucax merged 1 commit into
frequenz-floss:v1.x.xfrom
llucax:fix-timer-swallowed-cancel
Nov 24, 2026
Merged

llucax merged 1 commit into
frequenz-floss:v1.x.xfrom
llucax:fix-timer-swallowed-cancel

Conversation

@llucax

@llucax llucax commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Cancelling a task waiting in MonotonicTimer.ready() right after the timer was reset or ticked could be ignored, because the cleanup of the task ready() stopped waiting for also caught the caller's own CancelledError. The commit message has the details.

Fixes #624.

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 llucax added this to the v1.13.1 milestone Nov 24, 2026
@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:utilities Affects the utility receivers (`Timer`, `Event`, `FileWatcher`) labels Nov 24, 2026
@llucax
llucax marked this pull request as ready for review November 24, 2026 16:58
@llucax
llucax requested a review from a team as a code owner November 24, 2026 16:58
@llucax
llucax requested review from florian-wagner-frequenz and removed request for a team November 24, 2026 16:58
@llucax
llucax enabled auto-merge November 24, 2026 16:58
@llucax llucax self-assigned this Nov 24, 2026
@llucax llucax removed this from the v1.13.1 milestone Nov 24, 2026
@llucax
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 llucax added this to the v1.13.1 milestone Nov 24, 2026
@llucax
llucax marked this pull request as ready for review November 24, 2026 17:07
@llucax
llucax enabled auto-merge November 24, 2026 17:07
@llucax llucax linked an issue Nov 24, 2026 that may be closed by this pull request

@Marenz Marenz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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.

@llucax
llucax merged commit b5e4684 into frequenz-floss:v1.x.x Nov 24, 2026
9 checks passed
@llucax

llucax commented Nov 24, 2026

Copy link
Copy Markdown
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:utilities Affects the utility receivers (`Timer`, `Event`, `FileWatcher`)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MonotonicTimer.ready() can swallow its own cancellation

2 participants