Skip to content

Commit 478b82b

Browse files
committed
Ensure component data is stale after receiving a timer event
Because of the nature of async, it might happen that data is slightly delayed, so a timer event gets triggered, but component data arrives before the timer event arrives, and in such cases, we end up going into a bad state. This issue is resolved in this PR, by checking that the component data is actually late, every time a timer triggers. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 82d5834 commit 478b82b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/frequenz/sdk/actor/power_distributing/_battery_status.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,25 @@ async def _run(
339339
self._handle_status_set_power_result(selected.value)
340340

341341
elif selected_from(selected, battery_timer):
342+
if (
343+
datetime.now(tz=timezone.utc)
344+
- self._battery.last_msg_timestamp
345+
) < timedelta(seconds=self._max_data_age):
346+
# This means that we have received data from the battery
347+
# since the timer triggered, but the timer event arrived
348+
# late, so we can ignore it.
349+
continue
342350
self._handle_status_battery_timer()
343351

344352
elif selected_from(selected, inverter_timer):
353+
if (
354+
datetime.now(tz=timezone.utc)
355+
- self._inverter.last_msg_timestamp
356+
) < timedelta(seconds=self._max_data_age):
357+
# This means that we have received data from the inverter
358+
# since the timer triggered, but the timer event arrived
359+
# late, so we can ignore it.
360+
continue
345361
self._handle_status_inverter_timer()
346362

347363
else:

0 commit comments

Comments
 (0)