mirror of
https://github.com/rommapp/romm.git
synced 2026-12-06 15:54:45 +01:00
fix(streaming): hand back a claim that lands after the player left, match the platform on a named join, and label only own emulator keys (code-review)
Leaving mid-claim unmounts the view, so a 202 held by the save-pull wait landed with nothing left to release it. A named join now applies the same platform check as the unnamed walk, emulatorLabel ignores prototype members, and the busy-pool 409 reads its snapshots concurrently.
This commit is contained in:
7 files changed
+101
-20
No files matched your search
@@ -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):
|
||||
|
||||
@@ -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
|
||||
):
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,10 @@ const EMULATOR_LABELS: Record<string, string> = {
|
||||
|
||||
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;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -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<typeof CLAIM>((resolve) => {
|
||||
claimed = resolve;
|
||||
}),
|
||||
);
|
||||
const wrapper = await launch({ picker: false });
|
||||
const playing = vmOf(wrapper).onPlay();
|
||||
await flushPromises();
|
||||
await (
|
||||
wrapper.vm as unknown as { performStop: () => Promise<void> }
|
||||
).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 });
|
||||
|
||||
@@ -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<void> {
|
||||
const released = await streamingStore.releaseSession(
|
||||
platform,
|
||||
false,
|
||||
claimedContainer.value,
|
||||
claimedAt.value,
|
||||
);
|
||||
holdsClaim.value = !released;
|
||||
if (released) {
|
||||
claimedContainer.value = null;
|
||||
claimedAt.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
useSocketEvent<LaunchPhase>("streaming:launch-phase", (payload) => {
|
||||
if (!isOurClaim(payload) || playerState.value !== "loading") return;
|
||||
launchPhase.value = payload.phase;
|
||||
@@ -674,22 +690,8 @@ useSocketEvent<LaunchPhase>("streaming:launch-phase", (payload) => {
|
||||
useSocketEvent<LaunchReady>("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<void> {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user