diff --git a/backend/endpoints/streaming.py b/backend/endpoints/streaming.py index 9b61f401a..a753ec884 100644 --- a/backend/endpoints/streaming.py +++ b/backend/endpoints/streaming.py @@ -427,7 +427,12 @@ async def _reserve_container( # A drain marker names no holder, so the holder comes from a live session, # while `draining` still says a container is about to come free. - snapshots = [await get_session(candidate.key) or {} for candidate in candidates] + snapshots = [ + snapshot or {} + for snapshot in await asyncio.gather( + *(get_session(candidate.key) for candidate in candidates) + ) + ] draining = any(snapshot.get("draining") for snapshot in snapshots) holder = next( ( @@ -1153,7 +1158,11 @@ async def join_session( candidate, _, session = await access.resolve_named_container( platform, container ) - found = (candidate, session) if session is not None else None + found = ( + (candidate, session) + if session is not None and session_platform_matches(session, platform) + else None + ) else: found = None for candidate in containers_for_platform(platform): diff --git a/backend/tests/endpoints/test_streaming.py b/backend/tests/endpoints/test_streaming.py index c36aa7035..1e7393161 100644 --- a/backend/tests/endpoints/test_streaming.py +++ b/backend/tests/endpoints/test_streaming.py @@ -7707,6 +7707,31 @@ def test_joining_walks_past_a_session_on_another_platform( assert r.json()["rom_id"] == second_rom.id +def test_joining_a_named_container_busy_with_another_platform_is_a_404( + client, access_token, viewer_access_token, rom: Rom +): + """Naming the container skips the walk, not its platform check: the game there + now may be another platform's, never the one the caller asked to join.""" + member = _ws_pool_member( + rom, 0, platforms={rom.platform_slug: "pcsx2", "ngc": "dolphin"} + ) + with _streaming(member): + _claim_multiplayer(client, access_token, rom.id) + key = session_store.session_redis_key(_key_of(member)) + session = json.loads(asyncio.run(async_cache.get(key))) + session["platform"] = "ngc" + asyncio.run(async_cache.set(key, json.dumps(session))) + with _joined_room(): + r = _join( + client, + viewer_access_token, + rom.platform_slug, + container=_key_of(member), + ) + + assert r.status_code == 404 + + def test_joining_a_solo_session_finds_nothing_to_join( client, access_token, viewer_access_token, rom: Rom ): diff --git a/frontend/src/stores/streaming.test.ts b/frontend/src/stores/streaming.test.ts index 144c76c71..d07a86dfe 100644 --- a/frontend/src/stores/streaming.test.ts +++ b/frontend/src/stores/streaming.test.ts @@ -75,6 +75,12 @@ describe("emulator labels", () => { const store = useStreamingStore(); expect(store.emulatorLabel("snes9x")).toBe("snes9x"); }); + + it("falls back to the id for one named like an object's own members", () => { + const store = useStreamingStore(); + expect(store.emulatorLabel("constructor")).toBe("constructor"); + expect(store.emulatorLabel("__proto__")).toBe("__proto__"); + }); }); describe("joinable sessions", () => { diff --git a/frontend/src/stores/streaming.ts b/frontend/src/stores/streaming.ts index 8011426ce..6c011993d 100644 --- a/frontend/src/stores/streaming.ts +++ b/frontend/src/stores/streaming.ts @@ -84,7 +84,9 @@ export const useStreamingStore = defineStore("streaming", () => { function emulatorLabel(emulator: string | null | undefined): string { if (!emulator) return ""; // Label keys are lowercase, and a configured emulator keeps its case. - return config.value.emulator_labels[emulator.toLowerCase()] ?? emulator; + const labels = config.value.emulator_labels; + const key = emulator.toLowerCase(); + return Object.hasOwn(labels, key) ? labels[key] : emulator; } /** diff --git a/frontend/src/test-utils/streamingStore.ts b/frontend/src/test-utils/streamingStore.ts index e4c1103f5..3b34d2bb8 100644 --- a/frontend/src/test-utils/streamingStore.ts +++ b/frontend/src/test-utils/streamingStore.ts @@ -6,7 +6,10 @@ const EMULATOR_LABELS: Record = { export function useStreamingStore() { return { - emulatorLabel: (id: string | null | undefined) => - id ? (EMULATOR_LABELS[id.toLowerCase()] ?? id) : "", + emulatorLabel: (id: string | null | undefined) => { + if (!id) return ""; + const key = id.toLowerCase(); + return Object.hasOwn(EMULATOR_LABELS, key) ? EMULATOR_LABELS[key] : id; + }, }; } diff --git a/frontend/src/v2/views/Player/Stream.test.ts b/frontend/src/v2/views/Player/Stream.test.ts index 9eef69877..16dc1df04 100644 --- a/frontend/src/v2/views/Player/Stream.test.ts +++ b/frontend/src/v2/views/Player/Stream.test.ts @@ -587,6 +587,35 @@ describe("Stream claim hygiene", () => { ); }); + it("hands the container back when the claim lands after the player left", async () => { + // Leaving mid-claim unmounts the view, so no socket or unmount path is left + // to release what the 202 then grants. + let claimed = (_: typeof CLAIM) => {}; + mocks.claimSession.mockReturnValue( + new Promise((resolve) => { + claimed = resolve; + }), + ); + const wrapper = await launch({ picker: false }); + const playing = vmOf(wrapper).onPlay(); + await flushPromises(); + await ( + wrapper.vm as unknown as { performStop: () => Promise } + ).performStop(); + wrapper.unmount(); + + claimed(CLAIM); + await playing; + await flushPromises(); + + expect(mocks.releaseSession).toHaveBeenCalledWith( + "gba", + false, + CLAIM.container, + CLAIM.claimed_at, + ); + }); + it("names the claim it saves when it leaves a running game", async () => { mocks.saveAndExit.mockResolvedValue({ released: true, saved: true }); const wrapper = await launch({ picker: false }); diff --git a/frontend/src/v2/views/Player/Stream.vue b/frontend/src/v2/views/Player/Stream.vue index 65193f1dd..f6a7bbd04 100644 --- a/frontend/src/v2/views/Player/Stream.vue +++ b/frontend/src/v2/views/Player/Stream.vue @@ -666,6 +666,22 @@ function isOurClaim(payload: { ); } +// For a claim the player walked away from while it came up. Only a release the +// backend took clears it, and the unmount path retries one that failed. +async function handBackClaim(platform: string): Promise { + const released = await streamingStore.releaseSession( + platform, + false, + claimedContainer.value, + claimedAt.value, + ); + holdsClaim.value = !released; + if (released) { + claimedContainer.value = null; + claimedAt.value = null; + } +} + useSocketEvent("streaming:launch-phase", (payload) => { if (!isOurClaim(payload) || playerState.value !== "loading") return; launchPhase.value = payload.phase; @@ -674,22 +690,8 @@ useSocketEvent("streaming:launch-phase", (payload) => { useSocketEvent("streaming:launch-ready", async (payload) => { if (!isOurClaim(payload)) return; launchPhase.value = null; - // The player left while the game was coming up. The claim is theirs and - // still held, so hand the container back rather than entering the stream. if ((playerState.value as PlayerState) === "exited") { - const released = await streamingStore.releaseSession( - payload.platform, - false, - claimedContainer.value, - claimedAt.value, - ); - // Only a release the backend took clears the claim; the unmount path is - // the retry for one that failed. - holdsClaim.value = !released; - if (released) { - claimedContainer.value = null; - claimedAt.value = null; - } + await handBackClaim(payload.platform); return; } if (payload.resume === false) snackbar.warning(t("play.resume-failed")); @@ -869,6 +871,11 @@ async function onPlay(cardImport?: MemoryCardImport): Promise { claimedContainer.value = launching.container; claimedAt.value = launching.claimed_at; holdsClaim.value = true; + // Every exit path ran before there was a claim to hand back. + if ((playerState.value as PlayerState) === "exited") { + await handBackClaim(rom.value.platform_slug); + return; + } const endedEarly = earlyNotices.splice(0).find(isOurClaim); if (endedEarly) await endFromNotice(endedEarly); await flourish;