Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion js/net/src/group.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from "bun:test";
import { Manual, Milli, Nano, yieldNow } from "@moq/clock";
import { Signal } from "@moq/signals";
import { FrameTooLarge, StreamCode, StreamError } from "./error.ts";
import { FrameTooLarge, ProtocolViolation, StreamCode, StreamError } from "./error.ts";
import { encodable } from "./flate/group.ts";
import { Producer } from "./group.ts";
import { hooks, type Replacement } from "./internal.ts";
Expand Down Expand Up @@ -511,6 +511,58 @@ test("done ignores replayed frames an adopted copy buffers", async () => {
producer.finish();
});

test("a resumed read refuses a replacement that ends before the frames already read", async () => {
for (const repeated of [2, 1]) {
const producer = new Producer(0n);
const consumer = producer.mirror();
const replacement = new Producer(0n);
for (const text of ["zero", "one"].slice(0, repeated)) replacement.writeFrame(textFrame(text));
replacement.finish();
const failed = new Error("route failed");
hooks.recoverGroup(consumer, {
changed: new Signal(false),
wants: (error) => error === failed,
replace: async () => ({
group: replacement.mirror(),
expiry: { expired: () => false, changed: [] },
adopt: () => {},
discard: () => {},
}),
});
producer.writeFrame(textFrame("zero"));
producer.writeFrame(textFrame("one"));
expect(await consumer.readString()).toBe("zero");
expect(await consumer.readString()).toBe("one");
producer.abort(failed);
if (repeated === 2) expect(await consumer.readFrame()).toBeUndefined();
else await expect(consumer.readFrame()).rejects.toBeInstanceOf(ProtocolViolation);
}
});

test("a resumed read of a copy that held nothing takes the replacement's end", async () => {
// The copy starts past the group's end, so it proves no frame there.
const producer = new Producer(0n);
hooks.startGroup(producer, 3);
const consumer = producer.mirror();
const replacement = new Producer(0n);
replacement.writeFrame(textFrame("zero"));
replacement.writeFrame(textFrame("one"));
replacement.finish();
const failed = new Error("route failed");
hooks.recoverGroup(consumer, {
changed: new Signal(false),
wants: (error) => error === failed,
replace: async () => ({
group: replacement.mirror(),
expiry: { expired: () => false, changed: [] },
adopt: () => {},
discard: () => {},
}),
});
producer.abort(failed);
expect(await consumer.readFrame()).toBeUndefined();
});

test("buffered frames are still readable after the group closes", async () => {
const { producer, consumer } = pair(0n);
producer.writeFrame(textFrame("a"));
Expand Down
15 changes: 14 additions & 1 deletion js/net/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,8 @@ export class Consumer {
#attempt?: Attempt;
// The frame a replacement copy resumes at: frames below it were read from an earlier copy.
#skip = 0;
// How far the copies read before held frames, which the copy read now must not end short of.
#held = 0;
// Sticky failure, set once, including a budget that gave up on the group.
#terminal?: Error;
#verdict = new Once<Error | null>();
Expand Down Expand Up @@ -1397,6 +1399,9 @@ export class Consumer {
return;
}
replacement.adopt();
// Only a frame it holds proves where it reaches; an empty copy proves nothing past its start.
const total = this.#state.total.peek();
if (total > this.#state.offset) this.#held = Math.max(this.#held, total);
// The copy left behind stops teeing into the state this consumer read.
if (this.#state.closed.peek() === undefined) this.#state.closed.set(null);
this.#arrived ??= this.#state.arrived;
Expand Down Expand Up @@ -1598,11 +1603,19 @@ export class Consumer {
if (closed !== undefined) {
// The copy finished the group: a replacement asked for meanwhile is moot.
this.#abandon();
// Unless it finished before frames an earlier copy delivered, so the copies
// disagree on where the group ends.
if (this.#state.total.peek() < this.#held) {
this.#terminal = new ProtocolViolation(`group ${this.sequence} ended before frames already read`);
if (this.#verdict.peek() === undefined) this.#verdict.set(this.#terminal);
this.#closed?.refresh();
throw this.#terminal;
}
this.#closed?.refresh();
return;
}

// Nothing buffered and the group is still open: this wait is the stall the
// Nothing buffered and the group is still open: this wait is the
// drift budget bounds, and the only place it applies.
if (this.#expire()) continue;
// A stall while a newer route serves looks there too.
Expand Down
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ moves what remains of them once their claims merge.
- [moq-mux reads only the base layer's H.265 SPS](/quest/m1/h265-base-layer-sps.md) - a layered MV-HEVC or SHVC stream imports as its base layer instead of misreading an enhancement-layer SPS, which still reaches the track
- [A cancelled dispatch lane stops its phase promptly](/quest/m1/dispatch-cancel-race.md) - a TERM landing just before a lane forks its phase's command still stops that command, instead of waiting out the phase
- [Lifecycles are explicit state](/quest/m1/explicit-state/README.md) - every lifecycle the 2026-11-07 audit found implicit is one enum or union, fixing the bugs in the 11 lifecycles it redesigns, with review and agent rules against new ones
- [Copies of one group identity must agree beyond their bytes](/quest/m1/copy-identity.md) - copies that differ in timestamp, object fields, or frame count fail as a protocol violation
- [Session auth grants and replies stay consistent and bounded](/quest/m1/auth-stream-bounds.md) - an AUTH_ERROR revokes its grant, queued grants and retiring tokens are bounded, and dropping queued requests cannot deadlock
- [Relay settings and admin input that cannot work are refused](/quest/m1/relay-config-checks.md) - clustered stats without a node name, pin without workers, repeated filter keys, and unknown transports fail loud
- [Track drop runs are forgotten below the floor](/quest/m1/drop-runs-prune.md) - a track's dropped-group record stays proportional to its window in Rust and JS
Expand Down
13 changes: 0 additions & 13 deletions quest/m1/copy-identity.md

This file was deleted.

34 changes: 32 additions & 2 deletions rs/moq-net/src/model/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,31 @@ pub struct Consumer {
recover: Option<Box<(super::resume::Recover, u64)>>,
}

/// One copy's read of a frame, as [`agree`] compares it.
type Read<'a> = (
&'a kio::Consumer<GroupState>,
&'a Info,
Option<&'a super::object::Fields>,
);

/// Whether two copies' reads of one frame carry the same timestamp and object properties,
/// where both copies hold them: timestamps that may be send times differ per copy, and only
/// a copy whose Object IDs arrive with its frames says a frame without fields has none.
fn agree(a: Read<'_>, b: Read<'_>) -> bool {
fn properties(fields: Option<&super::object::Fields>) -> Option<&super::object::Properties> {
fields
.map(|fields| &fields.properties)
.filter(|properties| **properties != super::object::Properties::default())
}
fn copy(state: &kio::Consumer<GroupState>) -> (bool, bool) {
let state = state.read();
(state.unchecked, state.wire().is_some())
}
let ((a_unchecked, a_wire), (b_unchecked, b_wire)) = (copy(a.0), copy(b.0));
(a_unchecked || b_unchecked || a.1.timestamp == b.1.timestamp)
&& (!a_wire || !b_wire || properties(a.2) == properties(b.2))
}

impl std::ops::Deref for Consumer {
type Target = Info;

Expand Down Expand Up @@ -1623,8 +1648,13 @@ impl Consumer {
if frame.fields.as_ref().is_some_and(|fields| fields.id != *index) {
return Poll::Ready(Err(Error::Unsupported));
}
// Same name, same content: a different size is the routes disagreeing.
if frame.info.size != self.info.size || frame.inflated != self.inflated {
// Same name, same content: anything a reader sees differing is the routes disagreeing.
if frame.info.size != self.info.size
|| frame.inflated != self.inflated
|| !agree(
(&self.state, &self.info, self.fields.as_deref()),
(&frame.state, &frame.info, frame.fields.as_deref()),
) {
return Poll::Ready(Err(Error::ProtocolViolation));
}
recover.adopt(&replacement);
Expand Down
Loading