-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix blocking I/O in the event loop when loading timezones #117721
Merged
Conversation
This file contains 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
20 tasks
bdraco
changed the title
Fix blocking I/O in the event loop to load timezones with aiozoneinfo
Add method to avoid blocking I/O in the event loop to when loading timezones
May 19, 2024
draft because all the current usage needs to be analyzed to see what its going to take to move to the new method as some of these calls are not in coros |
MartinHjelmare
changed the title
Add method to avoid blocking I/O in the event loop to when loading timezones
Add method to avoid blocking I/O in the event loop when loading timezones
May 19, 2024
bdraco
commented
May 20, 2024
bdraco
changed the title
Add method to avoid blocking I/O in the event loop when loading timezones
Fix blocking I/O in the event loop when loading timezones
May 20, 2024
5 tasks
bdraco
requested review from
dgomes,
gjohansson-ST,
a team,
fabaff,
Danielhiversen,
elupus,
allenporter,
azogue,
hunterjm,
bieniu,
farmio,
marvin-w and
frenck
as code owners
May 20, 2024 03:20
frenck
approved these changes
May 20, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @bdraco 👍
../Frenck
thanks |
bajansen
pushed a commit
to bajansen/homeassistant
that referenced
this pull request
May 20, 2024
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
blocking event loop
cla-signed
core
deprecation
Indicates a breaking change to happen in the future
small-pr
PRs with less than 30 lines.
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.
Breaking change
Constructing
ZoneInfo
objects may do blocking I/O to load the zone info from disk if the timezone passed is not in the cache.dt_util.async_get_time_zone
is now available to replacedt_util.get_time_zone
to fetch a time zone in the event loop which is async safe and will not do blocking I/O in the event loop.hass.config.set_time_zone
is deprecated and replaced withhass.config.async_set_time_zone
.hass.config.set_time_zone
will be removed in 2025.6. Setting the time zone only affects tests, as no integration should be calling this function in production.Examining
dt_util.DEFAULT_TIME_ZONE
directly is deprecated anddt_util.get_default_time_zone()
should be used instead.If your integration needs to construct
ZoneInfo
objects in the event loop, it is recommended to use theaiozoneinfo
library.Proposed change
aiozoneinfo is a simple wrapper that check if the ZoneInfo is in the cache, and if not it will load it in the executor since ZoneInfo will do blocking I/O in the loop if the zone info is not in the cache https://github.com/python/cpython/blob/a3db4e8f964499eabeddb19a6201f3d3b73d7443/Lib/zoneinfo/_zoneinfo.py#L73. A new library was created because integrations have the same problem and will likely want to use it in their PyPI package.
We have this problem in a few different places in the code. To avoid solving it multiple different ways,
dt_util.async_get_time_zone
is now available.Everything that used
dt_util.DEFAULT_TIME_ZONE
, manually constructed the zone info, or useddt_util.get_time_zone(entity.hass.config.time_zone)
has been updated todt_util.get_default_time_zone()
All instances in core that did blocking I/O to load the time zone data from disk have been fixed except for airnow which needs a more involved solution, see #117615
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: