fix(sync): a save from an older copy can no longer put a run heat back to ready (1.89.37)

This commit is contained in:
larssand committed 2026-11-15 16:35:43 +01:00
1 parent 93c590e5b8
commit cea278518e
7 files changed
+104 -6

No files matched your search

+6
View File
@@ -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
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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": {
+6 -3
View File
@@ -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(
+35
View File
@@ -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 };
+1
View File
@@ -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";
+53
View File
@@ -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);
});