fix: generation guard closes RF sampler timer leak across reconnect

tick() awaits state.queue.add(sample) mid-cycle. If disconnectAll()
runs during that await, stopRfSampler() clears a timer that has
already fired, and the resumed tick then reschedules itself at the
end regardless. If the user reconnects inside that window,
startRfSampler() overwrites state.rfTimer with its new session's
timer, but the stale tick's later reschedule clobbers it again,
leaving a second concurrent tick loop running against its own
closure's orphaned `pending` map.

Each RF sampler session now bumps state.rfGen; a tick only
reschedules itself if the generation it captured at start is still
current, so a superseded tick dies quietly after finishing (and
queuing) whatever sample it was mid-flight on.

No test added: app.js exports nothing and wires DOM/BLE listeners
directly at import time (window.addEventListener('DOMContentLoaded',
...), document.getElementById via els()), with no existing
app.test.mjs or DOM/BLE mocking harness in this repo. Standing up
that infrastructure for this one seam would be a larger change than
the fix itself.

Bump sw.js CACHE per repo convention for any frontend file change.
This commit is contained in:
efiten committed 2026-08-18 12:42:52 +02:00
1 parent b4c9194265
commit 125eb356f5
2 files changed
+14 -2

No files matched your search

+1 -1
View File
@@ -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-v16';
const CACHE = 'coredrive-rx-v17';
self.addEventListener('install', () => self.skipWaiting());
self.addEventListener('activate', (e) => e.waitUntil((async () => {
+13 -1
View File
@@ -42,7 +42,7 @@ const state = {
// auto-discover
lastHeardAt: null, lastFireAt: 0, tick: null,
// RF environment sampler
rfTimer: null, lastRfSample: null,
rfTimer: null, lastRfSample: null, rfGen: 0,
};
const RECENT_MAX = 20;
@@ -518,6 +518,16 @@ function startRfSampler() {
const cfg = getConfig();
if (!cfg || !cfg.rfSampler) return;
// Generation guard: a tick awaits state.queue.add(sample) mid-cycle. If
// disconnectAll() → stopRfSampler() → startRfSampler() (reconnect) all happen
// during that await, the stale tick would otherwise resume, overwrite the new
// session's state.rfTimer with its own reschedule, and run a second concurrent
// tick loop against this closure's now-orphaned `pending` map. Each session
// bumps state.rfGen; a tick only reschedules itself if its captured
// generation is still current.
state.rfGen += 1;
const myGen = state.rfGen;
const pending = new Map(); // subType -> resolve
state.transport.onFrame((dvFrame) => {
const bytes = new Uint8Array(dvFrame.buffer, dvFrame.byteOffset, dvFrame.byteLength);
@@ -554,6 +564,7 @@ function startRfSampler() {
dbg('rf sample incomplete — discarded', 'no');
}
}
if (state.rfGen !== myGen) return; // superseded by a disconnect/reconnect during the await above
state.rfTimer = setTimeout(tick, nextSampleDelay(state.motion ? state.motion.paused : false));
};
@@ -561,6 +572,7 @@ function startRfSampler() {
}
function stopRfSampler() {
state.rfGen += 1; // invalidate any tick currently mid-await so it will not reschedule itself
if (state.rfTimer) { clearTimeout(state.rfTimer); state.rfTimer = null; }
}