mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
fix(streaming): surface the refusal truncation count, fall through failed foreign reads, and log malformed import-spec replies
This commit is contained in:
@@ -115,6 +115,7 @@ class LaunchFailedPayload(BaseModel):
|
||||
claimed_at: str
|
||||
detail: str
|
||||
refusals: list[ImportRefusalSchema] | None = None
|
||||
refusals_truncated: int = 0
|
||||
|
||||
|
||||
class LaunchPhasePayload(BaseModel):
|
||||
|
||||
@@ -239,7 +239,11 @@ async def hydrate_import_archive(
|
||||
)
|
||||
state_included = True
|
||||
|
||||
if not members and base is None:
|
||||
if not members:
|
||||
# No foreign material made it in (nothing was foreign, or reading it
|
||||
# failed): there is nothing to import. Let the caller fall through
|
||||
# to ordinary hydration for any native save, rather than uploading a
|
||||
# v2 archive whose `.import/` section is empty.
|
||||
return ImportHydration(None, False)
|
||||
|
||||
archive_bytes = await asyncio.to_thread(build_import_archive, rom.id, base, members)
|
||||
|
||||
@@ -6,6 +6,7 @@ progress behind it reach the player over the socket instead.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import dataclasses
|
||||
from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
@@ -109,10 +110,13 @@ async def run_launch(
|
||||
log.exception("launch failed, platform=%s", platform)
|
||||
await lifecycle.abort_claim(session_key, session, blank_card_id)
|
||||
refusals = (
|
||||
[ImportRefusalSchema(**vars(r)) for r in exc.refusals]
|
||||
[ImportRefusalSchema(**dataclasses.asdict(r)) for r in exc.refusals]
|
||||
if isinstance(exc, broker.ImportRefusedError)
|
||||
else None
|
||||
)
|
||||
refusals_truncated = (
|
||||
exc.truncated if isinstance(exc, broker.ImportRefusedError) else 0
|
||||
)
|
||||
await push_to_user(
|
||||
session.get("user_id"),
|
||||
"streaming:launch-failed",
|
||||
@@ -122,6 +126,7 @@ async def run_launch(
|
||||
claimed_at=session["claimed_at"],
|
||||
detail=_failure_detail(exc),
|
||||
refusals=refusals,
|
||||
refusals_truncated=refusals_truncated,
|
||||
).model_dump(),
|
||||
)
|
||||
return
|
||||
|
||||
@@ -70,10 +70,12 @@ def reset_import_spec_cache() -> None:
|
||||
def _parse_import_spec(body: dict[str, Any]) -> ImportSpec | None:
|
||||
raw_kinds = body.get("kinds")
|
||||
if not isinstance(raw_kinds, list):
|
||||
log.warning("import-spec response has no kinds list, treating as unknown")
|
||||
return None
|
||||
kinds = []
|
||||
for entry in raw_kinds:
|
||||
if not isinstance(entry, dict) or not isinstance(entry.get("kind"), str):
|
||||
log.warning("import-spec response has a malformed kind entry, %r", entry)
|
||||
return None
|
||||
max_members = entry.get("max_members")
|
||||
kinds.append(
|
||||
@@ -85,6 +87,10 @@ def _parse_import_spec(body: dict[str, Any]) -> ImportSpec | None:
|
||||
)
|
||||
state_channel = body.get("state_channel")
|
||||
if state_channel not in ("archive", "push", "none"):
|
||||
log.warning(
|
||||
"import-spec response has an unrecognized state_channel, %r",
|
||||
state_channel,
|
||||
)
|
||||
return None
|
||||
state_slot = body.get("state_slot")
|
||||
return ImportSpec(
|
||||
@@ -119,10 +125,14 @@ def import_spec(
|
||||
return None
|
||||
log.warning("import-spec check failed with HTTP %d, treating as unknown", code)
|
||||
return None
|
||||
except (urllib.error.URLError, OSError, ValueError):
|
||||
except (urllib.error.URLError, OSError):
|
||||
log.warning("import-spec check unreachable, treating as unknown")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
log.warning("import-spec response was not valid JSON, %s", exc)
|
||||
return None
|
||||
if not isinstance(resp, dict):
|
||||
log.warning("import-spec response was not a JSON object, treating as unknown")
|
||||
return None
|
||||
return _parse_import_spec(resp)
|
||||
|
||||
|
||||
@@ -5404,6 +5404,40 @@ def test_hydrate_import_archive_falls_back_to_the_newest_native_save_as_base(
|
||||
assert ".import/state/Game.00.pcsx2" in zf.namelist()
|
||||
|
||||
|
||||
def test_hydrate_import_archive_falls_through_when_the_only_foreign_read_fails(
|
||||
rom: Rom, admin_user: User
|
||||
):
|
||||
"""A native save plus a foreign state whose bytes are missing on disk:
|
||||
there is nothing foreign to carry, so this must not upload a base-only
|
||||
import archive. The caller falls back to ordinary save hydration."""
|
||||
save = db_save_handler.add_save(
|
||||
_save_for(rom, admin_user, "Game [retroarch a].saves.zip", "retroarch", "h1")
|
||||
)
|
||||
state = db_state_handler.add_state(
|
||||
_state_for(rom, admin_user, "Game.00.pcsx2", "pcsx2")
|
||||
)
|
||||
upload = MagicMock(return_value="rom-1.zip")
|
||||
with (
|
||||
patch(
|
||||
"handler.streaming.imports.fs_asset_handler.read_file",
|
||||
new=AsyncMock(side_effect=FileNotFoundError),
|
||||
),
|
||||
patch("handler.streaming.imports.webstation.upload_archive", upload),
|
||||
):
|
||||
result = asyncio.run(
|
||||
imports.hydrate_import_archive(
|
||||
admin_user.id,
|
||||
rom,
|
||||
_resolved(_clearing_webstation(rom)),
|
||||
save=save,
|
||||
save_is_foreign=False,
|
||||
state=state,
|
||||
)
|
||||
)
|
||||
assert result == imports.ImportHydration(None, False)
|
||||
upload.assert_not_called()
|
||||
|
||||
|
||||
def test_hydrate_import_archive_returns_none_when_nothing_is_foreign(
|
||||
rom: Rom, admin_user: User
|
||||
):
|
||||
@@ -5667,7 +5701,7 @@ def test_run_launch_pushes_refusals_when_the_broker_refuses_an_import(
|
||||
suggest_emulator=None,
|
||||
docs=None,
|
||||
)
|
||||
activate = MagicMock(side_effect=broker.ImportRefusedError([refusal], 0))
|
||||
activate = MagicMock(side_effect=broker.ImportRefusedError([refusal], 2))
|
||||
session = {"broker_session_id": "s1", "claimed_at": "t1", "user_id": admin_user.id}
|
||||
pushed = AsyncMock()
|
||||
with (
|
||||
@@ -5710,6 +5744,7 @@ def test_run_launch_pushes_refusals_when_the_broker_refuses_an_import(
|
||||
"docs": None,
|
||||
}
|
||||
]
|
||||
assert payload["refusals_truncated"] == 2
|
||||
assert "shape_mismatch" in payload["detail"]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user