fix(streaming): don't hydrate default-core states into another RetroArch core

This commit is contained in:
Zach Clendenen
2026-09-27 09:49:13 -05:00
parent 007fb90e53
commit 9f00d757a8
3 changed files with 71 additions and 1 deletions

View File

@@ -177,7 +177,11 @@ async def run_launch(
# background, the stream should not wait on file transfers.
background.spawn_sync_task(
states.hydrate_states_to_broker(
user.id, rom.id, container, resume_pushed=resume_pushed
user.id,
rom.id,
container,
resume_pushed=resume_pushed,
core_tier=core_tier,
)
)

View File

@@ -481,6 +481,7 @@ async def hydrate_states_to_broker(
rom_id: int,
container: ResolvedContainer,
resume_pushed: bool = False,
core_tier: str | None = None,
) -> int:
"""Background task: push the newest stored state for this ROM down to the
freshly claimed container. Emulators read state files lazily, so pushing
@@ -494,9 +495,22 @@ async def hydrate_states_to_broker(
nothing to add here: any push would overwrite it before the broker's
deferred load fires. A container that resumes from its save archive takes no
state file at all, and the archive already carries the newest exit state.
A container on a configured core other than the broker's default gets
nothing: stored states don't record which core wrote them, so each one is
presumed to be the default's, and pushing it lands where the other core's
quick-load reads it. `core_tier` is what the activate reply said; None
means it said nothing, which is treated as not the default.
"""
if resume_pushed or container.resumes_from_archive:
return 0
if container.core is not None and core_tier != "default":
log.info(
"not hydrating states into core %s (%s), they were made by the default",
container.core,
core_tier,
)
return 0
user = db_user_handler.get_user(user_id)
rom = db_rom_handler.get_rom(rom_id)

View File

@@ -4771,6 +4771,58 @@ def test_hydrate_skips_states_missing_on_disk(rom: Rom, admin_user: User):
push.assert_not_called()
def _hydrate_ra(rom: Rom, user: User, value, core_tier):
"""Hydrate a RetroArch container serving `rom` as `value`, returning the
count and the push mock."""
db_state_handler.add_state(_state_for(rom, user, "Game.state", "retroarch"))
container = _resolved(_webstation(platforms={rom.platform_slug: value}))
with (
patch(
"handler.filesystem.fs_asset_handler.read_file",
new=AsyncMock(return_value=b"state-bytes"),
),
patch("handler.streaming.states.push_state_file", return_value=True) as push,
):
pushed = asyncio.run(
states.hydrate_states_to_broker(
user.id, rom.id, container, core_tier=core_tier
)
)
return pushed, push
@pytest.mark.parametrize("tier", ["vetted", "untested", "blocked", None])
def test_hydrate_pushes_nothing_into_a_non_default_core(
rom: Rom, admin_user: User, tier
):
"""Stored states don't record their core yet, so every one is presumed the
default's; pushed into another core it lands where quick-load reads it."""
pushed, push = _hydrate_ra(rom, admin_user, "retroarch:bsnes", tier)
assert pushed == 0
push.assert_not_called()
def test_hydrate_still_pushes_when_the_configured_core_is_the_default(
rom: Rom, admin_user: User
):
pushed, _ = _hydrate_ra(rom, admin_user, "retroarch:snes9x", "default")
assert pushed == 1
def test_hydrate_is_unchanged_without_a_core(rom: Rom, admin_user: User):
pushed, _ = _hydrate_ra(rom, admin_user, "retroarch", None)
assert pushed == 1
def test_the_launch_hands_the_core_tier_to_hydration(client, access_token, rom: Rom):
with patch(
"handler.streaming.states.hydrate_states_to_broker",
new=AsyncMock(return_value=0),
) as hydrate:
_ra_claim(client, access_token, rom, "retroarch:bsnes", _BSNES_REPLY)
assert hydrate.call_args.kwargs["core_tier"] == "untested"
def _add_state_at(rom: Rom, user: User, file_name: str, day: int) -> State:
"""Add a state with an explicit updated_at, so history order is deterministic."""
state = _state_for(rom, user, file_name, "pcsx2")