home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
69.03k stars 28.28k forks source link

Refactor statistics to avoid creating tasks #116743

Closed bdraco closed 1 week ago

bdraco commented 2 weeks ago

Proposed change

Refactor statistics to avoid creating tasks. Everything here was scheduling timer handles or events and can avoid creating tasks. Having the tasks in play made this integration much harder to debug.

The only task we need to create is the one time task to init from the database

We have been seeing increased cpu usage with statistics since at least 2024.2.x https://github.com/home-assistant/core/issues/110433 and it seems to have gotten worse in 2024.5.x https://github.com/home-assistant/core/issues/116650 https://github.com/home-assistant/core/issues/116731 as there is potential for tasks to race

Type of change

Additional information

Checklist

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

To help with the load of incoming pull requests:

home-assistant[bot] commented 2 weeks ago

Hey there @thomdietrich, mind taking a look at this pull request as it has been labeled with an integration (statistics) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `statistics` can trigger bot actions by commenting: - `@home-assistant close` Closes the pull request. - `@home-assistant rename Awesome new title` Renames the pull request. - `@home-assistant reopen` Reopen the pull request. - `@home-assistant unassign statistics` Removes the current integration label and assignees on the pull request, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
bdraco commented 1 week ago

Tagged since a handful of users have confirmed this fixes the race between tasks that was cause the system to get slower and slower over time

ThomDietrich commented 1 week ago

@bdraco did you eventually figure out what caused this development?

bdraco commented 1 week ago

@bdraco did you eventually figure out what caused this development?

There were multiple tasks racing against each other, creating more tasks. By making everything callbacks the flow is synchronous so it can't happen anymore.

ThomDietrich commented 1 week ago

Okay, thanks for resolving this!!