mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-19 09:15:19 +03:00
fix(api): a box gets one run, and a stopped run keeps no address
Two invariants the session endpoint stated but did not hold. A box runs one thing at a time. `POST /session` read `activeForBox` and refused when something was already running, but the read and the insert are two statements with nothing between them: two requests that both saw "nothing is running" each got a row, and the job poll then handed the host the same box to start twice. Demonstrated at 2 rows and 2 jobs from one box. That is the failure the state claim exists to prevent, one step earlier, and it takes the same answer — a partial unique index on the predicate the read asks about, so the database refuses the second insert. `Session.request` turns that refusal into the same 409 in the same words, so a caller cannot tell which of the two caught it. The migration resolves any existing duplicates before creating the index, keeping each box's newest unstopped run because that is the one a person is waiting on, and stopping the rest rather than deleting them. Separately, a run that reached `ended` or `failed` kept the last ticket it published. Publishing a new one is already refused, so the stale address was both the only ticket a client could read for a dead run and the one nothing was allowed to replace — and a client that polls would dial it. Terminal transitions now clear it, in `setState` as well as in the compare-and-set, so the invariant does not depend on which writer stopped the run. Seven tests, each checked against the unfixed code first. The published descriptions for the ticket field and the read endpoint now say that a stopped run has no address.
This commit is contained in:
@@ -164,12 +164,17 @@ export namespace SessionApi {
|
||||
// A box runs one thing at a time. Refusing is the honest answer;
|
||||
// starting a second run would leave two rows that both think they
|
||||
// own the same hardware.
|
||||
//
|
||||
// This read is the message, not the guarantee — two callers can
|
||||
// both pass it. `Session.request` is refused by a unique index on
|
||||
// the same predicate, and answers with the same 409 in the same
|
||||
// words, so which one caught it is not visible from here.
|
||||
const active = await Session.activeForBox(box.id);
|
||||
if (active) {
|
||||
conflict('That box already has a run that has not stopped');
|
||||
conflict(Session.BOX_BUSY);
|
||||
}
|
||||
|
||||
const session = await Session.create({
|
||||
const session = await Session.request({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId: game.id,
|
||||
@@ -185,7 +190,7 @@ export namespace SessionApi {
|
||||
tags: ['Session'],
|
||||
summary: 'Read a run you asked for',
|
||||
description:
|
||||
'Poll this while a run starts. The ticket appears part-way through and is republished as addresses are discovered, so re-read it rather than keeping the first one — a client that treats the first ticket as final works on a local network and fails from anywhere else.',
|
||||
'Poll this while a run starts. The ticket appears part-way through and is republished as addresses are discovered, so re-read it rather than keeping the first one — a client that treats the first ticket as final works on a local network and fails from anywhere else. Once the run reaches a terminal state the ticket is null: stop polling and stop dialling it.',
|
||||
responses: {
|
||||
200: {
|
||||
content: { 'application/json': { schema: Result(Session.Info) } },
|
||||
|
||||
Reference in New Issue
Block a user