fix(streaming): take one answer from a save pull once the emulator has stopped

The pull retries to wait out an emulator still writing its saves, but every teardown stops the emulator first, so a pull after an exit with nothing new ran all five attempts and held the player's next claim for about twelve seconds. Only a background save-and-exit, where the broker may still be saving, keeps retrying.
This commit is contained in:
Zach Clendenen
2026-09-17 20:10:07 -05:00
parent 2512069345
commit 36844e6c93
4 changed files with 75 additions and 10 deletions

View File

@@ -912,7 +912,13 @@ async def save_and_exit_session(
await lifecycle.clear_session_activity(session_key, session)
# Before the key goes, so a claim that wins it next waits for the pull.
pull_mark = await lifecycle.mark_exit_saves_pending(container, session)
lifecycle.collect_exit_saves(container, session, pull_mark)
# A webstation exit always blocks, whatever wait says.
lifecycle.collect_exit_saves(
container,
session,
pull_mark,
settled=effective_wait or container.is_webstation,
)
# Sync the exit save to the library. With wait=false the broker save may
# still be running; the pull blocks on the broker until it finishes.
@@ -1608,7 +1614,9 @@ async def force_release_all(
pull_mark = await lifecycle.mark_exit_saves_pending(
container, session
)
lifecycle.collect_exit_saves(container, session, pull_mark)
lifecycle.collect_exit_saves(
container, session, pull_mark, settled=True
)
# Note who ended it before the key goes, so the player's next poll
# can explain the stream vanishing.

View File

@@ -125,12 +125,13 @@ async def _pull_exit_saves(
container: ResolvedContainer,
mark: saves.SavePullMark,
broker_session: str | None,
settled: bool,
) -> None:
"""The spawned half of `collect_exit_saves`, which lets the next claim
through however the pull ended."""
try:
await saves.pull_saves_to_library(
mark.user_id, mark.rom_id, container, broker_session
mark.user_id, mark.rom_id, container, broker_session, settled=settled
)
finally:
await saves.clear_save_pull_pending(mark)
@@ -161,11 +162,14 @@ def collect_exit_saves(
container: ResolvedContainer,
session: dict[str, Any],
mark: saves.SavePullMark | None,
*,
settled: bool,
) -> None:
"""Start pulling the in-game save archive a stopped session left behind.
Fire and forget: the broker keeps the archive after the emulator dies, so
no teardown has to wait on it. The pull clears `mark` however it ends.
no teardown has to wait on it. The pull clears `mark` however it ends, and
`settled` says the emulator is done writing, so the first answer is final.
One home for the rule: every teardown path files a session's saves the same
way, and under the session's owner rather than whoever ended it.
@@ -173,7 +177,7 @@ def collect_exit_saves(
if mark is None:
return
background.spawn_sync_task(
_pull_exit_saves(container, mark, broker_session_id(session))
_pull_exit_saves(container, mark, broker_session_id(session), settled)
)
@@ -389,7 +393,7 @@ async def teardown_released_session(
# Awaited, not spawned: the claim is released below.
await collect_exit_state(container, session, state_slot)
collect_exit_saves(container, session, pull_mark)
collect_exit_saves(container, session, pull_mark, settled=True)
# The pull clears it from here.
pull_mark = None
@@ -463,7 +467,7 @@ async def _teardown_abandoned_session(
await record_play_session(session)
await clear_session_activity(session_key, session)
await collect_exit_state(container, session, state_slot)
collect_exit_saves(container, session, pull_mark)
collect_exit_saves(container, session, pull_mark, settled=True)
pull_mark = None
except Exception:
log.exception("abandoned session teardown failed, key=%s", session_key)

View File

@@ -196,11 +196,14 @@ async def pull_saves_to_library(
rom_id: int,
container: ResolvedContainer,
broker_session_id: str | None = None,
*,
settled: bool = False,
) -> bool:
"""Background task: pull in-game saves from the broker and store them.
Best-effort by design, a sync failure must never surface to the player,
the save still exists inside the container.
the save still exists inside the container. The retries wait out an
emulator still writing, so a `settled` one gets a single attempt.
"""
user = db_user_handler.get_user(user_id)
rom = db_rom_handler.get_rom(rom_id)
@@ -208,7 +211,7 @@ async def pull_saves_to_library(
return False
emulator = container.emulator
for attempt in range(broker.PULL_ATTEMPTS):
for attempt in range(1 if settled else broker.PULL_ATTEMPTS):
if attempt > 0:
await asyncio.sleep(broker.PULL_RETRY_DELAY)
content = await asyncio.to_thread(

View File

@@ -4855,6 +4855,56 @@ def test_an_abandoned_teardown_that_fails_before_its_pull_leaves_nothing_pending
assert asyncio.run(_save_pull_pending(admin_user.id, rom.id)) is False
def test_a_release_that_changed_no_saves_lets_the_next_claim_straight_through(
client, access_token, admin_user: User, rom: Rom
):
"""The stop has finished writing by the time the pull asks, so the broker's
first "nothing new" is final and retrying it only holds the next claim."""
with _streaming(_container_for(rom)):
_claim_ok(client, access_token, rom.id)
with (
patch("handler.streaming.commands.stop", return_value=None),
patch("handler.streaming.background.spawn_sync_task") as spawn,
patch(
"handler.streaming.saves.fetch_save_archive", return_value=None
) as fetch,
):
client.delete(
f"/api/streaming/sessions/{rom.platform_slug}",
headers=_auth(access_token),
)
asyncio.run(spawn.call_args[0][0])
assert fetch.call_count == 1
assert asyncio.run(_save_pull_pending(admin_user.id, rom.id)) is False
def test_a_background_save_and_exit_keeps_asking_while_the_emulator_writes(
client, access_token, rom: Rom
):
"""With wait=false the broker is still saving when the pull starts, so an
early "nothing new" is not the answer yet."""
with _streaming(_container_for(rom)):
_claim_ok(client, access_token, rom.id)
with (
patch("handler.streaming.commands.save_and_exit", return_value=(False, 10)),
patch("handler.streaming.background.spawn_sync_task") as spawn,
patch(
"handler.streaming.saves.fetch_save_archive", return_value=None
) as fetch,
):
client.post(
f"/api/streaming/sessions/{rom.platform_slug}/save-and-exit",
json={"slot": 0, "wait": False},
headers=_auth(access_token),
)
for spawned in (c.args[0] for c in spawn.call_args_list):
if spawned.cr_code.co_name == "_pull_exit_saves":
asyncio.run(spawned)
else:
spawned.close()
assert fetch.call_count == broker.PULL_ATTEMPTS
def test_save_and_exit_marks_the_save_pull_before_giving_up_the_key(
client, access_token, admin_user: User, rom: Rom
):
@@ -4930,7 +4980,7 @@ def test_an_earlier_pull_finishing_leaves_a_later_exits_mark(
):
for _ in range(2):
mark = await lifecycle.mark_exit_saves_pending(container, session)
lifecycle.collect_exit_saves(container, session, mark)
lifecycle.collect_exit_saves(container, session, mark, settled=True)
first, second = (c.args[0] for c in spawn.call_args_list)
await first
behind_second = await _save_pull_pending(admin_user.id, rom.id)