diff --git a/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.test.ts b/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.test.ts index 609e2c795f5..531ee347220 100644 --- a/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.test.ts +++ b/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.test.ts @@ -43,16 +43,21 @@ describe('RelayWallClockTimer', () => { expect(source.count()).toBe(0) }) - it('keeps waiting when the machine resumes before the task is due', () => { + it('re-arms from the wall clock when the machine resumes before the task is due', () => { vi.useFakeTimers() let wall = 1_000_000 const source = resumeSource() const timer = new RelayWallClockTimer(() => wall, source.subscribe) const task = vi.fn() timer.schedule(180_000, task) + // A 60 s sleep: the timer clock lost it, so 120 s of wall time are left. wall += 60_000 source.resume() expect(task).not.toHaveBeenCalled() + vi.advanceTimersByTime(119_000) + expect(task).not.toHaveBeenCalled() + vi.advanceTimersByTime(1_000) + expect(task).toHaveBeenCalledTimes(1) vi.advanceTimersByTime(180_000) expect(task).toHaveBeenCalledTimes(1) }) diff --git a/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.ts b/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.ts index d3c5edcfa42..da84d170f86 100644 --- a/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.ts +++ b/src/main/runtime/mobile-cloud-relay/relay-wall-clock-timer.ts @@ -8,7 +8,7 @@ const subscribeResume: SubscribeResume = (onResume) => // A one-shot timer due at a wall-clock time. Node timers run on a clock that stops while the // machine sleeps, so a relay token renewal due during a sleep would fire that much later after // wake, after the token's wall-clock expiry: the relay then closes the still-live control 4401. -// On resume, a task already due runs at once. +// On resume, a task already due runs at once; one not yet due is re-armed from the wall clock. export class RelayWallClockTimer { private timer: ReturnType | null = null private unsubscribe: (() => void) | null = null @@ -24,12 +24,20 @@ export class RelayWallClockTimer { this.cancel() this.dueAt = this.now() + delayMs this.task = task - this.timer = setTimeout(() => this.fire(), delayMs) - // The lifecycle reports the current state on subscribe; only later resumes count. + this.arm(delayMs) + // The lifecycle reports the current state on subscribe; only later resumes count. On each + // resume a due task runs, and one not yet due is re-armed for the time left on the wall clock + // (the timer clock lost the whole sleep). let subscribed = false this.unsubscribe = this.onResume(() => { - if (subscribed && this.task === task && this.now() >= this.dueAt) { + if (!subscribed || this.task !== task) { + return + } + const left = this.dueAt - this.now() + if (left <= 0) { this.fire() + } else { + this.arm(left) } }) subscribed = true @@ -45,6 +53,13 @@ export class RelayWallClockTimer { this.task = null } + private arm(delayMs: number): void { + if (this.timer) { + clearTimeout(this.timer) + } + this.timer = setTimeout(() => this.fire(), delayMs) + } + private fire(): void { const task = this.task this.cancel()