diff --git a/docs/releases/v1.18.3.md b/docs/releases/v1.18.3.md new file mode 100644 index 0000000..b0b4476 --- /dev/null +++ b/docs/releases/v1.18.3.md @@ -0,0 +1,31 @@ +# CoreDrive RX v1.18.3 + +Fixes a browser-only error that cost the contact-path restore on the first region ask to +every saved-contact repeater. + +## What's new + +- **Fix: "regions: ask to … failed: Illegal invocation".** `holdUntilReply`'s default + argument was `timers = { setTimeout, clearTimeout }`, so the native timers were called + with that object literal as their receiver. Browsers require the global there and + Chromium rejects anything else with `TypeError: Illegal invocation`. Node's timers do + not check, which is why 279 tests passed over it. + + The throw lands inside `askRepeater`'s `try`, after the request has already gone out, + so two things were skipped: the 20 s hold that keeps the queue still until the + repeater answers, and the write that restores that repeater's original `out_path`. + The restore record stayed in `localStorage` and was replayed on the next connect, so + the path came back — but only at the next connect, not at the end of the ask. + + It fires once per repeater per session: only the ask that applies the path override + reaches the hold, and after that the contact's `out_path_len` is already 0. A field + log from 2026-09-18 shows exactly three, one each for `14036f`, `e3d3f4` and `cf7903`, + every one on "attempt 1 of 4". + + `test/regionsched-timers.test.mjs` pins the receiver rule with a stub that throws + unless the timers are called the way a browser demands. + +## Upgrade notes + +- Service-worker cache bumped to `coredrive-rx-v52`. +- No configuration or data migration. The MQTT payload contract is unchanged. diff --git a/package.json b/package.json index 05cf6d4..b3b7d70 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coredrive-rx", - "version": "1.18.2", + "version": "1.18.3", "private": false, "license": "GPL-3.0-or-later", "type": "module", diff --git a/public/sw.js b/public/sw.js index 4278902..91d9505 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-v51'; +const CACHE = 'coredrive-rx-v52'; // 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/regionsched.js b/src/regionsched.js index 025884d..0deba96 100644 --- a/src/regionsched.js +++ b/src/regionsched.js @@ -243,7 +243,19 @@ export function markAnswered(answered, queue, target) { // were visible and all arrived 1–2s after the ask. Ending the hold on the reply removes // only time AFTER the answer is in hand; how early a restore is safe while no reply has // come is a firmware question, so the timeout stays for that case. -export function holdUntilReply(waiters, target, timeoutMs, timers = { setTimeout, clearTimeout }) { +// The default timers are wrapped, not passed by reference. A browser requires the +// native timer functions to be called with the global as `this`, and +// `({ setTimeout }).setTimeout(fn, ms)` hands them the object literal instead, which +// Chromium rejects with "TypeError: Illegal invocation". Node's timers do not check, +// so every test here passed while the field log of 2026-09-18 lost the hold AND the +// contact-path restore on the first ask to each saved-contact repeater (three of them +// that morning). test/regionsched-timers.test.mjs pins the receiver rule. +const defaultTimers = () => ({ + setTimeout: (fn, ms) => setTimeout(fn, ms), + clearTimeout: (id) => clearTimeout(id), +}); + +export function holdUntilReply(waiters, target, timeoutMs, timers = defaultTimers()) { return new Promise((resolve) => { const timer = timers.setTimeout(() => { waiters.delete(target); resolve('timeout'); }, timeoutMs); waiters.set(target, () => { timers.clearTimeout(timer); waiters.delete(target); resolve('reply'); }); diff --git a/test/regionsched-timers.test.mjs b/test/regionsched-timers.test.mjs new file mode 100644 index 0000000..cf89f8a --- /dev/null +++ b/test/regionsched-timers.test.mjs @@ -0,0 +1,52 @@ +// holdUntilReply's DEFAULT timers, which every other test bypasses by passing its own. +// +// Browsers require the native timer functions to be called with the global object as +// `this`: `({ setTimeout }).setTimeout(fn, 0)` throws "TypeError: Illegal invocation" +// in Chromium. Node's setTimeout is an ordinary function and does not check, so a +// default built as an object literal passes every test here and throws on a phone. +// +// It did. Field log 2026-09-18 08:21:06, 08:26:23 and 08:26:54, one per repeater, +// each immediately after the ask went out and each on the one attempt where the +// contact-path override had been applied — so the hold was skipped and the repeater's +// original path was never restored in that session. +// +// The stub below is the browser's rule, not a mock of our code: it throws unless it is +// called with the global as its receiver. +// Run: node --test +import { test } from 'node:test'; +import assert from 'node:assert'; +import { holdUntilReply, releaseHold } from '../src/regionsched.js'; + +const A = 'a'.repeat(64); + +function withStrictTimers(fn) { + const realSet = globalThis.setTimeout; + const realClear = globalThis.clearTimeout; + globalThis.setTimeout = function strictSetTimeout(cb, ms) { + if (this !== globalThis && this !== undefined) throw new TypeError('Illegal invocation'); + return realSet(cb, ms); + }; + globalThis.clearTimeout = function strictClearTimeout(id) { + if (this !== globalThis && this !== undefined) throw new TypeError('Illegal invocation'); + return realClear(id); + }; + try { return fn(); } finally { + globalThis.setTimeout = realSet; + globalThis.clearTimeout = realClear; + } +} + +test('the default timers survive a browser receiver check', async () => { + const waiters = new Map(); + const held = withStrictTimers(() => holdUntilReply(waiters, A, 50)); + assert.strictEqual(typeof held.then, 'function'); + assert.strictEqual(await held, 'timeout'); +}); + +test('releasing a default-timer hold clears its timer without throwing', async () => { + const waiters = new Map(); + const held = withStrictTimers(() => holdUntilReply(waiters, A, 20000)); + withStrictTimers(() => assert.strictEqual(releaseHold(waiters, A), true)); + assert.strictEqual(await held, 'reply'); + assert.strictEqual(waiters.size, 0); +});