diff --git a/package.json b/package.json index daf5f23..e017fda 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coredrive-rx", - "version": "1.11.1", + "version": "1.11.2", "private": false, "license": "GPL-3.0-or-later", "type": "module", diff --git a/public/sw.js b/public/sw.js index 22a9957..478dbe9 100644 --- a/public/sw.js +++ b/public/sw.js @@ -1,7 +1,7 @@ // Minimal service worker: makes the app installable and lets the shell load // offline. Network-first (always fresh online), cache fallback (offline). // Only the static app assets go through fetch; MQTT runs over WSS (not cached). -const CACHE = 'coredrive-rx-v37'; +const CACHE = 'coredrive-rx-v38'; // config.json is deliberately NEVER cached, in either direction. It is // per-deployment runtime config, and a stale copy is worse than none: flags added diff --git a/src/app.js b/src/app.js index d8a24b6..b6cd785 100644 --- a/src/app.js +++ b/src/app.js @@ -26,7 +26,7 @@ import { shareLog } from './sharelog.js'; import { Gps } from './gps.js'; import { Queue } from './queue.js'; import { Publisher, KEEPALIVE_SECS } from './publisher.js'; -import { drainOnce } from './drain.js'; +import { drainOnce, serialiseDrain } from './drain.js'; import { loadConfig, getConfig, featureEnabled } from './config.js'; import { buildRfLogRecord } from './capture.js'; import { buildStatsRequest, parseStats, mergeSample, nextSampleDelay, STATS_CORE, STATS_RADIO, STATS_PACKETS } from './rfstats.js'; @@ -921,7 +921,12 @@ async function refreshCounters() { // a 59-record backlog needs ~5 s of continuous link at 86 ms round-trip and ~18 s at // 300 ms. On a mobile link that stayed up about a second at a time, that design could // never commit anything at all. -async function drain() { +// Serialised: drainLoop, the broker 'connect' event, the 'online' event and the Push +// button all call this, and two overlapping passes each take their own queue snapshot +// and publish the SAME rows. Seen in the field as 'published 59 record(s)' followed one +// second later by 'published 49 record(s)' for a 59-record queue — 49 duplicate rows +// delivered to the ingestor. A caller arriving mid-pass now joins the running one. +const drain = serialiseDrain(async () => { const r = await drainOnce({ queue: state.queue, publisher: state.publisher, @@ -935,7 +940,7 @@ async function drain() { dbg('published ' + r.committed + ' record(s)' + (r.stopped === 'link' ? ' before the link dropped — rest kept' : ''), 'ok'); } return r.committed; -} +}); // drainLoop runs forever every 5 s. A publish to a dead socket never acks, but // publisher.publish now times out (rejecting), and rescheduling lives in `finally`, so a diff --git a/src/drain.js b/src/drain.js index 0b78aad..018c029 100644 --- a/src/drain.js +++ b/src/drain.js @@ -101,3 +101,29 @@ export async function drainOnce({ queue, publisher, pubkey, name, failures, log return { published, committed, skipped, stopped }; } + +// serialiseDrain wraps a drain function so only ONE pass runs at a time; a caller +// arriving mid-pass joins the one already running instead of starting its own. +// +// Four call sites had no coordination: the 5 s drainLoop, the broker 'connect' event, +// the 'online' event and the "Push pending now" button. Two overlapping passes each +// call queue.takeAll(), so both see — and both PUBLISH — the rows the other has not +// committed yet. Observed in the field as `published 59 record(s)` followed one second +// later by `published 49 record(s)` for a 59-record queue: the second pass started +// after the first had committed its opening batch of COMMIT_EVERY, found the other 49 +// still queued, and delivered every one of them to the ingestor a second time. +// +// Joining rather than queueing a follow-up is deliberate: the second caller wants to +// know the outcome, and anything queued after the running pass took its snapshot is +// picked up by the next 5 s tick anyway. The slot is released on rejection too, so a +// failed pass can never wedge every future drain. +export function serialiseDrain(fn) { + let inFlight = null; + return (...args) => { + if (inFlight) return inFlight; + inFlight = Promise.resolve() + .then(() => fn(...args)) + .finally(() => { inFlight = null; }); + return inFlight; + }; +} diff --git a/test/drain.test.mjs b/test/drain.test.mjs index 9c0b870..d42b729 100644 --- a/test/drain.test.mjs +++ b/test/drain.test.mjs @@ -156,3 +156,46 @@ test('repeated LINK drops never quarantine an innocent record', async () => { assert.strictEqual(failures.size, 0, 'no strikes may accumulate from link drops'); assert.deepStrictEqual(q.remaining(), [1, 2, 3], 'nothing lost, nothing skipped'); }); + +// --- serialiseDrain: two passes must never publish the same rows ------------- + +test('serialiseDrain joins a concurrent caller onto the pass already running', async () => { + // Field evidence: "published 59 record(s)" followed one second later by + // "published 49 record(s)" for a 59-record queue. 59 - 49 = 10 = COMMIT_EVERY: + // a second pass called takeAll() after the first had committed its opening batch, + // saw the other 49 still queued, and delivered every one of them a second time. + // Callers are drainLoop (5 s), the broker 'connect' event, the 'online' event and + // the Push button — none of which coordinated. + const { serialiseDrain } = await import('../src/drain.js'); + let starts = 0; + let release; + const gate = new Promise((r) => { release = r; }); + const drain = serialiseDrain(async () => { starts++; await gate; return 'result'; }); + + const a = drain(); + const b = drain(); + const c = drain(); + release(); + assert.deepStrictEqual(await Promise.all([a, b, c]), ['result', 'result', 'result']); + assert.strictEqual(starts, 1, 'exactly one pass may run'); +}); + +test('serialiseDrain allows a NEW pass once the previous one finished', async () => { + const { serialiseDrain } = await import('../src/drain.js'); + let starts = 0; + const drain = serialiseDrain(async () => { starts++; }); + await drain(); + await drain(); + assert.strictEqual(starts, 2); +}); + +test('serialiseDrain releases the slot after a rejection, and propagates it', async () => { + // A failed pass must not wedge every future drain — that would be the config-load + // bug all over again, in the publish path. + const { serialiseDrain } = await import('../src/drain.js'); + let starts = 0; + const drain = serialiseDrain(async () => { starts++; if (starts === 1) throw new Error('boom'); return 'ok'; }); + await assert.rejects(() => drain(), /boom/); + assert.strictEqual(await drain(), 'ok', 'the slot must be free again'); + assert.strictEqual(starts, 2); +});