Fix: media worker crashes silently before writing error state

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.
This commit is contained in:
Developer
2026-07-06 15:36:07 +00:00
parent 787f46700f
commit a3888026ab
@@ -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,