fix(logs): rule on every swallowed exception in the backend
114 exception handlers neither logged nor re-raised. The defect was never that
they are silent — plenty of them should be — it is that their silence said
nothing, so nobody could tell a decision from an oversight. Each one has now been
read. 44 got a line; the other 70 carry `# silent-ok: <why>`.
What the silent ones had been hiding, in the cases that got a line:
- A failed ffprobe sent every HDR file down the 8-bit pipeline, so playback
degraded to a software transcode with nothing saying why; a failed audio probe
collapsed a multi-audio file to one track and the player showed no language
picker; a failed first-PTS probe left subtitles seconds out of sync.
- A subtitle sidecar that existed but would not parse made a PARTIAL track read as
complete, permanently — the viewer loses every cue past the cached window.
- An unreadable crop/trim/cut-list in an edit spec was dropped, so the clip came
out uncropped, untrimmed, or shorter than the one the user built.
- A dropped `max_height` downloaded at best quality against an explicit cap.
- A failed videos.list enrich made every candidate undroppable-from-judgement and
therefore dropped: a working search rendered as "no results".
- An unusable password hash answered as "wrong password", locking the owner out
and telling them they mistyped; a rotated TOKEN_ENCRYPTION_KEY turned every
stored token into "not connected".
- A stored admin setting that would not coerce fell back to the env default while
the settings page went on showing the saved value.
- A one-off InnerTube config failure pinned the whole process to the hard-coded
fallback key, and searches return nothing when that goes stale.
- The HLS sweep reported 0 swept — like a clean run — when it could not list its
own root, leaving every crashed session's segments on disk forever.
Four of them are counted rather than logged per item (dropped cues, dropped cut
list segments, unreclaimed GC files) and reported once per call, because the
alternative is one line per cue over a whole subtitle track.
ruff gains S110 and BLE001. Every blind `except Exception` that remains carries
`# noqa: BLE001 — <why>`, which is the point: catching everything is sometimes
right, and always worth a sentence. plex/sync.py's three best-effort backfill
catches now keep the traceback — a blind catch over a batch that fetches AND
writes could be Plex, the database or our own bug, and `%s` of the exception told
them apart badly. main.py's boot-time log-level read likewise.
logs.event's `code` is positional-only now: `event("x", code=...)` was a TypeError,
and `code` is exactly the word a caller reaches for when the thing that failed has
one of its own.
tests/test_swallowed_exceptions.py is the wider rule ruff cannot see: a handler
that neither logs nor re-raises must say why. Both cases mutation-checked — a
marker removed and a log line removed each fail it, naming the handler.
This commit is contained in:
+76
-10
@@ -230,7 +230,15 @@ def _probe_video_kind(src: Path) -> str:
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
except (subprocess.SubprocessError, OSError):
|
||||
except (subprocess.SubprocessError, OSError) as exc:
|
||||
# The safe kind is a real answer, but it is the answer for an 8-bit SDR source — a blind
|
||||
# ffprobe sends every HDR file down it, so playback quietly degrades to a software
|
||||
# transcode with nothing anywhere saying why. The path stays out of the line: it is the
|
||||
# title of whatever is playing.
|
||||
log.info(
|
||||
"video-kind probe failed; assuming the HW-safe pipeline",
|
||||
extra=logs.event("plex.stream.probe_kind_failed", reason=type(exc).__name__),
|
||||
)
|
||||
return _VK_HWSAFE
|
||||
pix_fmt = bits = transfer = ""
|
||||
for line in out.stdout.splitlines():
|
||||
@@ -259,7 +267,14 @@ def _opencl_present() -> bool:
|
||||
software libx264. Its own function so it's monkeypatchable in tests, mirroring _render_node_present."""
|
||||
try:
|
||||
return any(_OPENCL_ICD_DIR.glob("*.icd"))
|
||||
except OSError:
|
||||
except OSError as exc:
|
||||
# A MISSING directory does not come through here — `glob` simply yields nothing for one.
|
||||
# This is a directory we cannot read, and the answer it forces (no OpenCL) costs every HDR
|
||||
# source the hardware tonemap path.
|
||||
log.info(
|
||||
"the OpenCL ICD directory could not be read; treating OpenCL as absent",
|
||||
extra=logs.event("plex.stream.opencl_probe_failed", reason=type(exc).__name__),
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
@@ -445,6 +460,8 @@ def _sw_child_preexec() -> None:
|
||||
playback."""
|
||||
try:
|
||||
os.nice(_SW_TRANSCODE_NICE)
|
||||
# silent-ok: this runs in the forked child, between fork and exec. Logging there can deadlock
|
||||
# on a lock some other thread held at fork time, and the child is about to become ffmpeg.
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
@@ -471,7 +488,14 @@ def _probe_audio_langs(src: Path) -> list[str]:
|
||||
timeout=10,
|
||||
)
|
||||
return [ln.strip() for ln in out.stdout.splitlines()]
|
||||
except (subprocess.SubprocessError, OSError):
|
||||
except (subprocess.SubprocessError, OSError) as exc:
|
||||
# An empty list IS the single-audio path, so a failed probe silently collapses a file with
|
||||
# several audio tracks down to one — and the viewer gets a player with no language picker
|
||||
# rather than an error.
|
||||
log.warning(
|
||||
"audio-track probe failed; falling back to a single-audio session",
|
||||
extra=logs.event("plex.stream.probe_audio_failed", reason=type(exc).__name__),
|
||||
)
|
||||
return []
|
||||
|
||||
|
||||
@@ -580,8 +604,14 @@ def _probe_first_pts(seg: Path) -> float | None:
|
||||
tok = line.strip().strip(",")
|
||||
if tok:
|
||||
return float(tok)
|
||||
except (subprocess.SubprocessError, ValueError, OSError):
|
||||
pass
|
||||
except (subprocess.SubprocessError, ValueError, OSError) as exc:
|
||||
# None means "keep the offset the client asked for", and the whole point of this probe is
|
||||
# that the two differ: without it the subtitle track is shifted by whatever the container
|
||||
# started its timestamps at. Seconds of drift, and nothing on screen calls it a failure.
|
||||
log.warning(
|
||||
"first-PTS probe failed; the requested offset is used as-is",
|
||||
extra=logs.event("plex.stream.first_pts_probe_failed", reason=type(exc).__name__),
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@@ -590,10 +620,18 @@ def _kill(s: HlsSession) -> None:
|
||||
s.proc.terminate()
|
||||
try:
|
||||
s.proc.wait(timeout=3)
|
||||
# silent-ok: the escalation IS the handling. A transcode that ignores TERM for three
|
||||
# seconds gets KILL, which is exactly what this branch does.
|
||||
except subprocess.TimeoutExpired:
|
||||
s.proc.kill()
|
||||
except Exception:
|
||||
pass
|
||||
except Exception as exc: # noqa: BLE001 — teardown finishes whatever the process does
|
||||
# Not nothing: what survives this is an ffmpeg nobody owns any more, holding a VAAPI
|
||||
# handle and a core, and the transcode cap that is supposed to bound them counts rows in
|
||||
# `_sessions` rather than processes. One line is the only way that ever surfaces.
|
||||
log.info(
|
||||
"could not stop the transcode process",
|
||||
extra=logs.event("plex.stream.kill_failed", key=s.key, reason=type(exc).__name__),
|
||||
)
|
||||
shutil.rmtree(s.dir, ignore_errors=True)
|
||||
|
||||
|
||||
@@ -865,7 +903,20 @@ def artifact_may_arrive(s: "HlsSession", filename: str) -> bool:
|
||||
return True
|
||||
try:
|
||||
return (s.dir / segment_name(seq - 1, variant)).exists()
|
||||
except OSError:
|
||||
except OSError as exc:
|
||||
# `exists()` answers False for a missing file without raising, so this is the session
|
||||
# directory itself refusing — a full or unmounted disk. Every segment then 404s, which the
|
||||
# player shows as a stall. Per-segment, but the sink groups by message template, so a
|
||||
# broken session is one grouped line rather than one per segment.
|
||||
log.warning(
|
||||
"a session segment could not be checked",
|
||||
extra=logs.event(
|
||||
"plex.stream.segment_probe_failed",
|
||||
key=s.key,
|
||||
seq=seq,
|
||||
reason=type(exc).__name__,
|
||||
),
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
@@ -882,12 +933,16 @@ def wait_for(path: Path, timeout: float = 20.0, proc: subprocess.Popen | None =
|
||||
try:
|
||||
if path.exists() and path.stat().st_size > 0:
|
||||
return True
|
||||
# silent-ok: this is a 150 ms poll — a line here is a line every 150 ms for the whole
|
||||
# timeout. The final check below decides, and the 404 the caller then returns is honest.
|
||||
except OSError:
|
||||
pass
|
||||
if proc is not None and proc.poll() is not None:
|
||||
# the producer exited — no more segments are coming; one last check then give up.
|
||||
try:
|
||||
return path.exists() and path.stat().st_size > 0
|
||||
# silent-ok: one last look after the producer exited. False here becomes a 404 the
|
||||
# player recovers from, which is the honest answer either way.
|
||||
except OSError:
|
||||
return False
|
||||
time.sleep(0.15)
|
||||
@@ -920,7 +975,16 @@ def sweep_orphans(older_than: float | None = None) -> int:
|
||||
dropped = 0
|
||||
try:
|
||||
entries = list(_HLS_ROOT.iterdir())
|
||||
except OSError: # the root doesn't exist yet — nothing to sweep
|
||||
# silent-ok: nothing has been transcoded yet, so there is nothing to sweep.
|
||||
except FileNotFoundError:
|
||||
return 0
|
||||
except OSError as exc:
|
||||
# Any OTHER reason the root will not list is not "nothing to sweep": the sweep reports 0
|
||||
# like a clean run while every crashed session's segments stay on disk forever.
|
||||
log.warning(
|
||||
"the HLS root could not be listed; no orphans were swept",
|
||||
extra=logs.event("plex.stream.sweep_root_unreadable", reason=type(exc).__name__),
|
||||
)
|
||||
return 0
|
||||
for p in entries:
|
||||
if not _SESSION_DIR_RE.match(p.name):
|
||||
@@ -930,7 +994,9 @@ def sweep_orphans(older_than: float | None = None) -> int:
|
||||
try:
|
||||
if p.stat().st_mtime >= cutoff:
|
||||
continue # this run made (or reused) it — never ours to delete
|
||||
except OSError: # vanished under us (a concurrent restart of that session)
|
||||
# silent-ok: vanished under us — a concurrent restart of that session removed it, which
|
||||
# is the outcome this sweep wanted anyway.
|
||||
except OSError:
|
||||
continue
|
||||
with _lock:
|
||||
if any(s.dir == p for s in _sessions.values()):
|
||||
|
||||
Reference in New Issue
Block a user