Fix: user_id validation causes read timeout on slow Jellyfin
The previous fix called client.users() unconditionally to validate the user_id, adding an extra HTTP round-trip before the build. On a slow Jellyfin connection this burned through the 10s timeout before the actual libraries() call. Restructured to try libraries(user_id) directly first. Only when that fails does the worker resolve the username via the users API. So: - Correctly configured user_id (internal hash): zero extra round-trips. - Username like 'admin': libraries() fails → users() resolves it → retry. 283 backend tests pass; ruff clean.
This commit is contained in:
@@ -114,27 +114,29 @@ def _resolve_jellyfin(service_id: str) -> tuple[Any, str]:
|
|||||||
raise RuntimeError("No Jellyfin users found and no user_id configured on the service")
|
raise RuntimeError("No Jellyfin users found and no user_id configured on the service")
|
||||||
user_id = users[0]["Id"]
|
user_id = users[0]["Id"]
|
||||||
else:
|
else:
|
||||||
# The config field accepts either a Jellyfin internal user ID (a long
|
# Try the configured user_id directly. It might be the internal
|
||||||
# hash) or a username (e.g. "admin"). Validate against the users API:
|
# Jellyfin Id (a long hash) — in that case libraries() succeeds
|
||||||
# if the configured value doesn't match any user's Id, try matching by
|
# without an extra users() round-trip. Only if it fails do we
|
||||||
# Name, then fall back to the first user.
|
# resolve it via the users API (the config field accepts usernames
|
||||||
users = client.users()
|
# like 'admin' too, but Jellyfin's API rejects them on /Users/<id>).
|
||||||
valid_ids = {str(u.get("Id", "")) for u in users}
|
try:
|
||||||
if user_id not in valid_ids:
|
client.libraries(user_id)
|
||||||
|
except Exception:
|
||||||
|
users = client.users()
|
||||||
match = next((u for u in users if str(u.get("Name", "")) == user_id), None)
|
match = next((u for u in users if str(u.get("Name", "")) == user_id), None)
|
||||||
if match:
|
if match:
|
||||||
user_id = match["Id"]
|
resolved = match["Id"]
|
||||||
logger.info(
|
logger.info(
|
||||||
"Resolved username '%s' to Jellyfin Id '%s'",
|
"Resolved username '%s' to Jellyfin Id '%s'",
|
||||||
service.get("config", {}).get("user_id"),
|
|
||||||
user_id,
|
user_id,
|
||||||
|
resolved,
|
||||||
)
|
)
|
||||||
|
user_id = resolved
|
||||||
elif users:
|
elif users:
|
||||||
user_id = users[0]["Id"]
|
user_id = users[0]["Id"]
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"user_id '%s' not found; falling back to first user '%s'",
|
"user_id '%s' not found; falling back to first user",
|
||||||
service.get("config", {}).get("user_id"),
|
service.get("config", {}).get("user_id"),
|
||||||
user_id,
|
|
||||||
)
|
)
|
||||||
return client, user_id
|
return client, user_id
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user