From 016801d6472d68f7145e7aa729d65eb7c460cc96 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 20 Nov 2026 17:12:52 -0800 Subject: [PATCH] fix(watch): a buffered audio head waits at most the max delay The hold released only as timestamps advanced, so a publisher repeating one timestamp grew it without bound. Release it once it has waited the max delay too, as Rust's gate bounds what it keeps by arrival time. Co-Authored-By: Claude Opus 5.5 --- js/watch/src/audio/order.test.ts | 23 ++++++++++++++++++++++- js/watch/src/audio/order.ts | 14 ++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/js/watch/src/audio/order.test.ts b/js/watch/src/audio/order.test.ts index 86eeffd9409..2b97cb8a289 100644 --- a/js/watch/src/audio/order.test.ts +++ b/js/watch/src/audio/order.test.ts @@ -1,5 +1,6 @@ -import { describe, expect, it } from "bun:test"; +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; import { Duration } from "@moq/clock"; +import { Manual } from "@moq/clock/test"; import { Order } from "./order"; type Read = { group: bigint; frame: { timestamp: Duration } }; @@ -7,6 +8,14 @@ const read = (group: bigint, timestamp: number): Read => ({ group, frame: { time const timestamps = (reads: Read[]) => reads.map(({ frame }) => frame.timestamp.asMicros()); describe("Order", () => { + let clock: Manual; + let installed: { close(): void }; + beforeEach(() => { + clock = new Manual(); + installed = clock.install(); + }); + afterEach(() => installed.close()); + it("releases a held head sorted once a newer group arrives", () => { const order = new Order(() => ({ floor: 100, maxDelay: 1000 })); for (const t of [40, 20, 0]) expect(order.push(read(BigInt(t / 20), t))).toEqual([]); @@ -80,6 +89,18 @@ describe("Order", () => { expect(timestamps(order.push(read(5n, 140)))).toEqual([0, 20, 60, 140]); }); + // A publisher repeating one timestamp never advances the span or runs past the floor, so only the + // time the head has waited releases it, however many frames arrived meanwhile. + it("releases a head once it has waited the max delay", () => { + const order = new Order(() => ({ floor: 100, maxDelay: 1000 })); + for (let i = 0; i < 100; i++) expect(order.push(read(5n, 20))).toEqual([]); + clock.jump(Duration.micros(999)); + expect(order.push(read(5n, 20))).toEqual([]); + clock.jump(Duration.micros(1)); + expect(order.push(read(5n, 20))).toHaveLength(102); + expect(timestamps(order.push(read(5n, 20)))).toEqual([20]); + }); + it("holds nothing live", () => { const order = new Order(undefined); expect(timestamps(order.push(read(1n, 20)))).toEqual([20]); diff --git a/js/watch/src/audio/order.ts b/js/watch/src/audio/order.ts index aa10e752f39..0d7edff11db 100644 --- a/js/watch/src/audio/order.ts +++ b/js/watch/src/audio/order.ts @@ -1,4 +1,4 @@ -import type { Duration } from "@moq/clock"; +import { type Duration, type Instant, now } from "@moq/clock"; /** * Puts one subscription's frames in timestamp order for the audio decoder. @@ -13,7 +13,8 @@ import type { Duration } from "@moq/clock"; * A buffered subscription holds its head and releases it sorted. The head is complete once a newer * group than the first arrives, or once the first group alone runs past the floor: the ring could not * start playing before holding that much, so waiting for the head costs no startup latency. It is - * also released once it spans the subscription's max delay, since nothing older is served. After + * also released once it spans the subscription's max delay, since nothing older is served, or once it + * has waited that long, which bounds what it keeps by arrival time whatever the timestamps say. After * that, and throughout when not holding, a frame older than one already decoded is not admitted. */ export class Order { @@ -21,8 +22,8 @@ export class Order #hold: (() => { floor: number; maxDelay: number }) | undefined; // The head, in arrival order. #held: T[] = []; - // The group the head started with, and its own first frame. - #first?: { group: bigint; timestamp: number }; + // The group the head started with, its own first frame, and when that arrived. + #first?: { group: bigint; timestamp: number; arrived: Instant }; // The oldest and newest held timestamps. #span?: { oldest: number; newest: number }; // The newest frame admitted since the decoder was last reset. @@ -40,7 +41,7 @@ export class Order const { group, frame } = read; const timestamp = frame.timestamp.asMicros(); - this.#first ??= { group, timestamp }; + this.#first ??= { group, timestamp, arrived: now() }; if (group > this.#first.group) return [...this.flush(), read]; this.#held.push(read); @@ -49,7 +50,8 @@ export class Order this.#span = { oldest, newest }; const ran = group === this.#first.group && timestamp - this.#first.timestamp > hold.floor; - if (ran || newest - oldest >= hold.maxDelay) return this.flush(); + const waited = now().since(this.#first.arrived).asMicros(); + if (ran || newest - oldest >= hold.maxDelay || waited >= hold.maxDelay) return this.flush(); return []; }