fix(api): the same game added to a run twice is one launch (#426)

Follow-up to #422, from the nesktop #3 review. POST /session/{id}/launch
for a game that already has an open launch (requested or running, no
stop requested) in the run now answers that launch with 200 instead of
inserting a second one. The check runs under the run's row lock that
addLaunch already takes, so two concurrent adds resolve to one launch.
OpenAPI documents the 200.

Tested: apps/api 208 pass against the local test Postgres, including a
new test adding the same game twice concurrently.
This commit is contained in:
Wanjohi
2026-10-17 16:51:25 +03:00
committed by GitHub
parent 0c348d3973
commit 3ce8aee427
3 changed files with 29 additions and 1 deletions
+12 -1
View File
@@ -1230,7 +1230,8 @@ export namespace Session {
| { outcome: 'notFound' }
| { outcome: 'notRunning'; session: Info }
| { outcome: 'hostTooOld' }
| { outcome: 'created'; launch: Launch.Info };
| { outcome: 'created'; launch: Launch.Info }
| { outcome: 'existing'; launch: Launch.Info };
/**
* Add a launch to a run that is going. The library and account checks are
@@ -1266,6 +1267,16 @@ export namespace Session {
return { outcome: 'notRunning', session: serialize(run) };
}
if (!(await Launch.hostTakesLaunches(run.id))) return { outcome: 'hostTooOld' };
// The same game asked for twice (two windows, or a retry after a
// lost reply) is one launch: under the run's lock the first add
// has committed, so the second finds it and gets it back.
const open = (await Launch.listForSession(run.id)).find(
(l) =>
l.gameId === input.gameId &&
(Launch.OPEN as readonly string[]).includes(l.state) &&
l.timeStopRequested === null
);
if (open) return { outcome: 'existing', launch: open };
const launch = await Launch.insert({
id: Identifier.ascending('launch'),
sessionId: run.id,