From cea278518e1d36e7b8fc38411d9a3d195f8e5488 Mon Sep 17 00:00:00 2001 From: larssand Date: Sun, 15 Nov 2026 16:35:43 +0100 Subject: [PATCH] fix(sync): a save from an older copy can no longer put a run heat back to ready (1.89.37) --- CHANGELOG.md | 6 +++++ package-lock.json | 4 +-- package.json | 2 +- server.js | 9 ++++--- server/session_guard.js | 35 ++++++++++++++++++++++++ tests/run.js | 1 + tests/session_guard.test.js | 53 +++++++++++++++++++++++++++++++++++++ 7 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 server/session_guard.js create mode 100644 tests/session_guard.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 308c528..4d935e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ The application version is maintained in `package.json` and displayed in the adm No changes recorded yet. +## 1.89.37 + +### Fixed + +- A heat that has been run no longer jumps back to ready on its own. Every open browser saves its whole copy of the state, and a second computer that was not the Race Controller Master (or a tab in the background) could still hold a copy from before the heat was run: its next save put the heat back to "ready" and its laps were lost, so the heat had to be run again. The server now keeps a started, running or finished heat and its results when a save tries to turn it back without a Reset; Reset still works, and a copy from before a Reset can't bring the old run back. The server log notes each heat it kept. + ## 1.89.36 ### Fixed diff --git a/package-lock.json b/package-lock.json index 533bc90..93914af 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rc-racecore", - "version": "1.89.36", + "version": "1.89.37", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "rc-racecore", - "version": "1.89.36", + "version": "1.89.37", "hasInstallScript": true, "dependencies": { "better-sqlite3": "^12.11.1", diff --git a/package.json b/package.json index ebb8a64..60c07ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-racecore", - "version": "1.89.36", + "version": "1.89.37", "private": true, "description": "RC RaceCore live timing and race control with AMMC ingest and SQLite persistence", "engines": { diff --git a/server.js b/server.js index 159b526..e034ea2 100644 --- a/server.js +++ b/server.js @@ -6,6 +6,7 @@ const { spawn, execFile } = require("child_process"); const express = require("express"); const QRCode = require("qrcode"); const Database = require("better-sqlite3"); +const { keepStartedSessions } = require("./server/session_guard.js"); const publicTrainingProfile = require("./server/public_training_profile"); const { createBackupService } = require("./server/backup_service"); const { createAuth, COOKIE_NAME: AUTH_COOKIE_NAME, TOKEN_MAX_AGE_MS } = require("./server/auth"); @@ -408,8 +409,7 @@ app.post("/api/sim/stop", async (_req, res) => { const RESULT_PROTECTION_WINDOW_MS = Number(process.env.RC_RESULT_PROTECTION_WINDOW_MS || 15000); const recentSessionResultWrites = new Map(); -function protectRecentSessionResults(incomingState) { - const stored = readStoredAppState(); +function protectRecentSessionResults(incomingState, stored = readStoredAppState()) { if (!stored?.state?.resultsBySession || typeof stored.state.resultsBySession !== "object") return incomingState; const now = Date.now(); for (const [sessionId, writtenAt] of recentSessionResultWrites) { @@ -507,7 +507,10 @@ app.post("/api/state", (req, res) => { return; } - const stateJson = JSON.stringify(protectRecentSessionResults(req.body)); + const stored = readStoredAppState(); + const { state: guarded, kept } = keepStartedSessions(req.body, stored?.state); + if (kept.length) console.log(`[state] Kept ${kept.length} run heat(s) a save from an older copy tried to put back to ready: ${kept.join(", ")}`); + const stateJson = JSON.stringify(protectRecentSessionResults(guarded, stored)); const nowIso = new Date().toISOString(); db.prepare( diff --git a/server/session_guard.js b/server/session_guard.js new file mode 100644 index 0000000..235c6dd --- /dev/null +++ b/server/session_guard.js @@ -0,0 +1,35 @@ +// Every open browser saves its whole copy of the state. A second computer that is not the +// Race Controller Master (or a tab left in the background) can hold a copy from before a heat +// was run, and its next save put that heat back to "ready" with no laps - the heat had to be +// run again. A heat that has started only goes back through Reset (which stamps a newer +// resultsResetAt), so a save that turns a started heat back without one comes from an older +// copy: the server keeps its own version of that heat and its results. +const STAGE = { ready: 0, running: 1, paused: 1, finished: 2, stopped: 2 }; +const stageOf = (session) => STAGE[String(session?.status || "ready")] ?? 0; + +function keepStartedSessions(incoming, stored) { + const kept = []; + if (!incoming || !Array.isArray(incoming.sessions) || !Array.isArray(stored?.sessions)) return { state: incoming, kept }; + const storedById = new Map(stored.sessions.filter((session) => session?.id).map((session) => [session.id, session])); + incoming.sessions = incoming.sessions.map((session) => { + const before = storedById.get(session?.id); + if (!before) return session; + const resetBefore = Number(before.resultsResetAt || 0); + const resetNow = Number(session.resultsResetAt || 0); + if (resetNow > resetBefore) return session; // a deliberate Reset + // Same reset (or none): only a started heat can be turned back. An older reset: a copy + // from before the latest Reset, which must not bring the old run back. + if (resetNow === resetBefore && (!before.startedAt || stageOf(session) >= stageOf(before))) return session; + kept.push(session.id); + return before; + }); + kept.forEach((sessionId) => { + const result = stored.resultsBySession?.[sessionId]; + if (result === undefined) return; + if (!incoming.resultsBySession || typeof incoming.resultsBySession !== "object") incoming.resultsBySession = {}; + incoming.resultsBySession[sessionId] = result; + }); + return { state: incoming, kept }; +} + +module.exports = { keepStartedSessions }; diff --git a/tests/run.js b/tests/run.js index 1031c69..6fb7f79 100644 --- a/tests/run.js +++ b/tests/run.js @@ -64,3 +64,4 @@ import "./decoder_watch.test.js"; import "./results_autosave.test.js"; import "./check_in.test.js"; import "./my_race.test.js"; +import "./session_guard.test.js"; diff --git a/tests/session_guard.test.js b/tests/session_guard.test.js new file mode 100644 index 0000000..13414e3 --- /dev/null +++ b/tests/session_guard.test.js @@ -0,0 +1,53 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { createRequire } from "node:module"; + +const require = createRequire(import.meta.url); +const { keepStartedSessions } = require("../server/session_guard.js"); + +const stored = () => ({ + sessions: [ + { id: "a1", status: "finished", startedAt: 100, endedAt: 400 }, + { id: "a2", status: "running", startedAt: 500 }, + { id: "a3", status: "ready", startedAt: null }, + ], + resultsBySession: { a1: { competitors: { k: { laps: 14 } } }, a2: { competitors: { k: { laps: 3 } } } }, +}); + +test("a save from an older copy (the second computer, not master) can't put run heats back to ready", () => { + const incoming = { + sessions: [{ id: "a1", status: "ready", startedAt: null }, { id: "a2", status: "ready", startedAt: null }, { id: "a3", status: "ready", startedAt: null }], + resultsBySession: {}, + }; + const { state, kept } = keepStartedSessions(incoming, stored()); + assert.deepEqual(kept, ["a1", "a2"]); + assert.equal(state.sessions[0].status, "finished"); + assert.equal(state.sessions[1].status, "running"); + assert.equal(state.resultsBySession.a1.competitors.k.laps, 14, "A final 1's laps are kept"); +}); + +test("a copy from while a heat was running can't undo its finish", () => { + const incoming = { sessions: [{ id: "a1", status: "running", startedAt: 100 }], resultsBySession: { a1: { competitors: {} } } }; + const { state, kept } = keepStartedSessions(incoming, stored()); + assert.deepEqual(kept, ["a1"]); + assert.equal(state.sessions[0].status, "finished"); +}); + +test("Reset still works, and a copy from before the reset can't bring the old run back", () => { + const reset = { sessions: [{ id: "a1", status: "ready", startedAt: null, resultsResetAt: 900 }], resultsBySession: { a1: { competitors: {} } } }; + const after = keepStartedSessions(reset, stored()); + assert.deepEqual(after.kept, []); + assert.equal(after.state.sessions[0].status, "ready"); + const old = { sessions: [{ id: "a1", status: "finished", startedAt: 100 }] }; + const { state, kept } = keepStartedSessions(old, { sessions: reset.sessions, resultsBySession: reset.resultsBySession }); + assert.deepEqual(kept, ["a1"]); + assert.equal(state.sessions[0].resultsResetAt, 900); +}); + +test("normal progress, new and deleted heats pass through untouched", () => { + const incoming = { sessions: [{ id: "a2", status: "finished", startedAt: 500, endedAt: 800 }, { id: "a3", status: "running", startedAt: 900 }, { id: "new", status: "ready" }] }; + const { state, kept } = keepStartedSessions(incoming, stored()); + assert.deepEqual(kept, []); + assert.deepEqual(state.sessions.map((session) => session.id), ["a2", "a3", "new"], "a1 deleted on purpose stays deleted"); + assert.equal(keepStartedSessions(null, stored()).state, null); +});