From b6dcfc6a3c4e8325db9ef7dae4239f2caac83a27 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Fri, 25 Sep 2026 11:48:16 -0400 Subject: [PATCH] polish(pr-ready step 4): trim comments and docstrings, keep the newest-state lookup off the event loop Co-Authored-By: Claude Opus 5.5 (1M context) --- backend/endpoints/streaming.py | 16 ++++++---------- backend/handler/streaming/saves.py | 10 +++------- backend/handler/streaming/states.py | 6 ++---- backend/handler/streaming/webstation.py | 10 +++------- backend/tests/endpoints/test_streaming.py | 4 +++- frontend/src/v2/views/Player/Stream.vue | 4 +--- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/backend/endpoints/streaming.py b/backend/endpoints/streaming.py index 961789f72..e0d2c05e9 100644 --- a/backend/endpoints/streaming.py +++ b/backend/endpoints/streaming.py @@ -675,10 +675,8 @@ async def _hydrate_saves( if container.is_webstation: if save_foreign or import_state is not None: - # A foreign pick never has its kind's v1 path built; it rides in - # a `.import/` member instead, alongside whichever kind (if any) - # stayed native. A memory-card-synced container is not special- - # cased here: the broker refuses with memcard_synced_separately. + # A synced memory card is not special-cased: the broker refuses it + # with memcard_synced_separately. try: result = await imports.hydrate_import_archive( request.user.id, @@ -694,11 +692,9 @@ async def _hydrate_saves( if result.path is not None: return result if save_foreign: - # The foreign save itself is what failed; there is no native - # side of this pick to fall back to. + # A foreign save has no native side to fall back to. return imports.ImportHydration() - # The foreign state failed to import; a native save riding - # alongside it (or none at all) still gets ordinary hydration. + # Only the foreign state failed, so any native save still hydrates. # Restore runs inside activate on this protocol, so hydration only gets # the bytes onto the container and names the path activate restores. # Still runs under whole-card sync: the archive carries the state the @@ -856,8 +852,8 @@ async def claim_session( and not resume_foreign and container.resumes_from_archive ): - newest_states = states.user_states_for_emulator( - request.user.id, rom.id, container.emulator + newest_states = await asyncio.to_thread( + states.user_states_for_emulator, request.user.id, rom.id, container.emulator ) older_exit_state = not newest_states or newest_states[0].id != resume_state.id diff --git a/backend/handler/streaming/saves.py b/backend/handler/streaming/saves.py index 29f68e4ab..3ed9a1805 100644 --- a/backend/handler/streaming/saves.py +++ b/backend/handler/streaming/saves.py @@ -266,14 +266,10 @@ def newest_restorable(user_id: int, rom_id: int, emulator: str) -> Save | None: def resolve_save_archive( user_id: int, rom: Rom, container: ResolvedContainer, save_id: int ) -> tuple[Save, bool]: - """Validate a pick from the launch screen's save list and return - (save, is_foreign). + """Validate a launch-screen save pick and return (save, is_foreign). - Raises 404 for a save that is not the claiming user's own on this ROM. A - pick this emulator can restore natively resolves as (save, False). A - pick it cannot is checked against the broker's own import-spec before - being refused with 400, resolving as (save, True) when the broker says - it would still take it as a declared import. + Raises 404 for a save that is not the claiming user's own on this ROM, and + 400 for one neither restorable here nor accepted as an import. """ save = db_save_handler.get_save(user_id=user_id, id=save_id) # Same 404 for another user's save and another ROM's, so neither leaks. diff --git a/backend/handler/streaming/states.py b/backend/handler/streaming/states.py index dbc9b526b..4c4cf6676 100644 --- a/backend/handler/streaming/states.py +++ b/backend/handler/streaming/states.py @@ -118,10 +118,8 @@ def resolve_resume_state( Visibility follows the same rule as the state list the picker was built from: the claiming user's own states plus other users' public ones. - Raises 404 for anything invisible. A state this emulator wrote and can - read its own slot from resolves as (state, slot, False). Anything else - resolves as (state, spec slot, True) when the broker's import-spec takes - it as a resume, and is refused with 400 otherwise. + Raises 404 for anything invisible, 400 for a foreign state the broker + will not import. """ state = next( ( diff --git a/backend/handler/streaming/webstation.py b/backend/handler/streaming/webstation.py index dd8e2c6ba..0c7a4af87 100644 --- a/backend/handler/streaming/webstation.py +++ b/backend/handler/streaming/webstation.py @@ -55,9 +55,8 @@ class ImportSpec: return None -# Per container, so one broker's answer never speaks for another. A 404 (no -# imports) or 422 (unknown pair) holds for the worker's life; an answer only -# for one claim's worth of checks; a failure is never cached. +# Per container, so one broker's answer never speaks for another. A 404 or 422 +# holds for the worker's life, an answer for one claim's checks, a failure not at all. _IMPORT_SPEC_TTL = 30.0 _import_spec_cache: dict[tuple[str, str, str], tuple[float, "ImportSpec | None"]] = {} @@ -91,10 +90,7 @@ def _parse_import_spec(body: dict[str, Any]) -> ImportSpec | None: def import_spec( container: ResolvedContainer, emulator: str, platform: str ) -> ImportSpec | None: - """What this container's broker will accept as a declared import for - (emulator, platform), or None when nothing will (no imports at all, an - unrecognized pair, or the check itself could not be answered right now). - """ + """What this broker accepts as a declared import, or None when nothing or unknown.""" if not container.is_webstation: return None cache_key = (container.key, emulator, platform) diff --git a/backend/tests/endpoints/test_streaming.py b/backend/tests/endpoints/test_streaming.py index 25a64be50..930287a53 100644 --- a/backend/tests/endpoints/test_streaming.py +++ b/backend/tests/endpoints/test_streaming.py @@ -5220,7 +5220,9 @@ def test_resolve_resume_state_refuses_a_foreign_pick_when_the_channel_is_none( other = db_state_handler.add_state( _state_for(rom, admin_user, "Game.01.p2s", "pcsx2") ) - spec = webstation.ImportSpec(kinds=(), state_channel="none", state_slot=None) + spec = webstation.ImportSpec( + kinds=frozenset(), state_channel="none", state_slot=None + ) with patch("handler.streaming.states.webstation.import_spec", return_value=spec): with pytest.raises(HTTPException) as exc: states.resolve_resume_state( diff --git a/frontend/src/v2/views/Player/Stream.vue b/frontend/src/v2/views/Player/Stream.vue index 5fbd49bd6..9a3a5c9b9 100644 --- a/frontend/src/v2/views/Player/Stream.vue +++ b/frontend/src/v2/views/Player/Stream.vue @@ -969,9 +969,7 @@ async function onPlay(cardImport?: MemoryCardImport): Promise { errorHint.value = t("play.error-hint-not-configured"); } } else if (status === 400 && typeof detail === "string") { - // A save/state pick the backend refused (e.g. no container will - // import it) carries a specific reason worth showing over the - // generic hint. + // A refused save/state pick carries a reason worth more than the generic hint. errorType.value = "server"; errorMessage.value = t("play.stream-error-generic"); errorHint.value = detail;