From 314b5cd50a91d6e7a001bf3a8734e270e2517805 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 14:20:11 -0700 Subject: [PATCH 1/7] chore(quest): claim watch AV-sync demand-break investigation Co-Authored-By: GPT-6 From 4f50128f34b4000114770d69a315b8fde20626ca Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 14:43:01 -0700 Subject: [PATCH 2/7] test(watch): grade sync while a resumed viewer catches up Co-Authored-By: GPT-6 --- quest/m1/watch-break-av-sync.md | 19 ++++++++++++++ test/interop/clients/js/media.ts | 29 +++++++++++----------- test/interop/clients/js/src/resume.test.ts | 20 +++++++++++++++ test/interop/clients/js/src/resume.ts | 16 ++++++++++++ test/justfile | 2 +- 5 files changed, 70 insertions(+), 16 deletions(-) create mode 100644 test/interop/clients/js/src/resume.test.ts create mode 100644 test/interop/clients/js/src/resume.ts diff --git a/quest/m1/watch-break-av-sync.md b/quest/m1/watch-break-av-sync.md index 7983500cdc1..85afd019604 100644 --- a/quest/m1/watch-break-av-sync.md +++ b/quest/m1/watch-break-av-sync.md @@ -21,3 +21,22 @@ sync re-anchors one track and not the other. Fix it at that cause, and assert the offset in the media check. Public API: none expected. Wire: none. + +### Investigation + +On Linux Chromium the unchanged full media fixture passed its positive lifecycle +run and all negative controls. The old reattach case measured sync only after +reaching live, leaving the reported pre-live failure outside its checked window. +The draft moves the resume sync check into that catch-up window. It excludes the +held silent preview and requires each advancing audible pair to stay within the +existing one-tone-step tolerance. The negative unit fixture carries the reported +1.6-second stale-audio offset. + +A passing run does not establish the root cause or fix the player. Retain this +quest until the new early assertion captures a failure with container group and +shared-reference evidence. + +With the early assertion enabled, the positive lifecycle including reattach +passed (30.3 fps, median skew 0 ms, p95 200 ms). The full run then stopped in +the offset negative control on its unrelated audio-tone completeness floor +(68/81 samples, 84% versus 90%). Preserve that validation limit. diff --git a/test/interop/clients/js/media.ts b/test/interop/clients/js/media.ts index de75fe60006..1144300c2dc 100644 --- a/test/interop/clients/js/media.ts +++ b/test/interop/clients/js/media.ts @@ -45,14 +45,9 @@ import { waitForState, waitForWatch, } from "./harness"; -import { - type CaptureState, - FAULTS, - leakedPlayerStarted, - SAMPLE_MS, - SAMPLE_RATE, -} from "./src/contract"; +import { type CaptureState, FAULTS, leakedPlayerStarted, SAMPLE_MS, SAMPLE_RATE } from "./src/contract"; import * as Pattern from "./src/pattern"; +import { resumeSkew } from "./src/resume"; /** Cases beyond the mandatory capability probe, publisher readiness, and cold start. */ const CASES = ["capture-denial", "pause", "disable", "rejoin", "detach", "republish", "late-join"] as const; @@ -205,6 +200,7 @@ async function waitForLive( player: Page, playerErrors: BrowserErrors, assertion: string, + resumed = false, ): Promise<{ first: number; lag: number; after: number }> { const since = Date.now(); let first: number | undefined; @@ -218,6 +214,15 @@ async function waitForLive( () => [], ); if (fixture && state?.frameId !== undefined) { + if (resumed) { + const skew = resumeSkew(state, frame); + if (skew !== undefined) + check( + Math.abs(skew) <= MAX_SKEW_STEPS * Pattern.STEP_MS, + "resume audio/video sync", + () => `${assertion}: offset ${skew}ms; ${traceLine(state, since)}`, + ); + } lag = fixture.frameId - state.frameId; first ??= lag; moved = frame !== undefined && state.frameId !== frame; @@ -263,7 +268,7 @@ function traceLine(sample: PlayerState, start: number): string { ` +${((sample.at - start) / 1000).toFixed(2)}s frame=${sample.frameId ?? "-"} ` + `step=${sample.toneStep ?? "-"}/${step} tone=${margin.toFixed(0)}dB ${sample.toneHz?.toFixed(0) ?? "-"}Hz ` + `rms=${sample.rms?.toFixed(3) ?? "-"} ats=${sample.audioTimestamp?.toFixed(0) ?? "-"} vts=${sample.videoTimestamp?.toFixed(0) ?? "-"} ` + - `paused=${sample.paused} delay=${sample.delay}ms audio=${sample.audioBytes}B${sample.audioStalled ? " stalled" : ""}` + `paused=${sample.paused} delay=${sample.delay}ms audio=${sample.audioBytes}B${sample.audioStalled ? " stalled" : ""}` ); } @@ -750,13 +755,7 @@ try { console.error(" detach released every session, audio graph, and worker"); await command(player, "reattach"); - await waitForState(player, playerErrors, { - deadline: Date.now() + timeoutMs, - assertion: "reattach resumes playback", - description: `the presented frame to move past the ${busy.frameId} showing before the detach`, - predicate: (state) => (state.frameId ?? 0) > (busy.frameId ?? 0), - }); - await waitForLive(publisher, publisherErrors, player, playerErrors, "reattach reaches live"); + await waitForLive(publisher, publisherErrors, player, playerErrors, "reattach reaches live", true); await measurePlayback(player, playerErrors, "after reattach"); } diff --git a/test/interop/clients/js/src/resume.test.ts b/test/interop/clients/js/src/resume.test.ts new file mode 100644 index 00000000000..75f3ed23535 --- /dev/null +++ b/test/interop/clients/js/src/resume.test.ts @@ -0,0 +1,20 @@ +import { expect, test } from "bun:test"; +import type { Sample } from "./contract"; +import * as Pattern from "./pattern"; +import { resumeSkew } from "./resume"; + +const state = (frameId: number, toneStep: number, audioStalled = false) => + ({ frameId, toneStep, audioStalled, rms: 0.35 }) as Sample; + +test("the first advancing audible picture exposes stale audio", () => { + const sample = state(1, Pattern.expectedStep(1) + 8); + expect(Math.abs(resumeSkew(sample, 0) ?? 0)).toBeGreaterThan(Pattern.STEP_MS); + expect(resumeSkew(state(1, Pattern.expectedStep(1)), 0)).toBe(0); +}); + +test("a held preview and buffering silence are not presented AV pairs", () => { + expect(resumeSkew(state(1, 8), undefined)).toBeUndefined(); + expect(resumeSkew(state(1, 8), 1)).toBeUndefined(); + expect(resumeSkew(state(1, 8, true), 0)).toBeUndefined(); + expect(resumeSkew({ ...state(1, 8), rms: 0 }, 0)).toBeUndefined(); +}); diff --git a/test/interop/clients/js/src/resume.ts b/test/interop/clients/js/src/resume.ts new file mode 100644 index 00000000000..cc71a73c285 --- /dev/null +++ b/test/interop/clients/js/src/resume.ts @@ -0,0 +1,16 @@ +/** The audible offset of an advancing resumed picture, excluding its silent preview. */ +import { AUDIBLE_RMS, type Sample } from "./contract"; +import * as Pattern from "./pattern"; + +export function resumeSkew(sample: Sample, previousFrame: number | undefined): number | undefined { + if ( + previousFrame === undefined || + sample.frameId === undefined || + sample.frameId === previousFrame || + sample.toneStep === undefined || + sample.audioStalled || + (sample.rms ?? 0) < AUDIBLE_RMS + ) + return undefined; + return Pattern.stepSkew(sample.toneStep, Pattern.expectedStep(sample.frameId)) * Pattern.STEP_MS; +} diff --git a/test/justfile b/test/justfile index 30114d289d7..e70173f7fff 100644 --- a/test/justfile +++ b/test/justfile @@ -25,7 +25,7 @@ harness: cd .. && bun test test/interop/compat/resolve.test.ts cd .. && bun install --frozen-lockfile cd interop/clients/js && bunx playwright install chromium - cd interop/clients/js && bun test ./harness.browser.ts ./src/contract.test.ts + cd interop/clients/js && bun test ./harness.browser.ts ./src/contract.test.ts ./src/resume.test.ts # Cross-language media interop test, built from this checkout. Stands up a # relay and runs the publisher x subscriber matrix. Default: rust only (fast From 316bae8d084ccb115c57b78c73c50a5497d194e5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 10 Oct 2026 19:19:25 -0700 Subject: [PATCH 3/7] fix(watch): discard video waits invalidated by clock resets Co-Authored-By: GPT-6 Astra --- .github/workflows/nightly.yml | 4 ++ js/justfile | 5 +++ js/watch/bench/sync.ts | 38 +++++++++++++++++++ js/watch/src/sync.test.ts | 33 ++++++++++++++++- js/watch/src/sync.ts | 39 ++++++++++++-------- js/watch/src/video/decoder.test.ts | 37 ++++++++++++++++++- js/watch/src/video/decoder.ts | 2 +- quest/m1/watch-break-av-sync.md | 20 ++++++++++ test/interop/clients/js/media.ts | 10 ++++- test/interop/clients/js/src/contract.test.ts | 24 +++++++++++- test/interop/clients/js/src/contract.ts | 18 +++++++++ 11 files changed, 206 insertions(+), 24 deletions(-) create mode 100644 js/watch/bench/sync.ts diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 27c99aa9cc9..6265f109764 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -137,6 +137,10 @@ jobs: if: ${{ !cancelled() }} run: nix develop --command just js catalog-bench + - name: JS playback clock reset fanout benchmark + if: ${{ !cancelled() }} + run: nix develop --command just js sync-bench + # Times Effect create and close beside a bare `new Error()`. No threshold: it only # has to keep running, and its numbers track the creation stack's cost. - name: JS signals effect benchmark diff --git a/js/justfile b/js/justfile index 6baf0524995..0a0925e62ae 100644 --- a/js/justfile +++ b/js/justfile @@ -68,6 +68,11 @@ signals-bench: bun install --frozen-lockfile bun signals/bench/effect.ts +# Sweep shared-clock reset over independent players and parked frames. +sync-bench: + bun install --frozen-lockfile + bun watch/bench/sync.ts + # Bundle the worklets with Vite, webpack, esbuild, and Bun, and load them in Chromium, with and # without a strict CSP. Manual only: it installs webpack and launches a browser. worklet-matrix: diff --git a/js/watch/bench/sync.ts b/js/watch/bench/sync.ts new file mode 100644 index 00000000000..53810da0191 --- /dev/null +++ b/js/watch/bench/sync.ts @@ -0,0 +1,38 @@ +/** Sweep clock-reset work over independent players and parked frames on one player. */ +import { Time } from "@moq/net"; +import { Sync } from "../src/sync.ts"; + +console.log("players,parked_frames,arm_us,reset_us"); +for (const players of [1, 16, 128]) { + for (const frames of [1, 16, 128]) { + const clocks = Array.from({ length: players }, () => new Sync({ delay: Time.Milli(60_000) })); + // Let the fixed delay propagate without advancing media time. + for (let i = 0; i < 10; i++) await Promise.resolve(); + let armed = 0; + let reset = 0; + try { + for (let iteration = -20; iteration < 100; iteration++) { + const clock = clocks[0]; + const timestamp = Time.Milli.now(); + clock.received(timestamp); + const start = performance.now(); + const waiting = Array.from({ length: frames }, () => clock.wait(timestamp)); + const ready = performance.now(); + clock.reset(); + const valid = await Promise.all(waiting); + const done = performance.now(); + if (valid.some((presentable) => presentable !== false)) + throw new Error("reset left a frame presentable"); + if (iteration >= 0) { + armed += ready - start; + reset += done - ready; + } + } + console.log( + `${players},${frames},${((armed * 1000) / 100).toFixed(1)},${((reset * 1000) / 100).toFixed(1)}`, + ); + } finally { + for (const clock of clocks) clock.close(); + } + } +} diff --git a/js/watch/src/sync.test.ts b/js/watch/src/sync.test.ts index d0bef3018d8..4bbc902054a 100644 --- a/js/watch/src/sync.test.ts +++ b/js/watch/src/sync.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from "bun:test"; +import { describe, expect, it, spyOn } from "bun:test"; import { Time } from "@moq/net"; import { Signal } from "@moq/signals"; import { type Delay, Sync } from "./sync"; @@ -124,7 +124,7 @@ describe("auto delay", () => { describe("wait", () => { it("returns at once when constructed instant, before effects flush", async () => { const sync = new Sync({ delay: "instant" }); - await sync.wait(Time.Milli.zero); + expect(await sync.wait(Time.Milli.zero)).toBe(true); sync.close(); }); @@ -159,3 +159,32 @@ describe("wait", () => { sync.close(); }); }); + +it("a reset invalidates waits even when the next frame restores the same reference", async () => { + const now = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1000)); + const timer = spyOn(globalThis, "setTimeout").mockImplementation( + (() => 1 as unknown as ReturnType) as unknown as typeof setTimeout, + ); + const clear = spyOn(globalThis, "clearTimeout").mockImplementation(() => {}); + const sync = new Sync({ delay: Time.Milli(1000) }); + try { + for (let i = 0; i < 10; i++) await Promise.resolve(); + sync.received(Time.Milli(0)); + let valid: boolean | undefined; + const waiting = sync.wait(Time.Milli(0)).then((result) => { + valid = result; + }); + const reference = sync.out.reference.peek(); + sync.reset(); + sync.received(Time.Milli(0)); + expect(sync.out.reference.peek()).toBe(reference); + for (let i = 0; i < 10; i++) await Promise.resolve(); + expect(valid).toBe(false); + await waiting; + } finally { + sync.close(); + clear.mockRestore(); + timer.mockRestore(); + now.mockRestore(); + } +}); diff --git a/js/watch/src/sync.ts b/js/watch/src/sync.ts index 048572cf574..55036fd72be 100644 --- a/js/watch/src/sync.ts +++ b/js/watch/src/sync.ts @@ -91,6 +91,9 @@ export class Sync { #media = new Signal<{ target: Getter }[]>([]); + // A reset invalidates waits even if a new frame restores the same reference in one turn. + #generation = new Signal(0); + #signals = new Effect(); constructor(props?: Inputs) { @@ -197,6 +200,7 @@ export class Sync { // in buffered mode (typically alongside flushing the audio buffer) so the new content // plays from its own first frame instead of inheriting the previous reference. reset(): void { + this.#generation.update((generation) => generation + 1); this.#out.reference.set(undefined); this.#late.clear(); } @@ -209,12 +213,13 @@ export class Sync { return Time.Milli.sub(Time.Milli.sub(Time.Milli.now(), reference), this.#out.delay.peek()); } - // Sleep until it's time to render this frame. - async wait(timestamp: Time.Milli): Promise { + /** Wait until this frame can present, returning false if a clock reset invalidates it. */ + async wait(timestamp: Time.Milli): Promise { + const generation = this.#generation.peek(); // A zero delay still sleeps: the sleep comes from the reference, which holds an early frame // until its timestamp comes up. "instant" is the only thing that skips the wait itself. // Peek the input, not `out.instant`, which lags a same-turn change until effects flush. - if (this.in.delay.peek() === "instant") return; + if (this.in.delay.peek() === "instant") return true; const reference = this.#out.reference.peek(); if (reference === undefined) { @@ -222,8 +227,9 @@ export class Sync { } for (;;) { + if (this.#generation.peek() !== generation) return false; // Switching to "instant" wakes the sleep below, so frames parked here leave. - if (this.in.delay.peek() === "instant") return; + if (this.in.delay.peek() === "instant") return true; // Sleep until it's time to decode the next frame. // NOTE: This function runs in parallel for each frame. @@ -231,33 +237,34 @@ export class Sync { const ref = Time.Milli.sub(now, timestamp); const currentRef = this.#out.reference.peek(); - if (currentRef === undefined) return; + if (currentRef === undefined) return false; const sleep = Time.Milli.add(Time.Milli.sub(currentRef, ref), this.#out.delay.peek()); - if (sleep <= 0) return; + if (sleep <= 0) return true; // Skip setTimeout for small sleeps; the timer resolution (~4ms) would overshoot. - if (sleep < 5) return; + if (sleep < 5) return true; - if (await this.#sleep(sleep)) return; + await this.#sleep(sleep); } } - // Sleeps for `ms`, or returns false early once anything the sleep was computed from changes. + // Sleeps for `ms`, or wakes early once the clock or its generation changes. // Releases every listener either way: frames sleep once each, so a listener left on a signal // that never changes would pile up for the life of the player. - #sleep(ms: number): Promise { + #sleep(ms: number): Promise { return new Promise((resolve) => { - const wake = (ok: boolean) => { + const wake = () => { clearTimeout(timer); for (const dispose of disposes) dispose(); - resolve(ok); + resolve(); }; - const timer = setTimeout(() => wake(true), ms); + const timer = setTimeout(wake, ms); const disposes = [ - this.in.delay.changed(() => wake(false)), - this.#out.delay.changed(() => wake(false)), - this.#out.reference.changed(() => wake(false)), + this.#generation.changed(wake), + this.in.delay.changed(wake), + this.#out.delay.changed(wake), + this.#out.reference.changed(wake), ]; }); } diff --git a/js/watch/src/video/decoder.test.ts b/js/watch/src/video/decoder.test.ts index 71ba45a02c2..666000b1998 100644 --- a/js/watch/src/video/decoder.test.ts +++ b/js/watch/src/video/decoder.test.ts @@ -5,7 +5,7 @@ import * as Moq from "@moq/net"; import { Time } from "@moq/net"; import { type Effect, Signal } from "@moq/signals"; import type { Broadcast } from "../broadcast"; -import { Sync } from "../sync"; +import { type Delay, Sync } from "../sync"; import { Decoder } from "./decoder"; import { Source } from "./source"; @@ -266,12 +266,14 @@ async function guardedPlayback(kind: "legacy" | "cmaf", reads: Read[], hold: rea mediaProducer.close(); }; const source = new Source({ broadcast, supported: async () => true }); - const sync = new Sync({ delay: "instant" }); + const delay = new Signal("instant"); + const sync = new Sync({ delay }); const enabled = new Signal(true); const decoder = new Decoder({ source, sync, enabled }); await microtasks(); return { decoder, + delay, enabled, catalog, submitted, @@ -463,3 +465,34 @@ for (const kind of ["legacy", "cmaf"] as const) { } }); } + +it("a shared audio clock reset cannot present a parked video frame", async () => { + const now = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1000)); + const timer = spyOn(globalThis, "setTimeout").mockImplementation( + (() => 1 as unknown as ReturnType) as unknown as typeof setTimeout, + ); + const clear = spyOn(globalThis, "clearTimeout").mockImplementation(() => {}); + const reads: Read[] = []; + const playback = await guardedPlayback("legacy", reads); + try { + playback.enabled.set(false); + playback.delay.set(Time.Milli(1000)); + await microtasks(); + reads.push(sample(0, 0), sample(0, 100_000, 0, false)); + playback.enabled.set(true); + await microtasks(); + expect(playback.submitted).toEqual([0, 100_000]); + expect(playback.decoder.out.timestamp.peek()).toBe(Time.Milli(0)); + // Audio can reset the shared reference while this video's own container + // discontinuity remains unchanged. + playback.sync.reset(); + await microtasks(); + expect(playback.decoder.out.timestamp.peek()).toBe(Time.Milli(0)); + } finally { + playback.close(); + await microtasks(); + clear.mockRestore(); + timer.mockRestore(); + now.mockRestore(); + } +}); diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index 9a4986e54d1..666d4d1dae8 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -399,7 +399,7 @@ class DecoderTrack { } // Returns immediately when the latency is "instant". - const ok = await effect.race(this.sync.wait(timestamp).then(() => true)); + const ok = await effect.race(this.sync.wait(timestamp)); if (!ok) return; if (generation !== this.#discontinuity) return; // the playhead moved while waiting diff --git a/quest/m1/watch-break-av-sync.md b/quest/m1/watch-break-av-sync.md index 85afd019604..3855bc406e6 100644 --- a/quest/m1/watch-break-av-sync.md +++ b/quest/m1/watch-break-av-sync.md @@ -40,3 +40,23 @@ With the early assertion enabled, the positive lifecycle including reattach passed (30.3 fps, median skew 0 ms, p95 200 ms). The full run then stopped in the offset negative control on its unrelated audio-tone completeness floor (68/81 samples, 84% versus 90%). Preserve that validation limit. + +Astra iteration reproduced a related shared-clock defect with mocked time: when +an audio reset cleared the reference, `Sync.wait` returned normally and the video +decoder presented its parked frame. The candidate gives resets a private +generation and returns a boolean from `Sync.wait`; video discards invalidated +waits. This also covers reset and re-anchor in one microtask. Focused regressions +fail before and pass after the fix. + +The media harness now keeps early-resume grading active until audio is running +and unstalled. A moving silent preview no longer ends that grading. Existing +skew and timing bounds remain unchanged. + +This does not yet establish the cause of the original 1.6-second reattach sample. +Keep the quest until real-media evidence connects that failure to the fixed path +or independently demonstrates that it is resolved. Frames already waiting for +presentation are covered; queued WebCodecs outputs need separate evidence. + +Public API candidate: `Sync.wait` returns `Promise` instead of +`Promise`; callers must honor false. Recommend this normal cancellation +result over an exception or a public generation counter. Wire: unchanged. diff --git a/test/interop/clients/js/media.ts b/test/interop/clients/js/media.ts index 1144300c2dc..1a8ff1dee8b 100644 --- a/test/interop/clients/js/media.ts +++ b/test/interop/clients/js/media.ts @@ -45,7 +45,7 @@ import { waitForState, waitForWatch, } from "./harness"; -import { type CaptureState, FAULTS, leakedPlayerStarted, SAMPLE_MS, SAMPLE_RATE } from "./src/contract"; +import { type CaptureState, FAULTS, leakedPlayerStarted, reachesLive, SAMPLE_MS, SAMPLE_RATE } from "./src/contract"; import * as Pattern from "./src/pattern"; import { resumeSkew } from "./src/resume"; @@ -226,8 +226,14 @@ async function waitForLive( lag = fixture.frameId - state.frameId; first ??= lag; moved = frame !== undefined && state.frameId !== frame; + const live = reachesLive({ + previous: frame, + source: fixture.frameId, + sample: state, + bound: LIVE_LAG_FRAMES, + }); frame = state.frameId; - if (moved && lag <= LIVE_LAG_FRAMES) return { first, lag, after: Date.now() - since }; + if (live) return { first, lag, after: Date.now() - since }; } await sleep(POLL_INTERVAL_MS); } diff --git a/test/interop/clients/js/src/contract.test.ts b/test/interop/clients/js/src/contract.test.ts index d22d56c5a50..4d0efa7c1c5 100644 --- a/test/interop/clients/js/src/contract.test.ts +++ b/test/interop/clients/js/src/contract.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "bun:test"; -import { leakedPlayerStarted, type Resources } from "./contract"; +import { leakedPlayerStarted, type Resources, reachesLive } from "./contract"; const playing: Resources = { transports: 1, sockets: 0, audioContexts: 1, workers: 0 }; @@ -18,3 +18,25 @@ test("unchanged counts are not a leak start", () => { expect(leakedPlayerStarted(playing, playing)).toBe(false); }); +const nearLive = { + previous: 183, + source: 196, + bound: 45, + sample: { frameId: 186, audioContext: "running", audioStalled: false }, +}; + +test("an advancing preview while audio buffers is not live playback", () => { + expect(reachesLive({ ...nearLive, sample: { ...nearLive.sample, audioStalled: true } })).toBe(false); +}); + +test("a suspended audio graph cannot establish live playback", () => { + expect(reachesLive({ ...nearLive, sample: { ...nearLive.sample, audioContext: "suspended" } })).toBe(false); +}); + +test("live playback requires an advancing readable frame within the existing bound", () => { + expect(reachesLive(nearLive)).toBe(true); + expect(reachesLive({ ...nearLive, previous: undefined })).toBe(false); + expect(reachesLive({ ...nearLive, previous: 186 })).toBe(false); + expect(reachesLive({ ...nearLive, source: 232 })).toBe(false); + expect(reachesLive({ ...nearLive, sample: { ...nearLive.sample, frameId: undefined } })).toBe(false); +}); diff --git a/test/interop/clients/js/src/contract.ts b/test/interop/clients/js/src/contract.ts index cb5562dcb6e..43576717127 100644 --- a/test/interop/clients/js/src/contract.ts +++ b/test/interop/clients/js/src/contract.ts @@ -154,6 +154,24 @@ export type Sample = { resources: Resources; }; +/** Whether a changed presented frame belongs to playback within the live bound. */ +export function reachesLive(observation: { + previous?: number; + source: number; + sample: Pick; + bound: number; +}): boolean { + const { previous, source, sample, bound } = observation; + return ( + sample.audioContext === "running" && + !sample.audioStalled && + previous !== undefined && + sample.frameId !== undefined && + sample.frameId !== previous && + source - sample.frameId <= bound + ); +} + // ── the refused session ───────────────────────────────────────────────────── /** How a refused session ended, as `WebTransport.closed` reported it to the page. */ From 0eb07ab0ccf759f897c4516cc36d9852c2ad004d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 15:06:01 -0700 Subject: [PATCH 4/7] fix(watch): hold parked video waits across a clock reset Keep Sync.wait as Promise. A reset now parks a sleeping wait until the next frame re-anchors the clock instead of releasing it at once, and the video decoder drops a frame whose wait spanned a reset, keyed on an internal reset epoch that replaces its discontinuity generation. Drop the boolean return and the reset-fanout benchmark it needed. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/nightly.yml | 4 --- js/justfile | 5 --- js/watch/bench/sync.ts | 38 -------------------- js/watch/src/sync.test.ts | 49 ++++++++----------------- js/watch/src/sync.ts | 57 +++++++++++++++++------------- js/watch/src/video/decoder.test.ts | 38 ++++++++++++++++++++ js/watch/src/video/decoder.ts | 17 +++++---- quest/m1/watch-break-av-sync.md | 16 ++++----- 8 files changed, 101 insertions(+), 123 deletions(-) delete mode 100644 js/watch/bench/sync.ts diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 415d44181b7..a98a76d8594 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -145,10 +145,6 @@ jobs: if: ${{ !cancelled() }} run: nix develop --command just js catalog-bench - - name: JS playback clock reset fanout benchmark - if: ${{ !cancelled() }} - run: nix develop --command just js sync-bench - # Times Effect create and close beside a bare `new Error()`. No threshold: it only # has to keep running, and its numbers track the creation stack's cost. - name: JS signals effect benchmark diff --git a/js/justfile b/js/justfile index 897ca91b102..665735fcd6f 100644 --- a/js/justfile +++ b/js/justfile @@ -72,11 +72,6 @@ signals-bench: bun install --frozen-lockfile bun signals/bench/effect.ts -# Sweep shared-clock reset over independent players and parked frames. -sync-bench: - bun install --frozen-lockfile - bun watch/bench/sync.ts - # Bundle the worklets with Vite, webpack, esbuild, and Bun, and load them in Chromium, with and # without a strict CSP. Manual only: it installs webpack and launches a browser. worklet-matrix: diff --git a/js/watch/bench/sync.ts b/js/watch/bench/sync.ts deleted file mode 100644 index 53810da0191..00000000000 --- a/js/watch/bench/sync.ts +++ /dev/null @@ -1,38 +0,0 @@ -/** Sweep clock-reset work over independent players and parked frames on one player. */ -import { Time } from "@moq/net"; -import { Sync } from "../src/sync.ts"; - -console.log("players,parked_frames,arm_us,reset_us"); -for (const players of [1, 16, 128]) { - for (const frames of [1, 16, 128]) { - const clocks = Array.from({ length: players }, () => new Sync({ delay: Time.Milli(60_000) })); - // Let the fixed delay propagate without advancing media time. - for (let i = 0; i < 10; i++) await Promise.resolve(); - let armed = 0; - let reset = 0; - try { - for (let iteration = -20; iteration < 100; iteration++) { - const clock = clocks[0]; - const timestamp = Time.Milli.now(); - clock.received(timestamp); - const start = performance.now(); - const waiting = Array.from({ length: frames }, () => clock.wait(timestamp)); - const ready = performance.now(); - clock.reset(); - const valid = await Promise.all(waiting); - const done = performance.now(); - if (valid.some((presentable) => presentable !== false)) - throw new Error("reset left a frame presentable"); - if (iteration >= 0) { - armed += ready - start; - reset += done - ready; - } - } - console.log( - `${players},${frames},${((armed * 1000) / 100).toFixed(1)},${((reset * 1000) / 100).toFixed(1)}`, - ); - } finally { - for (const clock of clocks) clock.close(); - } - } -} diff --git a/js/watch/src/sync.test.ts b/js/watch/src/sync.test.ts index 4bbc902054a..4cce1e76d01 100644 --- a/js/watch/src/sync.test.ts +++ b/js/watch/src/sync.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it, spyOn } from "bun:test"; +import { describe, expect, it } from "bun:test"; import { Time } from "@moq/net"; import { Signal } from "@moq/signals"; import { type Delay, Sync } from "./sync"; @@ -124,7 +124,7 @@ describe("auto delay", () => { describe("wait", () => { it("returns at once when constructed instant, before effects flush", async () => { const sync = new Sync({ delay: "instant" }); - expect(await sync.wait(Time.Milli.zero)).toBe(true); + await sync.wait(Time.Milli.zero); sync.close(); }); @@ -147,44 +147,25 @@ describe("wait", () => { sync.close(); }); - it("wakes a sleeping wait on reset", async () => { + it("holds a sleeping wait across a reset until the next frame re-anchors", async () => { const sync = new Sync({ delay: 10_000 as Time.Milli }); await flush(); - sync.received(Time.Milli.now()); + const timestamp = Time.Milli.now(); + sync.received(timestamp); - const waiting = sync.wait(Time.Milli.now()); + let woke = false; + const waiting = sync.wait(timestamp).then(() => { + woke = true; + }); await flush(); sync.reset(); - await waiting; - sync.close(); - }); -}); + await flush(); + expect(woke).toBe(false); -it("a reset invalidates waits even when the next frame restores the same reference", async () => { - const now = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1000)); - const timer = spyOn(globalThis, "setTimeout").mockImplementation( - (() => 1 as unknown as ReturnType) as unknown as typeof setTimeout, - ); - const clear = spyOn(globalThis, "clearTimeout").mockImplementation(() => {}); - const sync = new Sync({ delay: Time.Milli(1000) }); - try { - for (let i = 0; i < 10; i++) await Promise.resolve(); - sync.received(Time.Milli(0)); - let valid: boolean | undefined; - const waiting = sync.wait(Time.Milli(0)).then((result) => { - valid = result; - }); - const reference = sync.out.reference.peek(); - sync.reset(); - sync.received(Time.Milli(0)); - expect(sync.out.reference.peek()).toBe(reference); - for (let i = 0; i < 10; i++) await Promise.resolve(); - expect(valid).toBe(false); + // The next frame anchors far enough ahead that this one is overdue. + sync.received(Time.Milli.add(timestamp, Time.Milli(20_000))); await waiting; - } finally { + expect(woke).toBe(true); sync.close(); - clear.mockRestore(); - timer.mockRestore(); - now.mockRestore(); - } + }); }); diff --git a/js/watch/src/sync.ts b/js/watch/src/sync.ts index 55036fd72be..d9d660bfbc6 100644 --- a/js/watch/src/sync.ts +++ b/js/watch/src/sync.ts @@ -91,8 +91,7 @@ export class Sync { #media = new Signal<{ target: Getter }[]>([]); - // A reset invalidates waits even if a new frame restores the same reference in one turn. - #generation = new Signal(0); + #epoch = 0; #signals = new Effect(); @@ -200,11 +199,20 @@ export class Sync { // in buffered mode (typically alongside flushing the audio buffer) so the new content // plays from its own first frame instead of inheriting the previous reference. reset(): void { - this.#generation.update((generation) => generation + 1); + this.#epoch++; this.#out.reference.set(undefined); this.#late.clear(); } + /** + * Bumped by every `reset()`. A frame that waited across a bump belongs to the previous + * timeline, even when the next frame restores the same reference value. + * @internal + */ + get epoch(): number { + return this.#epoch; + } + // The PTS that should be rendering right now, derived from the reference + buffer. // Returns undefined if no frames have been received yet. now(): Time.Milli | undefined { @@ -213,13 +221,13 @@ export class Sync { return Time.Milli.sub(Time.Milli.sub(Time.Milli.now(), reference), this.#out.delay.peek()); } - /** Wait until this frame can present, returning false if a clock reset invalidates it. */ - async wait(timestamp: Time.Milli): Promise { - const generation = this.#generation.peek(); + // Sleep until it's time to render this frame. A reset holds the wait until the next frame + // re-anchors the clock, then measures the frame against that new reference. + async wait(timestamp: Time.Milli): Promise { // A zero delay still sleeps: the sleep comes from the reference, which holds an early frame // until its timestamp comes up. "instant" is the only thing that skips the wait itself. // Peek the input, not `out.instant`, which lags a same-turn change until effects flush. - if (this.in.delay.peek() === "instant") return true; + if (this.in.delay.peek() === "instant") return; const reference = this.#out.reference.peek(); if (reference === undefined) { @@ -227,9 +235,8 @@ export class Sync { } for (;;) { - if (this.#generation.peek() !== generation) return false; // Switching to "instant" wakes the sleep below, so frames parked here leave. - if (this.in.delay.peek() === "instant") return true; + if (this.in.delay.peek() === "instant") return; // Sleep until it's time to decode the next frame. // NOTE: This function runs in parallel for each frame. @@ -237,34 +244,36 @@ export class Sync { const ref = Time.Milli.sub(now, timestamp); const currentRef = this.#out.reference.peek(); - if (currentRef === undefined) return false; + if (currentRef === undefined) { + await this.#sleep(); + continue; + } const sleep = Time.Milli.add(Time.Milli.sub(currentRef, ref), this.#out.delay.peek()); - if (sleep <= 0) return true; + if (sleep <= 0) return; // Skip setTimeout for small sleeps; the timer resolution (~4ms) would overshoot. - if (sleep < 5) return true; + if (sleep < 5) return; - await this.#sleep(sleep); + if (await this.#sleep(sleep)) return; } } - // Sleeps for `ms`, or wakes early once the clock or its generation changes. - // Releases every listener either way: frames sleep once each, so a listener left on a signal - // that never changes would pile up for the life of the player. - #sleep(ms: number): Promise { + // Sleeps for `ms` (forever if omitted), or returns false early once anything the sleep was + // computed from changes. Releases every listener either way: frames sleep once each, so a + // listener left on a signal that never changes would pile up for the life of the player. + #sleep(ms?: number): Promise { return new Promise((resolve) => { - const wake = () => { + const wake = (ok: boolean) => { clearTimeout(timer); for (const dispose of disposes) dispose(); - resolve(); + resolve(ok); }; - const timer = setTimeout(wake, ms); + const timer = ms === undefined ? undefined : setTimeout(() => wake(true), ms); const disposes = [ - this.#generation.changed(wake), - this.in.delay.changed(wake), - this.#out.delay.changed(wake), - this.#out.reference.changed(wake), + this.in.delay.changed(() => wake(false)), + this.#out.delay.changed(() => wake(false)), + this.#out.reference.changed(() => wake(false)), ]; }); } diff --git a/js/watch/src/video/decoder.test.ts b/js/watch/src/video/decoder.test.ts index edeb9b435e8..62fae96644e 100644 --- a/js/watch/src/video/decoder.test.ts +++ b/js/watch/src/video/decoder.test.ts @@ -613,6 +613,44 @@ it("a shared audio clock reset cannot present a parked video frame", async () => playback.sync.reset(); await microtasks(); expect(playback.decoder.out.timestamp.peek()).toBe(Time.Milli(0)); + // The next utterance anchors the clock past the parked frame's deadline. + playback.sync.received(Time.Milli(2000)); + await microtasks(); + expect(playback.decoder.out.timestamp.peek()).toBe(Time.Milli(0)); + } finally { + playback.close(); + await microtasks(); + clear.mockRestore(); + timer.mockRestore(); + now.mockRestore(); + } +}); + +it("a reset drops a parked video frame even when the next anchor restores the reference", async () => { + const now = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1000)); + const timers: Array<() => void> = []; + const timer = spyOn(globalThis, "setTimeout").mockImplementation(((fn: () => void) => { + timers.push(fn); + return timers.length as unknown as ReturnType; + }) as unknown as typeof setTimeout); + const clear = spyOn(globalThis, "clearTimeout").mockImplementation(() => {}); + const reads: Read[] = []; + const playback = await guardedPlayback("legacy", reads); + try { + playback.enabled.set(false); + playback.delay.set(Time.Milli(1000)); + await microtasks(); + reads.push(sample(0, 0), sample(0, 100_000, 0, false)); + playback.enabled.set(true); + await microtasks(); + expect(playback.submitted).toEqual([0, 100_000]); + // In one turn, so the reference never visibly changes and the parked sleep keeps its timer. + playback.sync.reset(); + playback.sync.received(Time.Milli(0)); + await microtasks(); + for (const fn of timers.splice(0)) fn(); + await microtasks(); + expect(playback.decoder.out.timestamp.peek()).toBe(Time.Milli(0)); } finally { playback.close(); await microtasks(); diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index 6d1373cd892..033a30fd29a 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -410,9 +410,7 @@ class DecoderTrack { // Decoded frames waiting to be rendered. #buffered = new Signal([]); - // The last discontinuity count seen from the container consumer. An output callback - // captures it when the frame comes out, so a picture already parked in sync.wait - // can be dropped after the playhead moves. + // The last discontinuity count seen from the container consumer. #discontinuity = 0; // Codec description from the last configure, reused after a playhead reset. @@ -447,9 +445,10 @@ class DecoderTrack { const decoder = new VideoDecoder({ output: async (frame: VideoFrame) => { try { - // The generation this frame was decoded in. If the playhead bumps while we wait - // below, this frame belongs to the previous generation and must be dropped. - const generation = this.#discontinuity; + // The clock epoch this frame was decoded in. A reset while we wait below (this + // track's playhead moving, or a shared reset such as audio's at an utterance + // boundary) means the frame belongs to the previous timeline and must be dropped. + const epoch = this.sync.epoch; const timestamp = Time.Milli.fromMicro(frame.timestamp as Time.Micro); if (timestamp < (this.timestamp.peek() ?? 0)) { @@ -469,9 +468,9 @@ class DecoderTrack { } // Returns immediately when the latency is "instant". - const ok = await effect.race(this.sync.wait(timestamp)); + const ok = await effect.race(this.sync.wait(timestamp).then(() => true)); if (!ok) return; - if (generation !== this.#discontinuity) return; // the playhead moved while waiting + if (epoch !== this.sync.epoch) return; if (timestamp < (this.timestamp.peek() ?? 0)) { // Late frame, don't render it. @@ -684,7 +683,7 @@ class DecoderTrack { // otherwise come out above the new keyframe and late-reject it. A delta continues a group the // bump interrupted (a stale marker or a max-delay hole), so it keeps the decoder's references; // a reset would make WebCodecs require a key chunk and throw. `reset()` cannot cancel an - // output callback that already holds a frame; the generation it captured rejects that one. + // output callback that already holds a frame; the clock epoch it captured rejects that one. #resetAtKeyframe(decoder: VideoDecoder, keyframe: boolean): void { if (!this.#resetPending) return; this.#resetPending = false; diff --git a/quest/m1/watch-break-av-sync.md b/quest/m1/watch-break-av-sync.md index 3855bc406e6..956f1f5de45 100644 --- a/quest/m1/watch-break-av-sync.md +++ b/quest/m1/watch-break-av-sync.md @@ -43,10 +43,12 @@ the offset negative control on its unrelated audio-tone completeness floor Astra iteration reproduced a related shared-clock defect with mocked time: when an audio reset cleared the reference, `Sync.wait` returned normally and the video -decoder presented its parked frame. The candidate gives resets a private -generation and returns a boolean from `Sync.wait`; video discards invalidated -waits. This also covers reset and re-anchor in one microtask. Focused regressions -fail before and pass after the fix. +decoder presented its parked frame. Decided 2026-10-11 (no public API change): +`Sync.wait` stays `Promise` and holds a parked wait across a reset until +the next frame re-anchors the clock. Video drops a frame whose wait spanned a +reset, keyed on an `@internal` reset epoch (stripped from the published types), +which also covers a reset and same-value re-anchor in one turn. Focused +regressions fail before and pass after the fix. The media harness now keeps early-resume grading active until audio is running and unstalled. A moving silent preview no longer ends that grading. Existing @@ -55,8 +57,4 @@ skew and timing bounds remain unchanged. This does not yet establish the cause of the original 1.6-second reattach sample. Keep the quest until real-media evidence connects that failure to the fixed path or independently demonstrates that it is resolved. Frames already waiting for -presentation are covered; queued WebCodecs outputs need separate evidence. - -Public API candidate: `Sync.wait` returns `Promise` instead of -`Promise`; callers must honor false. Recommend this normal cancellation -result over an exception or a public generation counter. Wire: unchanged. +presentation are covered; queued WebCodecs outputs need separate evidence. \ No newline at end of file From 3def6f3979a441df1312745477b4a98467c1728d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 15:24:43 -0700 Subject: [PATCH 5/7] fix(watch): release parked waits on close and keep the reattach startup timeout Closing a Sync now wakes waits parked across a reset, which could otherwise sleep forever and keep their listeners. The resumed live wait gets the full timeout to start moving again before its catch-up window opens, as the removed startup wait did. Co-Authored-By: Claude Opus 5.5 --- js/watch/src/sync.test.ts | 14 ++++++++++++++ js/watch/src/sync.ts | 9 +++++++-- test/interop/clients/js/media.ts | 15 ++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/js/watch/src/sync.test.ts b/js/watch/src/sync.test.ts index 4cce1e76d01..a2a833d5a57 100644 --- a/js/watch/src/sync.test.ts +++ b/js/watch/src/sync.test.ts @@ -168,4 +168,18 @@ describe("wait", () => { expect(woke).toBe(true); sync.close(); }); + + it("releases a wait parked across a reset when closed", async () => { + const sync = new Sync({ delay: 10_000 as Time.Milli }); + await flush(); + const timestamp = Time.Milli.now(); + sync.received(timestamp); + + const waiting = sync.wait(timestamp); + await flush(); + sync.reset(); + await flush(); + sync.close(); + await waiting; + }); }); diff --git a/js/watch/src/sync.ts b/js/watch/src/sync.ts index d9d660bfbc6..d530da2fb65 100644 --- a/js/watch/src/sync.ts +++ b/js/watch/src/sync.ts @@ -93,6 +93,9 @@ export class Sync { #epoch = 0; + // Releases parked waits, which may otherwise sleep forever on a reference that never returns. + #closed = new Signal(false); + #signals = new Effect(); constructor(props?: Inputs) { @@ -235,8 +238,8 @@ export class Sync { } for (;;) { - // Switching to "instant" wakes the sleep below, so frames parked here leave. - if (this.in.delay.peek() === "instant") return; + // Switching to "instant" or closing wakes the sleep below, so frames parked here leave. + if (this.in.delay.peek() === "instant" || this.#closed.peek()) return; // Sleep until it's time to decode the next frame. // NOTE: This function runs in parallel for each frame. @@ -274,6 +277,7 @@ export class Sync { this.in.delay.changed(() => wake(false)), this.#out.delay.changed(() => wake(false)), this.#out.reference.changed(() => wake(false)), + this.#closed.changed(() => wake(false)), ]; }); } @@ -288,6 +292,7 @@ export class Sync { } close() { + this.#closed.set(true); this.#signals.close(); } } diff --git a/test/interop/clients/js/media.ts b/test/interop/clients/js/media.ts index ec0855e40ab..e527cec2609 100644 --- a/test/interop/clients/js/media.ts +++ b/test/interop/clients/js/media.ts @@ -195,6 +195,9 @@ const framesToMs = (frames: number) => Math.round((frames * 1000) / Pattern.FPS) * playback runs: a player delayed seconds behind live still shows a near-live preview first. * Audio must also have left preroll, just as in measurePlayback: a new preview can replace an old * one while the audio buffer is still filling, so a changed canvas alone does not prove playback. + * + * A `resumed` player gets the full timeout to start moving again, and the catch-up window opens at + * its first advancing frame. Every audible advancing pair before live is graded for A/V sync. */ async function waitForLive( publisher: Page, @@ -204,12 +207,14 @@ async function waitForLive( assertion: string, resumed = false, ): Promise<{ first: number; lag: number; after: number }> { - const since = Date.now(); + let since = Date.now(); + let started = !resumed; + const startup = since + timeoutMs; let first: number | undefined; let lag: number | undefined; let frame: number | undefined; let moved = false; - while (Date.now() < since + LATE_CATCHUP_MS) { + while (Date.now() < (started ? since + LATE_CATCHUP_MS : startup)) { throwPageErrors(publisherErrors); throwPageErrors(playerErrors); const [fixture, state] = await Promise.all([readFixtureState(publisher), readPlayerState(player)]).catch( @@ -228,6 +233,10 @@ async function waitForLive( lag = fixture.frameId - state.frameId; first ??= lag; moved = frame !== undefined && state.frameId !== frame; + if (moved && !started) { + started = true; + since = Date.now(); + } const live = reachesLive({ previous: frame, source: fixture.frameId, @@ -244,7 +253,7 @@ async function waitForLive( throw new Failure( assertion, `${moved ? "still" : "held a frame"} ${lag === undefined ? "?" : framesToMs(lag)}ms behind the fixture ` + - `${LATE_CATCHUP_MS}ms after the first frame, against a ${framesToMs(LIVE_LAG_FRAMES)}ms bound for playback`, + `${started ? LATE_CATCHUP_MS : timeoutMs}ms after the first frame, against a ${framesToMs(LIVE_LAG_FRAMES)}ms bound for playback`, ); } From 5af4041667e298018cf77ebad6b82a3dd65cd4f0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 16:12:37 -0700 Subject: [PATCH 6/7] feat(watch): let an AbortSignal release a parked Sync.wait Sync.wait takes an optional signal, matching the audio ring's wait. The video decoder passes its track's signal, so a closed track releases waits parked across a reset instead of leaving their listeners until the next anchor. Co-Authored-By: Claude Opus 5.5 --- js/watch/src/sync.test.ts | 16 ++++++++++++ js/watch/src/sync.ts | 32 ++++++++++++++++-------- js/watch/src/video/decoder.test.ts | 40 ++++++++++++++++++++++++++++++ js/watch/src/video/decoder.ts | 6 ++--- quest/m1/watch-break-av-sync.md | 6 +++-- 5 files changed, 84 insertions(+), 16 deletions(-) diff --git a/js/watch/src/sync.test.ts b/js/watch/src/sync.test.ts index a2a833d5a57..2236ee1a91c 100644 --- a/js/watch/src/sync.test.ts +++ b/js/watch/src/sync.test.ts @@ -169,6 +169,22 @@ describe("wait", () => { sync.close(); }); + it("releases a wait parked across a reset when its signal aborts", async () => { + const sync = new Sync({ delay: 10_000 as Time.Milli }); + await flush(); + const timestamp = Time.Milli.now(); + sync.received(timestamp); + + const controller = new AbortController(); + const waiting = sync.wait(timestamp, controller.signal); + await flush(); + sync.reset(); + await flush(); + controller.abort(); + await waiting; + sync.close(); + }); + it("releases a wait parked across a reset when closed", async () => { const sync = new Sync({ delay: 10_000 as Time.Milli }); await flush(); diff --git a/js/watch/src/sync.ts b/js/watch/src/sync.ts index d530da2fb65..17f3cafbfe9 100644 --- a/js/watch/src/sync.ts +++ b/js/watch/src/sync.ts @@ -224,9 +224,13 @@ export class Sync { return Time.Milli.sub(Time.Milli.sub(Time.Milli.now(), reference), this.#out.delay.peek()); } - // Sleep until it's time to render this frame. A reset holds the wait until the next frame - // re-anchors the clock, then measures the frame against that new reference. - async wait(timestamp: Time.Milli): Promise { + /** + * Sleep until it's time to render this frame, or until `signal` aborts. + * + * A reset holds the wait until the next frame re-anchors the clock, then measures the frame + * against that new reference. Aborting resolves the wait early and releases it. + */ + async wait(timestamp: Time.Milli, signal?: AbortSignal): Promise { // A zero delay still sleeps: the sleep comes from the reference, which holds an early frame // until its timestamp comes up. "instant" is the only thing that skips the wait itself. // Peek the input, not `out.instant`, which lags a same-turn change until effects flush. @@ -238,8 +242,8 @@ export class Sync { } for (;;) { - // Switching to "instant" or closing wakes the sleep below, so frames parked here leave. - if (this.in.delay.peek() === "instant" || this.#closed.peek()) return; + // Switching to "instant", closing, or aborting wakes the sleep below, so frames parked here leave. + if (this.in.delay.peek() === "instant" || this.#closed.peek() || signal?.aborted) return; // Sleep until it's time to decode the next frame. // NOTE: This function runs in parallel for each frame. @@ -248,7 +252,7 @@ export class Sync { const currentRef = this.#out.reference.peek(); if (currentRef === undefined) { - await this.#sleep(); + await this.#sleep(undefined, signal); continue; } @@ -258,14 +262,15 @@ export class Sync { // Skip setTimeout for small sleeps; the timer resolution (~4ms) would overshoot. if (sleep < 5) return; - if (await this.#sleep(sleep)) return; + if (await this.#sleep(sleep, signal)) return; } } - // Sleeps for `ms` (forever if omitted), or returns false early once anything the sleep was - // computed from changes. Releases every listener either way: frames sleep once each, so a - // listener left on a signal that never changes would pile up for the life of the player. - #sleep(ms?: number): Promise { + // Sleeps for `ms` (forever if undefined), or returns false early once anything the sleep was + // computed from changes or `signal` aborts. Releases every listener either way: frames sleep + // once each, so a listener left on a signal that never changes would pile up for the life of + // the player. + #sleep(ms: number | undefined, signal?: AbortSignal): Promise { return new Promise((resolve) => { const wake = (ok: boolean) => { clearTimeout(timer); @@ -279,6 +284,11 @@ export class Sync { this.#out.reference.changed(() => wake(false)), this.#closed.changed(() => wake(false)), ]; + if (signal) { + const abort = () => wake(false); + signal.addEventListener("abort", abort, { once: true }); + disposes.push(() => signal.removeEventListener("abort", abort)); + } }); } diff --git a/js/watch/src/video/decoder.test.ts b/js/watch/src/video/decoder.test.ts index 62fae96644e..d18bc4775be 100644 --- a/js/watch/src/video/decoder.test.ts +++ b/js/watch/src/video/decoder.test.ts @@ -626,6 +626,46 @@ it("a shared audio clock reset cannot present a parked video frame", async () => } }); +it("closing the track releases a video wait parked across a reset", async () => { + const now = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1000)); + const timer = spyOn(globalThis, "setTimeout").mockImplementation( + (() => 1 as unknown as ReturnType) as unknown as typeof setTimeout, + ); + const clear = spyOn(globalThis, "clearTimeout").mockImplementation(() => {}); + const reads: Read[] = []; + const playback = await guardedPlayback("legacy", reads); + const waits: Promise[] = []; + const original = playback.sync.wait.bind(playback.sync); + const wait = spyOn(playback.sync, "wait").mockImplementation((timestamp, signal) => { + const waiting = original(timestamp, signal); + waits.push(waiting); + return waiting; + }); + try { + playback.enabled.set(false); + playback.delay.set(Time.Milli(1000)); + await microtasks(); + reads.push(sample(0, 0), sample(0, 100_000, 0, false)); + playback.enabled.set(true); + await microtasks(); + expect(playback.submitted).toEqual([0, 100_000]); + playback.sync.reset(); + await microtasks(); + expect(waits.map((waiting) => Bun.peek.status(waiting))).toContain("pending"); + // No frame re-anchors the shared clock, but the closed track must not leave its wait behind. + playback.enabled.set(false); + await microtasks(); + expect(waits.map((waiting) => Bun.peek.status(waiting))).not.toContain("pending"); + } finally { + wait.mockRestore(); + playback.close(); + await microtasks(); + clear.mockRestore(); + timer.mockRestore(); + now.mockRestore(); + } +}); + it("a reset drops a parked video frame even when the next anchor restores the reference", async () => { const now = spyOn(Time.Milli, "now").mockReturnValue(Time.Milli(1000)); const timers: Array<() => void> = []; diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index 033a30fd29a..a500500a653 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -467,9 +467,9 @@ class DecoderTrack { this.frame.set(frame.clone()); } - // Returns immediately when the latency is "instant". - const ok = await effect.race(this.sync.wait(timestamp).then(() => true)); - if (!ok) return; + // Returns immediately when the latency is "instant". Closing the track releases it. + await this.sync.wait(timestamp, effect.abort); + if (effect.abort.aborted) return; if (epoch !== this.sync.epoch) return; if (timestamp < (this.timestamp.peek() ?? 0)) { diff --git a/quest/m1/watch-break-av-sync.md b/quest/m1/watch-break-av-sync.md index 956f1f5de45..e4e2af33f72 100644 --- a/quest/m1/watch-break-av-sync.md +++ b/quest/m1/watch-break-av-sync.md @@ -20,7 +20,7 @@ the reachable pre-gap GOP by design, per #4914 and #5158) or the player's sync re-anchors one track and not the other. Fix it at that cause, and assert the offset in the media check. -Public API: none expected. Wire: none. +Public API: `Sync.wait` gains an optional `AbortSignal`. Wire: none. ### Investigation @@ -47,7 +47,9 @@ decoder presented its parked frame. Decided 2026-10-11 (no public API change): `Sync.wait` stays `Promise` and holds a parked wait across a reset until the next frame re-anchors the clock. Video drops a frame whose wait spanned a reset, keyed on an `@internal` reset epoch (stripped from the published types), -which also covers a reset and same-value re-anchor in one turn. Focused +which also covers a reset and same-value re-anchor in one turn. `Sync.wait` +gained an optional `AbortSignal` (additive), which the decoder passes so a +closed track releases its parked waits. Focused regressions fail before and pass after the fix. The media harness now keeps early-resume grading active until audio is running From fdb210a265231b2ad0e4a300508ae51210e004b4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 11 Oct 2026 16:18:44 -0700 Subject: [PATCH 7/7] fix(watch): check the same abort signal a parked video wait used A rerun of the track effect installs a new controller before the wait's continuation runs, so reading effect.abort again could see a fresh, un-aborted signal and present a frame from the ended subscription. Co-Authored-By: Claude Opus 5.5 --- js/watch/src/video/decoder.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/js/watch/src/video/decoder.ts b/js/watch/src/video/decoder.ts index a500500a653..a1f915fee1f 100644 --- a/js/watch/src/video/decoder.ts +++ b/js/watch/src/video/decoder.ts @@ -468,8 +468,10 @@ class DecoderTrack { } // Returns immediately when the latency is "instant". Closing the track releases it. - await this.sync.wait(timestamp, effect.abort); - if (effect.abort.aborted) return; + // Capture the signal: a rerun replaces `effect.abort` before this continuation runs. + const signal = effect.abort; + await this.sync.wait(timestamp, signal); + if (signal.aborted) return; if (epoch !== this.sync.epoch) return; if (timestamp < (this.timestamp.peek() ?? 0)) {