From 9f00d757a88ceb4f1e5d487f3c1fbb454841b5cb Mon Sep 17 00:00:00 2001 From: Zach Clendenen Date: Sun, 27 Sep 2026 09:49:13 -0500 Subject: [PATCH] fix(streaming): don't hydrate default-core states into another RetroArch core --- backend/handler/streaming/launch.py | 6 ++- backend/handler/streaming/states.py | 14 ++++++ backend/tests/endpoints/test_streaming.py | 52 +++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/backend/handler/streaming/launch.py b/backend/handler/streaming/launch.py index 6aefd91ea..db4e31348 100644 --- a/backend/handler/streaming/launch.py +++ b/backend/handler/streaming/launch.py @@ -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, ) ) diff --git a/backend/handler/streaming/states.py b/backend/handler/streaming/states.py index 4c4cf6676..f9ac154a1 100644 --- a/backend/handler/streaming/states.py +++ b/backend/handler/streaming/states.py @@ -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) diff --git a/backend/tests/endpoints/test_streaming.py b/backend/tests/endpoints/test_streaming.py index e471f1e7d..fb616fa5a 100644 --- a/backend/tests/endpoints/test_streaming.py +++ b/backend/tests/endpoints/test_streaming.py @@ -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")