Skip to content

fix: detect Jellyfin auth panic recovered internally as success - #67

Merged
jay739 merged 1 commit into
mainfrom
fix/login-treats-recovered-auth-panic-as-success
Jul 16, 2026
Merged

jay739 merged 1 commit into
mainfrom
fix/login-treats-recovered-auth-panic-as-success

Conversation

@jay739

@jay739 jay739 commented Jul 16, 2026

Copy link
Copy Markdown
Owner

Summary

The actual root cause of tonight's whole investigation (#64, #65, #66 all shipped real fixes for real bugs, but none changed the observed panic-burst/500 behavior at all) — found via a real stack trace, not further log-timing inference.

The panic in Authenticate() (#62's unchecked type assertion) is recovered inside hrfee/mediabrowser itself, by defer mb.timeoutHandler() registered before the panic-prone line. This is wired up via mediabrowser.NewNamedTimeoutHandler("Jellyfin", ..., true) in main.go (the true = noFail). It logs "Failed to authenticate with Jellyfin @ ..." via the stdlib log package directly — not app.err/app.debug, which is exactly why this was so hard to trace across three prior PRs tonight. None of those PRs' recover() wrappers ever had anything to catch, because the panic never reaches them — it's already handled one level down.

Authenticate() then returns with a nil error and a zero-value User{}, since the panic happens before mb.Authenticated = true and before the real return user, nil execute. Every err != nil check downstream sees a clean success and proceeds with empty/garbage user data. This is why /token/login returned a plain 500 (from CreateToken failing on an empty userID) with zero panic trace anywhere in the logs — the "panic" already happened and was already handled, one function call earlier than I was looking.

Change

Fixes validateJellyfinCredentials (the actual admin login path) to check app.authJf.Authenticated after the call, not just err — that's the one reliable signal the library only sets on genuine success. Kept the existing defer/recover() as a backstop in case a future library version removes its own internal recovery.

Scope note (deliberately not expanded further tonight)

safeGetUsers/safeUserByID/etc. (#64/#65/#66) likely have latent versions of this same gap wherever they wrap a method with the same lazy-reauth pattern, but GetUsers/UserByID/UserByName/NewUser/DeleteUser proceed to their own real HTTP call after a swallowed auth failure, which should independently surface a genuine error from that call (Jellyfin being unreachable) rather than silently succeeding — unlike Authenticate() itself, which has nothing left to do after auth completes. Worth verifying/hardening as a follow-up, not expanding this PR.

Test plan

  • With Jellyfin down, attempt admin login — confirm a clean 503 "Jellyfin unreachable" instead of a 500
  • With Jellyfin up, confirm normal login still works
  • CI build/test passes

Root cause of tonight's whole investigation, finally traced with a real
stack trace instead of log-timing inference: the panic in Authenticate()
(unchecked type assertion, #62) is recovered INSIDE hrfee/mediabrowser
itself, by a defer mb.timeoutHandler() registered before the panic-prone
line, wired up via mediabrowser.NewNamedTimeoutHandler(..., noFail=true)
in main.go. It logs "Failed to authenticate with Jellyfin @ ..." via the
stdlib log package directly (not app.err/app.debug, which is why this
was so hard to trace across #64/#65/#66 -- none of my recover() wrappers
in those PRs ever had anything to catch, because the panic never
reaches them).

Authenticate() returns afterward with a NIL error and a ZERO-VALUE User{},
since the panic happens before mb.Authenticated = true and before the
real return statement execute. Every err != nil check downstream sees a
clean success and proceeds with garbage/empty user data -- this is why
/token/login returned a plain 500 (CreateToken failing on an empty
userID) with zero panic trace anywhere, and why #64/#65/#66 never
changed the observed behavior at all.

Fixes validateJellyfinCredentials (the actual login path) to check
app.authJf.Authenticated after the call, not just err -- that's the one
reliable signal the library sets only on genuine success. Kept the
existing defer/recover() as a backstop in case a future library version
removes its own internal recovery.

Scope note: #64/#65/#66's wrapper functions (safeGetUsers/safeUserByID/
etc.) likely still have latent versions of this same gap wherever they
wrap a method that internally lazy-reauthenticates, but GetUsers/
UserByID/UserByName/NewUser/DeleteUser proceed to their own real HTTP
call after a swallowed auth failure, which should independently surface
a real error from THAT call instead of silently succeeding -- unlike
Authenticate() itself, which has nothing left to do after auth. Left
as a follow-up to verify/harden rather than expanding this fix further
tonight.
@jay739
jay739 merged commit 75bf094 into main Jul 16, 2026
7 checks passed
@jay739
jay739 deleted the fix/login-treats-recovered-auth-panic-as-success branch July 16, 2026 06:41
jay739 added a commit that referenced this pull request Jul 16, 2026
…68)

Found live 2026-07-16 testing with real admin credentials against a
genuinely stopped Jellyfin container (not just slow/unreachable-but-
listening): Authenticate() returned a real, non-nil *url.Error (DNS
lookup failure for the Jellyfin hostname, since the container isn't on
the Docker network when stopped) -- a completely different code path
from #67's fix, which only handles the case where the library's
internal panic-recovery returns a nil error with a zero-value user.

Neither ErrUnauthorized nor ErrForbidden match a network error, so this
fell through to a generic 500 "Jellyfin error", which is what was
observed testing login with real credentials right after #67 deployed.

Detects the network-failure shape (*url.Error, what
net/http.Client.Do always wraps transport-level failures in) and
returns 503 "Jellyfin unreachable" instead, matching the message #67
already produces for the other failure mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

⌂ 换网址