fix(stalker): keep a stopped watchdog interval stopped

A worker error that arrived after stop() started the page fallback
interval, which nothing cleared, so pings continued for an inactive
playlist. stop() now marks the interval stopped, detaches the worker
handlers, and the fallback refuses to start afterwards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
4grayandClaude Opus 5.5 committed 2026-09-29 20:48:57 +02:00
1 parent 3193d0e8d1
commit 46de639ebc
2 files changed
+23

No files matched your search

@@ -98,4 +98,19 @@ describe('createBackgroundInterval', () => {
jest.advanceTimersByTime(60_000);
expect(callback).toHaveBeenCalledTimes(1);
});
it('stays stopped when a worker error arrives after stop', () => {
scope.Worker = FakeWorker;
const callback = jest.fn();
const stop = createBackgroundInterval(callback, 30_000);
const [worker] = FakeWorker.instances;
const lateError = worker.onerror;
stop();
lateError?.(new Event('error'));
jest.advanceTimersByTime(90_000);
expect(callback).not.toHaveBeenCalled();
expect(jest.getTimerCount()).toBe(0);
});
});
@@ -24,7 +24,10 @@ export function createBackgroundInterval(
periodMs: number
): () => void {
let fallback: ReturnType<typeof setInterval> | undefined;
let stopped = false;
const startFallback = () => {
// A worker error that arrives after stop() must not revive the tick.
if (stopped) return;
fallback ??= setInterval(callback, periodMs);
};
const ticker = startTickerWorker();
@@ -45,6 +48,11 @@ export function createBackgroundInterval(
startFallback();
}
return () => {
stopped = true;
if (ticker) {
ticker.worker.onmessage = null;
ticker.worker.onerror = null;
}
stopWorker();
if (fallback !== undefined) clearInterval(fallback);
};