From 0454189165b5f6149c9107f7e76d951cf3ef20bb Mon Sep 17 00:00:00 2001 From: erikpendragon Date: Tue, 21 Apr 2026 10:25:20 -0400 Subject: [PATCH] Fix Jellyfin WebSocket blocking HA startup for 300s MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JellyfinWebSocket._listen() was spawned via hass.async_create_task(), which registers the coroutine on hass._tasks — the set Core awaits during the startup phase. Since _listen() is an infinite WebSocket read loop, it never completes, so Home Assistant always hits the startup-wait timeout (300s) before continuing. The result is a ~5 minute delay on every boot, with a "Something is blocking Home Assistant from wrapping up the start up phase" warning naming jellyfin.py:105. The same pattern is used in _schedule_reconnect() for the reconnect callback, which would have the same effect if it fires during startup. Switch both call sites to hass.async_create_background_task(), which is the correct HA API for long-running fire-and-forget loops: background tasks are tracked for clean shutdown but excluded from the startup wait set. Also pin min_ha_version to 2023.5.0 — the release that introduced async_create_background_task — so HACS / Supervisor refuse installation on older HA versions with a clear message rather than failing at connect time with AttributeError. Tested on HA 2026.4.3: STARTING->RUNNING dropped from ~300s to ~15s, no more blocking warning, Jellyfin sensor still populates and reconnect still works. --- custom_components/mediarr/manifest.json | 3 ++- custom_components/mediarr/server/jellyfin.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/custom_components/mediarr/manifest.json b/custom_components/mediarr/manifest.json index 94db5ae..f049612 100644 --- a/custom_components/mediarr/manifest.json +++ b/custom_components/mediarr/manifest.json @@ -10,5 +10,6 @@ "aiofiles>=0.8.0" ], "iot_class": "local_polling", - "version": "0.1.0" + "version": "0.1.0", + "min_ha_version": "2023.5.0" } diff --git a/custom_components/mediarr/server/jellyfin.py b/custom_components/mediarr/server/jellyfin.py index c10b554..ba31281 100644 --- a/custom_components/mediarr/server/jellyfin.py +++ b/custom_components/mediarr/server/jellyfin.py @@ -77,7 +77,7 @@ async def connect(self): _LOGGER.info("Connected to Jellyfin WebSocket") # Start listening for messages - self._hass.async_create_task(self._listen()) + self._hass.async_create_background_task(self._listen(), name="jellyfin_mediarr_websocket") except Exception as err: _LOGGER.error("WebSocket connection failed: %s", err) @@ -95,7 +95,7 @@ async def _schedule_reconnect(self): self._connection_retry_count += 1 _LOGGER.info(f"Scheduling reconnection attempt in {delay} seconds...") self._scheduled_retry = self._hass.loop.call_later( - delay, lambda: self._hass.async_create_task(self.connect()) + delay, lambda: self._hass.async_create_background_task(self.connect(), name="jellyfin_mediarr_reconnect") ) async def _listen(self):