From a3888026ab6c193a004c1c2ec126f44b59fde780 Mon Sep 17 00:00:00 2001 From: Developer Date: Mon, 6 Jul 2026 15:36:07 +0000 Subject: [PATCH] Fix: media worker crashes silently before writing error state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The worker called _resolve_jellyfin() and client.libraries() OUTSIDE the try/except block. If Jellyfin was unreachable, the worker crashed with an unhandled exception and NO error state was written to the DB — the status stayed 'queued' forever with zero feedback. Moved _resolve_jellyfin + client.libraries INSIDE the try block, and created final_index + staging_index BEFORE the try so the except handler can write the error state. Now any failure (connection refused, timeout, missing config) is written to the DB as build_error and the frontend shows it inline. 283 backend tests pass; ruff clean. --- .../workers/media_index_worker.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/media_library_viewer_api/workers/media_index_worker.py b/backend/src/media_library_viewer_api/workers/media_index_worker.py index fff35c4..82411c5 100644 --- a/backend/src/media_library_viewer_api/workers/media_index_worker.py +++ b/backend/src/media_library_viewer_api/workers/media_index_worker.py @@ -121,9 +121,11 @@ def run_build(final_index_path: str | Path, staging_index_path: str | Path, serv settings = get_settings() configure_logging(settings.log_level) logger.info("Media index worker starting: %s", describe_settings(settings)) - client, user_id = _resolve_jellyfin(service_id) - libraries = client.libraries(user_id) + # Create the indexes BEFORE the try block so the except handler can write + # error state to the DB. If _resolve_jellyfin or client.libraries fails, + # the worker needs to record the error — otherwise the status stays + # "queued" forever with no feedback. final_index = MediaIndex(final_index_path) staging_index = MediaIndex(staging_index_path) pid = os.getpid() @@ -131,10 +133,13 @@ def run_build(final_index_path: str | Path, staging_index_path: str | Path, serv staging_path = Path(staging_index.db_path) staging_path.unlink(missing_ok=True) - logger.info("Media index worker pid=%s libraries=%s", pid, len(libraries)) - _start_state(final_index, pid, len(libraries)) try: + client, user_id = _resolve_jellyfin(service_id) + libraries = client.libraries(user_id) + logger.info("Media index worker pid=%s libraries=%s", pid, len(libraries)) + _start_state(final_index, pid, len(libraries)) + count = build_media_index( client, user_id,