Skip to content

Commit

Permalink
Fix deadlock in holidays dynamic loading (#115385)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored and frenck committed Apr 12, 2024
1 parent d055f98 commit 4aca39b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
23 changes: 22 additions & 1 deletion homeassistant/components/holiday/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,36 @@

from __future__ import annotations

from functools import partial

from holidays import country_holidays

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.const import CONF_COUNTRY, Platform
from homeassistant.core import HomeAssistant
from homeassistant.setup import SetupPhases, async_pause_setup

from .const import CONF_PROVINCE

PLATFORMS: list[Platform] = [Platform.CALENDAR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Holiday from a config entry."""
country: str = entry.data[CONF_COUNTRY]
province: str | None = entry.data.get(CONF_PROVINCE)

# We only import here to ensure that that its not imported later
# in the event loop since the platforms will call country_holidays
# which loads python code from disk.
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
# import executor job is used here because multiple integrations use
# the holidays library and it is not thread safe to import it in parallel
# https://github.com/python/cpython/issues/83065
await hass.async_add_import_executor_job(
partial(country_holidays, country, subdiv=province)
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True
Expand Down
27 changes: 20 additions & 7 deletions homeassistant/components/workday/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryError
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.setup import SetupPhases, async_pause_setup

from .const import CONF_PROVINCE, DOMAIN, PLATFORMS

Expand All @@ -23,7 +24,11 @@ async def _async_validate_country_and_province(
if not country:
return
try:
await hass.async_add_executor_job(country_holidays, country)
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
# import executor job is used here because multiple integrations use
# the holidays library and it is not thread safe to import it in parallel
# https://github.com/python/cpython/issues/83065
await hass.async_add_import_executor_job(country_holidays, country)
except NotImplementedError as ex:
async_create_issue(
hass,
Expand All @@ -41,9 +46,13 @@ async def _async_validate_country_and_province(
if not province:
return
try:
await hass.async_add_executor_job(
partial(country_holidays, country, subdiv=province)
)
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
# import executor job is used here because multiple integrations use
# the holidays library and it is not thread safe to import it in parallel
# https://github.com/python/cpython/issues/83065
await hass.async_add_import_executor_job(
partial(country_holidays, country, subdiv=province)
)
except NotImplementedError as ex:
async_create_issue(
hass,
Expand Down Expand Up @@ -73,9 +82,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await _async_validate_country_and_province(hass, entry, country, province)

if country and CONF_LANGUAGE not in entry.options:
cls: HolidayBase = await hass.async_add_executor_job(
partial(country_holidays, country, subdiv=province)
)
with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES):
# import executor job is used here because multiple integrations use
# the holidays library and it is not thread safe to import it in parallel
# https://github.com/python/cpython/issues/83065
cls: HolidayBase = await hass.async_add_import_executor_job(
partial(country_holidays, country, subdiv=province)
)
default_language = cls.default_language
new_options = entry.options.copy()
new_options[CONF_LANGUAGE] = default_language
Expand Down

0 comments on commit 4aca39b

Please sign in to comment.