Skip to content

Commit 224519c

Browse files
authored
Fix memory leak in the timer (#334)
We were creating two futures, but only awaiting until the one of them finished and not canceling the second. Because `reset()` was never called, we were just creating more and more futures that would never complete. This change fixes this by canceling the second task immediately after the first one is complete.
2 parents 13cf174 + b0eabec commit 224519c

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

RELEASE_NOTES.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
# Frequenz channels Release Notes
22

3+
## Summary
4+
5+
<!-- Here goes a general summary of what this release is about -->
6+
37
## Upgrading
48

5-
- `FileWatcher`: The file polling mechanism is now forced by default. This provides reliable and consistent file monitoring on network file systems (e.g., CIFS). However, it may have a performance impact on local file systems or when monitoring a large number of files.
6-
- To disable file polling, set the `force_polling` parameter to `False`.
7-
- The `polling_interval` parameter defines the interval for polling changes. This is relevant only when polling is enabled and defaults to 1 second.
9+
<!-- Here goes notes on how to upgrade from previous versions, including deprecations and what they should be replaced with -->
810

911
## New Features
1012

11-
- `Timer.reset()` now supports setting the interval and will restart the timer with the new interval.
13+
<!-- Here goes the main new features and examples or instructions on how to use them -->
1214

1315
## Bug Fixes
1416

15-
- `FileWatcher`:
16-
- Fixed `ready()` method to return False when an error occurs. Before this fix, `select()` (and other code using `ready()`) never detected the `FileWatcher` was stopped and the `select()` loop was continuously waking up to inform the receiver was ready.
17-
- Reports file events correctly on network file systems like CIFS.
18-
19-
- `Timer.stop()` and `Timer.reset()` now immediately stop the timer if it is running. Before this fix, the timer would continue to run until the next interval.
17+
- Fixed a memory leak in the timer.

src/frequenz/channels/timer.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -682,14 +682,20 @@ async def ready(self) -> bool: # noqa: DOC502
682682
# could be reset while we are sleeping, in which case we need to recalculate
683683
# the time to the next tick and try again.
684684
while time_to_next_tick > 0:
685-
await next(
686-
asyncio.as_completed(
687-
[
688-
asyncio.sleep(time_to_next_tick / 1_000_000),
689-
self._reset_event.wait(),
690-
]
691-
)
685+
_, pending = await asyncio.wait(
686+
[
687+
asyncio.create_task(asyncio.sleep(time_to_next_tick / 1_000_000)),
688+
asyncio.create_task(self._reset_event.wait()),
689+
],
690+
return_when=asyncio.FIRST_COMPLETED,
692691
)
692+
for task in pending:
693+
task.cancel()
694+
try:
695+
await task
696+
except asyncio.CancelledError:
697+
pass
698+
693699
self._reset_event.clear()
694700
now = self._now()
695701
time_to_next_tick = self._next_tick_time - now

0 commit comments

Comments
 (0)