fix(ui,plex): a dialog keeps the focus its child asked for, and a hopeless segment fails at once

Both found by UAT in a real browser, after the tests for them were already green — which is the
whole reason this repo settles focus and timing claims by running them.

- **The autoFocus fix worked, and then the dialog took the focus back.** React 18's StrictMode runs
  every effect mount→cleanup→mount in development: the cleanup handed focus back to the trigger, and
  the second mount, finding the focus outside the dialog, moved it to the first focusable — off the
  `autoFocus` field a beat after the browser had put it there. Chrome measured it: INPUT (commitMount)
  → BODY (cleanup) → ✕ (re-mount). The effect is idempotent now — it remembers what the dialog was
  focused on and restores THAT on a re-mount — and the close-time restore is conditional on the
  dialog still holding the focus, which it tracks as it changes rather than computing at cleanup,
  where the panel is already out of the document. A dialog no longer yanks focus back from wherever
  the user has since moved it.

- **Passing ffmpeg's handle to the segment wait only shortened the FINISHED case.** Measured against
  the running localdev API: a request for `seg_99999.ts` on a live session still took 20 195 ms,
  because that ffmpeg has not exited and the wait has no other reason to stop. ffmpeg writes segments
  in order, so a segment whose predecessor does not exist is not the next one and cannot arrive —
  and that is knowable at once. Same request now: 45 ms.

Verified on localdev against the real library (Westworld S01E06, remux, multi-audio): Mod+K opens the
command palette with the focus in its search field and typing reaches it; playback runs on the new
session-scoped HLS URL; a request naming a stale session id is a 404; and a viewer paused for longer
than the finished-session rule keeps their 2.4 GB session across two reaper passes — the progress
checkpoint touches it — and resumes cleanly.

vitest 1009, pytest 1127.
This commit is contained in:
2026-09-12 04:31:51 +02:00
parent 33fddacee2
commit 1f81684b6e
4 changed files with 84 additions and 12 deletions
+30
View File
@@ -758,6 +758,36 @@ def current_session(key: str) -> HlsSession | None:
return s
# A segment file, single-audio (`seg_12.ts`) or one rendition of a multi-audio session
# (`seg_12_0.ts`). ffmpeg writes them strictly in order, which is what makes `segment_may_arrive`
# exact rather than a heuristic.
_SEG_NAME_RE = re.compile(r"^seg_(\d+)(_\d+)?\.ts$")
def segment_may_arrive(directory: Path, filename: str) -> bool:
"""Whether waiting for `filename` can possibly end in a file.
ffmpeg produces segments sequentially, so a segment whose PREDECESSOR does not exist yet is not
the next one — it is somewhere beyond the generated region, and no amount of waiting will
conjure it. Saying so at once matters because the route that waits is a SYNC one: it holds a
thread off the ~40-slot pool, and a pooled DB connection with it, for the whole timeout. An
ordinary seek past the produced region was enough to park one for 20 s, and forty concurrent
requests for a name that will never exist parked the whole API (deep-review round 5).
Anything that is not a segment (a playlist, a WebVTT track) is left to the timeout: those are
rewritten continuously and have no ordering to read."""
m = _SEG_NAME_RE.match(filename)
if m is None:
return True
idx = int(m.group(1))
if idx == 0:
return True
try:
return (directory / f"seg_{idx - 1}{m.group(2) or ''}.ts").exists()
except OSError:
return False
def wait_for(path: Path, timeout: float = 20.0, proc: subprocess.Popen | None = None) -> bool:
"""Wait until a session file (playlist / segment) exists and is non-empty. Segments are
produced ~faster than realtime, so a segment just ahead of playback appears quickly; a segment