From c83aba0fd1e69e539bdb8bbcc99bdfcd95e57e0a Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Tue, 4 Jun 2024 20:47:06 +0200 Subject: [PATCH] Initialize the Sentry SDK within an import executor job to not block event loop (#118830) --- homeassistant/components/sentry/__init__.py | 46 +++++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/sentry/__init__.py b/homeassistant/components/sentry/__init__.py index dcbcc59a749..8c042621db6 100644 --- a/homeassistant/components/sentry/__init__.py +++ b/homeassistant/components/sentry/__init__.py @@ -21,6 +21,7 @@ from homeassistant.helpers import config_validation as cv, entity_platform, inst from homeassistant.helpers.event import async_call_later from homeassistant.helpers.system_info import async_get_system_info from homeassistant.loader import Integration, async_get_custom_components +from homeassistant.setup import SetupPhases, async_pause_setup from .const import ( CONF_DSN, @@ -41,7 +42,6 @@ from .const import ( CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False) - LOGGER_INFO_REGEX = re.compile(r"^(\w+)\.?(\w+)?\.?(\w+)?\.?(\w+)?(?:\..*)?$") @@ -81,23 +81,33 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ), } - sentry_sdk.init( - dsn=entry.data[CONF_DSN], - environment=entry.options.get(CONF_ENVIRONMENT), - integrations=[sentry_logging, AioHttpIntegration(), SqlalchemyIntegration()], - release=current_version, - before_send=lambda event, hint: process_before_send( - hass, - entry.options, - channel, - huuid, - system_info, - custom_components, - event, - hint, - ), - **tracing, - ) + with async_pause_setup(hass, SetupPhases.WAIT_IMPORT_PACKAGES): + # sentry_sdk.init imports modules based on the selected integrations + def _init_sdk(): + """Initialize the Sentry SDK.""" + sentry_sdk.init( + dsn=entry.data[CONF_DSN], + environment=entry.options.get(CONF_ENVIRONMENT), + integrations=[ + sentry_logging, + AioHttpIntegration(), + SqlalchemyIntegration(), + ], + release=current_version, + before_send=lambda event, hint: process_before_send( + hass, + entry.options, + channel, + huuid, + system_info, + custom_components, + event, + hint, + ), + **tracing, + ) + + await hass.async_add_import_executor_job(_init_sdk) async def update_system_info(now): nonlocal system_info