diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 24e7affa1b2..6f8cb447585 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -12,6 +12,7 @@ import { type AnnounceBroadcast, AnnounceInit, AnnounceOk, encodeAnnounceBroadca import { Group } from "./group.ts"; import { Probe } from "./probe.ts"; import { StreamId } from "./stream.ts"; +import { encodeSubscribeResponse, SubscribeEnd } from "./subscribe.ts"; import { SUBSCRIBE_SETUP_TIMEOUT_MS, Subscriber } from "./subscriber.ts"; import { Track, TrackInfo } from "./track.ts"; import { Version } from "./version.ts"; @@ -862,6 +863,8 @@ interface FakeStream { written: Uint8Array[]; // Hands the stream to the subscriber, for an open the session was told to park. release: () => void; + // Fails every later write with `reason`, as a peer's STOP_SENDING does. Resolves on the first. + stop: (reason: unknown) => Promise; } // A session whose streams the test answers by hand and that never fails them on its own, so @@ -884,6 +887,7 @@ function fakeSession(park: number[] = [], stall: number[] = []) { let onFinish!: () => void; let onWrite!: () => void; let release!: () => void; + let stopped: { reason: unknown; refused: () => void } | undefined; const reading = new Promise((resolve) => (onRead = resolve)); const aborted = new Promise((resolve) => (onAbort = resolve)); const finished = new Promise((resolve) => (onFinish = resolve)); @@ -902,6 +906,10 @@ function fakeSession(park: number[] = [], stall: number[] = []) { const written: Uint8Array[] = []; const writable = new WritableStream({ write: (chunk) => { + if (stopped) { + stopped.refused(); + return Promise.reject(stopped.reason); + } written.push(new Uint8Array(chunk)); onWrite(); return new Promise((resolve) => { @@ -921,7 +929,11 @@ function fakeSession(park: number[] = [], stall: number[] = []) { }); const opened = new Promise((resolve) => (release = () => resolve({ readable, writable }))); if (!park.includes(streams.length)) release(); - streams.push({ inbound, reading, aborted, finished, writing, written, release }); + const stop = (reason: unknown) => + new Promise((refused) => { + stopped = { reason, refused }; + }); + streams.push({ inbound, reading, aborted, finished, writing, written, release, stop }); count.set(streams.length); return opened; }, @@ -1699,6 +1711,79 @@ test("a publisher FIN without SUBSCRIBE_END still violates an open subscription" } }); +// A publisher that finishes the track stops reading its subscribe stream, so a SUBSCRIBE_UPDATE +// written then fails. The track still ends with the publisher's finish; a reset still fails it. +test.each([ + ["lite-05 finish", Version.DRAFT_05, "finish"], + ["lite-06 finish", Version.DRAFT_06, "finish"], + ["lite-07 finish", Version.DRAFT_07, "finish"], + ["lite-07 reset", Version.DRAFT_07, "reset"], +] as const)("an update the publisher stopped reading defers to its %s", async (_, version, end) => { + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, HopSchema.parse(1n)); + const broadcast = subscriber.consume(Path.from("room")); + const track = broadcast.track("video").subscribe(); + try { + await opened(0); + await answerTrackInfo(streams[0], version); + const stream = await opened(1); + await stream.reading; + + const stopped = Object.assign(new Error("The stream was aborted by the remote server"), { + source: "stream", + streamErrorCode: StreamCode.Cancel, + }); + const refused = stream.stop(stopped); + track.update({ priority: 1 }); + await refused; + await drainReactions(); + expect(track.closed.peek()).toBeUndefined(); + + if (end === "finish") { + const chunks: Uint8Array[] = []; + const writer = new Writer( + new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), + version, + ); + await encodeSubscribeResponse(writer, { end: new SubscribeEnd(0) }, version); + for (const chunk of chunks) stream.inbound.enqueue(chunk); + stream.inbound.close(); + expect(await track.closed).toBeNull(); + } else { + stream.inbound.error( + Object.assign(new Error("reset"), { source: "stream", streamErrorCode: StreamCode.NotFound }), + ); + const err = await track.closed; + expect(err).toBeInstanceOf(StreamError); + expect((err as StreamError).code).toBe(StreamCode.NotFound); + } + } finally { + track.close(); + broadcast.close(); + subscriber.close(); + } +}); + +// Only a failed write defers to the responses: an update the wire can't carry still fails the +// track. Lite-05 sends the start group plus one, past the largest u53 here. +test("an update whose bound the wire can't carry fails the track", async () => { + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, Version.DRAFT_05, HopSchema.parse(1n)); + const broadcast = subscriber.consume(Path.from("room")); + const track = broadcast.track("video").subscribe(); + try { + await opened(0); + await answerTrackInfo(streams[0], Version.DRAFT_05); + await (await opened(1)).reading; + track.update({ groups: { start: { included: Number.MAX_SAFE_INTEGER } } }); + expect(await track.closed).toBeInstanceOf(RangeError); + } finally { + track.close(); + broadcast.close(); + subscriber.close(); + } +}); + test("a retired subscribe response type closes the session", async () => { const { quic, streams, opened, closes } = fakeSession(); const subscriber = new Subscriber(quic, Version.DRAFT_07, HopSchema.parse(1n)); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 797b686627f..d4bdfef4a43 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -23,7 +23,7 @@ import { Cost, type Hop, MAX_HOPS, type Route, routesEqual, UNKNOWN_HOP } from " import { groupBounds, hiddenBelow, scopeCaptures, scopeHead, scopeOverlaps } from "../internal.ts"; import * as Path from "../path.ts"; import { sendOrder } from "../priority.ts"; -import { type OpenOptions, type Reader, Stream } from "../stream.ts"; +import { type OpenOptions, type Reader, Stream, Writer } from "../stream.ts"; import { TAIL_GRACE_MS, Tail } from "../tail.ts"; import * as Time from "../time.ts"; import type * as track from "../track.ts"; @@ -768,8 +768,8 @@ export class Subscriber { // Terminal conditions (stream end, track close, a failed subscription update) settle at most // once; race them into one stable promise so the demand loop doesn't re-subscribe each pass. - // Updates stop quietly at the FIN, which can land before the responses ahead of it are - // decoded, so only their failure is terminal on its own. + // Updates stop quietly at the FIN or a failed write, either of which can land before the + // responses ahead of it are decoded, so only a refused update is terminal on its own. const terminal: PromiseLike[] = [closed, producer.closed]; if (subscriptionUpdates !== undefined) terminal.push(subscriptionUpdates.then(() => closed)); const done = race(terminal); @@ -1237,7 +1237,7 @@ export class Subscriber { /** * Send SUBSCRIBE_UPDATE messages whenever the track's aggregate subscription changes. * - * Resolves cleanly when the stream or track closes, so the caller can include + * Resolves cleanly when the stream or track closes, or a write fails, so the caller can include * this in a race without leaving a dangling pending write that would * become an unhandled rejection if the user calls update after close. * @@ -1293,7 +1293,17 @@ export class Subscriber { startGroup: current.groups?.start === undefined ? undefined : bounds.start, endGroup: inclusiveGroupEnd(bounds.end), }); - await update.encode(stream.writer, this.version); + // Encoded apart from the write, so a bound the wire can't carry still fails the track. + const encoded: Uint8Array[] = []; + const buffer = new WritableStream({ write: (chunk) => void encoded.push(chunk.slice()) }); + await update.encode(new Writer(buffer, this.version), this.version); + // A failed write means the publisher stopped reading, as it does once it finishes the + // track. The responses decide how the track ends, cleanly at the FIN or with a reset's error. + try { + for (const chunk of encoded) await stream.writer.write(chunk); + } catch { + return; + } lastSent = { ...current }; console.debug(`subscribe update: id=${id} broadcast=${broadcast} track=${track.name}`); } diff --git a/quest/m1/test-flakes-2/README.md b/quest/m1/test-flakes-2/README.md index 1a33b9d966b..f30b3e6fd2e 100644 --- a/quest/m1/test-flakes-2/README.md +++ b/quest/m1/test-flakes-2/README.md @@ -35,7 +35,6 @@ Public API: none. Wire: none. ## Required -- [Update after finish](/quest/m1/test-flakes-2/update-after-finish.md) - a subscribe update racing a publisher's finish ends the track cleanly in JS and Rust, not with the write's error - [Auth refresh mocks time](/quest/m1/test-flakes-2/auth-refresh-mock-time.md) - moq-relay's auth refresh and fixed-lease tests measure a grant's life on one mocked clock - [uring teardown close](/quest/m1/test-flakes-2/uring-teardown-close.md) - moq-uring's published-close teardown test holds under load, fixed at its cause - [WASM IETF read](/quest/m1/test-flakes-2/wasm-ietf-read.md) - the WASM suite's IETF read case reads whole groups on a loaded runner, fixed at the burst or the assertion as the evidence says diff --git a/quest/m1/test-flakes-2/update-after-finish.md b/quest/m1/test-flakes-2/update-after-finish.md deleted file mode 100644 index b8ad07a6b52..00000000000 --- a/quest/m1/test-flakes-2/update-after-finish.md +++ /dev/null @@ -1,39 +0,0 @@ -# [S] A subscribe update racing a finished track ends it cleanly - -## Goal - -When a publisher finishes a track while the subscriber is writing -SUBSCRIBE_UPDATE on that track's subscribe stream, the subscriber's track ends -cleanly with the publisher's finish, in JS and Rust, instead of failing with the -write's stream error. A real reset of the subscribe stream still fails the -track with the reset's error. - -## Plan - -Seen in the real-browser rename media case on #5635. `@moq/publish` finished -the old `video` track after a rename. The relay closed the subscribe stream -while the player wrote a SUBSCRIBE_UPDATE on it. The write threw "The stream -was aborted by the remote server". In `js/net/src/lite/subscriber.ts`, the -rejected `#runSubscriptionUpdates` won the `terminal` race, so the track closed -with that error. Watch logged a spawn error, and the media check failed on the -console error. It reproduced in one of five local runs on a loaded machine. - -Decided 2026-10-16 (recommended options; the paper trail is in the plan PR): - -- A failed update write defers to the subscribe stream's own outcome. If the - responses end cleanly, the track ends cleanly; if they reject, that error - wins. No retry and no timeout. -- Fix JS and Rust together. `rs/moq-net`'s `lite/subscriber.rs` serve loop - (`ServeMode::Select`) hands the track back with the flush error. Confirm - whether the same race fails a finished track there, and give it the same - rule. -- Regression tests in both languages: a publisher that finishes the track and - stops reading while an update is in flight. The subscriber's track ends - cleanly, and a reset still surfaces its error. Run - `just test interop --all`, since this is session behavior. - -Public API: none. Wire: none. - -## Related - -- [Browser follows a renamed track](/quest/m1/browser-rename-check.md) - the media check this race failed diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index d0a52c4e38c..29b94915aa0 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -71,12 +71,19 @@ impl Writer { } /// Poll until the write buffer has fully hit the stream. + /// + /// A failed stream never takes another byte, so a write error drops the buffer too. pub fn poll_flush(&mut self, cx: &mut Context<'_>) -> Poll> { while self.flushed < self.buffer.len() { let stream = self.stream.as_mut().unwrap(); - let n = ready!(stream.poll_write(cx, &self.buffer[self.flushed..])) - .map_err(|err| self.version.transport_error(err))?; - self.flushed += n; + match ready!(stream.poll_write(cx, &self.buffer[self.flushed..])) { + Ok(n) => self.flushed += n, + Err(err) => { + self.buffer.clear(); + self.flushed = 0; + return Poll::Ready(Err(self.version.transport_error(err))); + } + } } self.buffer.clear(); self.flushed = 0; diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 82536cd993f..aadf5039bbc 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -2883,6 +2883,79 @@ mod tests { )); } + /// A publisher that finishes the track stops reading its subscribe stream, so a + /// SUBSCRIBE_UPDATE written then fails. The track still ends with the publisher's + /// finish; a reset still hands it back with the reset's error. + #[moq_net_sim::test] + async fn an_update_the_publisher_stopped_reading_defers_to_its_end() { + use crate::transport::poll::{RecvStream as _, SendStream as _, Session as _}; + for reset in [false, true] { + let version = Version::Lite06; + let (session, mut peer) = crate::transport::memory::pair(Default::default()); + let subscriber = Subscriber::new(SubscriberConfig { + runtime: crate::time::Clock::sim(), + session, + origin: origin::Config::new(crate::Hop::new(1).unwrap()).produce(), + recv_bandwidth: None, + version, + peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Binding::InBand), + peer_hop: None, + cost: None, + going_away: Default::default(), + auth: crate::auth::Handle::new(false), + }); + let serve = TrackServe { + validity: Default::default(), + subscriber, + path: Path::new("room").to_owned(), + epoch: None, + name: "video".into(), + }; + let broadcast = crate::broadcast::Info::new().produce(); + let mut serving = ServeLoop::new( + &serve, + broadcast.reserve_track("video").unwrap(), + Default::default(), + None, + ); + let mut reader = serving.serving.subscribe(None); + assert!(serving.poll(&serve, &kio::Waiter::noop()).is_pending()); + assert!(matches!(serving.sub, Sub::Active(_)), "the SUBSCRIBE is on the wire"); + + // The publisher ends the subscription and stops reading the stream. + let (mut send, mut recv) = std::future::poll_fn(|cx| peer.poll_accept_bi(cx)).await.unwrap(); + recv.stop(crate::StreamError::Cancel.to_code()); + if reset { + send.reset(crate::StreamError::NotFound.to_code()); + } else { + let mut end = Vec::new(); + lite::SubscribeResponse::End(lite::SubscribeEnd { group: 0, streams: 0 }) + .encode(&mut crate::coding::Encoder::new(&mut end, version.into()), version) + .unwrap(); + std::future::poll_fn(|cx| send.poll_write(cx, &end)).await.unwrap(); + send.finish().unwrap(); + } + + // The update races it, and its write fails. + reader.update(Subscription::default().with_priority(1)).unwrap(); + let gate = serve.gate(); + let mut running = TrackServeRun { + serve, + state: TrackRunState::Serve(serving), + gate, + }; + assert!(kio::Task::poll(&mut running, &kio::Waiter::noop()).is_ready()); + let end = reader.recv_group().now_or_never().expect("the track ended").map(|_| ()); + match reset { + false => assert!(matches!(end, Ok(())), "{end:?}"), + true => assert!( + matches!(end, Err(Error::Stream(crate::StreamError::NotFound))), + "{end:?}" + ), + } + } + } + /// Mid-group demand: a subscriber resuming at frame 3 of group 5, capped by a later /// bound in the same group. fn mid_group_demand() -> Subscription { @@ -5530,12 +5603,11 @@ impl ServeLoop { if let Sub::Active(active) = &mut self.sub { match active.stream.writer.poll_flush(&mut cx) { Poll::Ready(Ok(())) => {} + // The publisher stopped reading, as it does once it finishes the + // track. The update is dropped, and the responses decide how the + // subscription ends: cleanly at the FIN, or with a reset's error. Poll::Ready(Err(err)) => { - // The stream is broken; drop it (the writer resets) and - // hand the track back. - serve.subscriber.remove_subscribe(active.id); - self.sub = Sub::None; - return Poll::Ready(ServeEnd::GiveBack(err)); + tracing::debug!(track = %serve.name, %err, "subscribe update dropped") } Poll::Pending => return Poll::Pending, }