mirror of
https://github.com/efiten/coredrive-rx.git
synced 2026-12-03 17:54:09 +00:00
fix: concurrent drains published the same records twice
A clean v1.11.1 field log drained the 59-record backlog four seconds after connecting - then logged "published 49 record(s)" one second later, for a queue that had held 59. 59 - 49 = 10 = COMMIT_EVERY, which pins the mechanism exactly. drain() had no mutual exclusion and four uncoordinated callers: the 5s 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. Here the second pass started just after the first committed its opening batch of 10, found the other 49 still queued, and delivered every one of them to the ingestor a second time. The hole predates the incremental-commit change - the old all-or-nothing drain could double-publish just as easily - but batching the commits is what made it visible and measurable in a log. serialiseDrain() now holds one in-flight pass; a caller arriving mid-pass joins it rather than starting its own. Joining rather than queueing a follow-up is deliberate: the second caller wants the outcome, and anything queued after the running pass took its snapshot is picked up by the next 5s tick. The slot is released on rejection too, so a failed pass cannot wedge every future drain. Otherwise that log is healthy and confirms the v1.11.1 fixes: the 59 buffered records went out in one pass, and the new instrumentation shows publisher #1 alone with connack rc=0, no retired-publisher events and no keepalive timeouts - the 4-second reconnect loop is gone. Tests: 237 pass (+3). SW cache v37 -> v38. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0dee3bd1e1
commit
22a61cdd1b
5 files changed
+79
-5
No files matched your search
+1
-1
@@ -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",
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+8
-3
@@ -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
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
Reference in new issue
Block a user