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) <noreply@anthropic.com>
This commit is contained in:
Georges-Antoine Assi
2026-09-25 11:48:16 -04:00
parent 8f7d092d67
commit b6dcfc6a3c
6 changed files with 18 additions and 32 deletions

View File

@@ -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

View File

@@ -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.

View File

@@ -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(
(

View File

@@ -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)

View File

@@ -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(

View File

@@ -969,9 +969,7 @@ async function onPlay(cardImport?: MemoryCardImport): Promise<void> {
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;