diff --git a/js/net/src/lite/announce_stream.test.ts b/js/net/src/lite/announce_stream.test.ts index 2cb882345d..ecd68ad879 100644 --- a/js/net/src/lite/announce_stream.test.ts +++ b/js/net/src/lite/announce_stream.test.ts @@ -1,5 +1,8 @@ import { expect, test } from "bun:test"; +import * as Instance from "../instance.ts"; import { createMockTransportPair } from "../mock.ts"; +import * as Path from "../path.ts"; +import { ownClaim } from "./announce.ts"; import { AnnounceStream } from "./announce_stream.ts"; import { Version } from "./version.ts"; @@ -51,3 +54,72 @@ test("a failed send breaks the stream", async () => { server.close(); } }); + +// Each uni stream `quic` opens from now on delivers its bytes and FIN, but its close never +// completes: the peer never acknowledges the FIN. +function holdFins(quic: WebTransport) { + const create = quic.createUnidirectionalStream.bind(quic); + quic.createUnidirectionalStream = (async (options?: WebTransportSendStreamOptions) => { + const inner = (await create(options)).getWriter(); + return new WritableStream({ + write: (chunk) => inner.write(chunk), + close: () => { + void inner.close(); + return new Promise(() => {}); + }, + }); + }) as WebTransport["createUnidirectionalStream"]; +} + +// Reads the peer's one incoming uni stream to its FIN. +async function readToFin(quic: WebTransport) { + const { value } = await quic.incomingUnidirectionalStreams.getReader().read(); + if (!value) throw new Error("no stream"); + const reader = value.getReader(); + while (!(await reader.read()).done) {} +} + +// A stream that only asked the peer for prefixes ends nothing with its FIN, so leaving does not +// wait for the peer to acknowledge it. +test("finishing a stream that sent no claim waits on no acknowledgement", async () => { + const { client, server } = createMockTransportPair(); + try { + holdFins(client); + const stream = AnnounceStream.of(client, version); + await stream.send({ + status: "request", + more: false, + remove: [], + add: [{ prefix: Path.from("a"), hidden: false }], + }); + await Promise.all([stream.finish(), readToFin(server)]); + } finally { + client.close(); + server.close(); + } +}); + +// A claim ends only with the stream's FIN, so leaving waits until the peer acknowledges it. +test("finishing a stream that sent a claim waits for its acknowledgement", async () => { + const { client, server } = createMockTransportPair(); + try { + holdFins(client); + const stream = AnnounceStream.of(client, version); + await stream.send({ + status: "start", + suffix: Path.from("a"), + instance: Instance.parse("a"), + claim: ownClaim(0n), + }); + let finished = false; + void stream.finish().then(() => { + finished = true; + }); + await readToFin(server); + await new Promise(setImmediate); + expect(finished).toBe(false); + } finally { + client.close(); + server.close(); + } +}); diff --git a/js/net/src/lite/announce_stream.ts b/js/net/src/lite/announce_stream.ts index c651801b45..4e62328f39 100644 --- a/js/net/src/lite/announce_stream.ts +++ b/js/net/src/lite/announce_stream.ts @@ -17,6 +17,8 @@ export class AnnounceStream { // The last write queued, which the next one follows. #tail: Promise = Promise.resolve(); #finished = false; + // A claim was sent, which only this stream's FIN ends. + #announced = false; #break!: (err: unknown) => void; /** @@ -58,6 +60,7 @@ export class AnnounceStream { */ send(...msgs: AnnounceBroadcast[]): Promise { if (this.#finished) return Promise.reject(new Error("the Announce Stream finished")); + if (msgs.some((msg) => msg.status === "start")) this.#announced = true; const sent = this.#tail.then(async () => { const writer = await this.#open(); for (const msg of msgs) await encodeAnnounceBroadcast(writer, msg, this.#version); @@ -68,7 +71,8 @@ export class AnnounceStream { /** * This side is leaving the session: finish the stream once everything sent is written, and - * wait for the peer to take it. A stream that never opened stays unopened. + * wait for the peer to take it when it carried a claim the FIN ends. A stream that never + * opened stays unopened. */ finish(): Promise { this.#finished = true; @@ -78,7 +82,7 @@ export class AnnounceStream { if (!opened) return; const writer = await opened; writer.close(); - await writer.closed; + if (this.#announced) await writer.closed; }); this.#tail = done.catch(() => {}); return done; diff --git a/quest/m0/test-flakes-3/rust-audio-close-drain.md b/quest/m0/test-flakes-3/rust-audio-close-drain.md deleted file mode 100644 index 41a1a5b830..0000000000 --- a/quest/m0/test-flakes-3/rust-audio-close-drain.md +++ /dev/null @@ -1,25 +0,0 @@ -# [S] The Rust audio subscriber closes within its drain - -## Goal - -`just test interop --all` passes its `* -> rust` cells under load: the -`interop-audio` client (`test/interop/clients/rust/audio.rs`) closes its -session without `Session::close` returning `Error::Timeout`. - -## Plan - -Seen once on 2026-11-19 while validating -[moq-dev/moq#9155](https://github.com/moq-dev/moq/pull/9155): the -`python -> rust` cell decoded the whole tone (`decoded 28848 samples of tone -(opus) with libopus`), then failed with `Error: timeout` after 7 s of its 30 s -budget. The client had connected over the WebSocket fallback, a sign the host -was loaded. Three reruns of the cell alone passed, and main's scheduled -Interop runs show no such cell. The output does not say which deadline -returned the timeout. The likeliest is the subscriber's close drain (one second, -`rs/moq-net/src/session.rs` `poll_drain`), but `Error::Timeout` also comes -from the route stream (`rs/moq-net/src/lite/subscriber/route_stream.rs`) and -the publisher and server deadlines. First find which one fired, by logging the -error's path in the client after it decodes, then what it still waited on, and -fix that cause rather than the deadline or the client's `?`. - -Public API: none. Wire: none. diff --git a/rs/moq-net/src/lite/publisher/routed.rs b/rs/moq-net/src/lite/publisher/routed.rs index 1af956677b..2f4c977331 100644 --- a/rs/moq-net/src/lite/publisher/routed.rs +++ b/rs/moq-net/src/lite/publisher/routed.rs @@ -182,6 +182,8 @@ pub(in crate::lite) struct Answer { live: HashMap, /// The session is leaving: nothing more is sent. withdrawn: bool, + /// The peer was sent a route or a claim, which only this stream can end. + announced: bool, /// Why the last error closes the session, when the route layer said. cause: Option, } @@ -228,6 +230,7 @@ impl Answer { told: BTreeMap::new(), live: HashMap::new(), withdrawn: false, + announced: false, cause: None, } } @@ -558,6 +561,12 @@ impl Answer { } } + /// Whether the peer was ever sent a route or a claim. One that never was holds nothing + /// a finished stream ends, so a draining close need not wait for it. + pub(in crate::lite) fn announced(&self) -> bool { + self.announced + } + /// The session is leaving: let go of the node, so nothing more is queued. Finishing /// the stream ends everything the peer was sent. pub(in crate::lite) fn withdraw(&mut self) { @@ -765,6 +774,7 @@ impl Answer { claims: HashMap::new(), } }); + self.announced = true; writer.buffer(&Wire::Route { offer }) } None => { @@ -910,6 +920,7 @@ impl Answer { })?; let announce = announce.expect("lite-07 assigns announce ids"); self.live.insert(key, (announce, claim)); + self.announced = true; } (Some((announce, held)), Some(claim)) => { let id = *announce; diff --git a/rs/moq-net/src/lite/subscriber/route_stream.rs b/rs/moq-net/src/lite/subscriber/route_stream.rs index d4bf5ac192..9862b006ac 100644 --- a/rs/moq-net/src/lite/subscriber/route_stream.rs +++ b/rs/moq-net/src/lite/subscriber/route_stream.rs @@ -155,12 +155,31 @@ impl RouteStream { /// Ready only with the error that ends the session. pub(super) fn poll(&mut self, waiter: &kio::Waiter) -> Poll { - match self.poll_run(waiter) { + let res = self.poll_run(waiter); + if self.withdrawing.is_some() && !self.owes(waiter) { + self.withdrawing = None; + } + match res { Poll::Ready(Err(err)) => Poll::Ready(lite::error::control(&mut self.subscriber.session.clone(), err)), Poll::Ready(Ok(())) | Poll::Pending => Poll::Pending, } } + /// Whether a draining close still waits on this stream: until its FIN is acknowledged, + /// unless the peer was never sent a route or claim for the FIN to end. + fn owes(&self, waiter: &kio::Waiter) -> bool { + match &self.state { + State::Running(running) => match running.sending { + Sending::Open => true, + Sending::Ending | Sending::Closing => running.answer.announced(), + Sending::Closed => false, + }, + // Nothing is announced before the route layer runs, and a leaving session never + // starts it announcing. + State::Opening | State::Setup(_) => self.publish.withdrawal.poll(waiter).is_pending(), + } + } + pub(super) fn poll_run(&mut self, waiter: &kio::Waiter) -> Poll> { loop { match &mut self.state { @@ -178,11 +197,7 @@ impl RouteStream { self.state = State::Running(Box::new(running)); } State::Running(running) => { - let res = running.poll(&self.subscriber, &self.publish, waiter); - if matches!(running.sending, Sending::Closed) { - self.withdrawing = None; - } - return res; + return running.poll(&self.subscriber, &self.publish, waiter); } } } diff --git a/rs/moq-net/tests/integration/session_close.rs b/rs/moq-net/tests/integration/session_close.rs index 49077e4d37..3d3bbbd9b2 100644 --- a/rs/moq-net/tests/integration/session_close.rs +++ b/rs/moq-net/tests/integration/session_close.rs @@ -280,6 +280,38 @@ async fn close_times_out_waiting_for_announcement_acknowledgements() { } } +/// A subscriber that announced nothing has nothing to withdraw, so its close never waits +/// on an acknowledgement, however late the peer sends it. +#[kio::test] +async fn close_without_announcements_waits_on_no_acknowledgement() { + for version in [ + "moq-lite-01", + "moq-lite-06", + "moq-lite-07-wip", + "moq-transport-18", + "moq-transport-22", + ] { + let publisher = produce_origin(1); + let broadcast = publisher + .route("bcast", moq_net::origin::RouteOptions::hidden()) + .unwrap(); + broadcast.announce().unwrap(); + let subscriber = produce_origin(2); + let mut options = MockConnectOptions::new(version.parse().unwrap()); + options.server_publish = Some(publisher.consume()); + options.client_subscribe = Some(subscriber.clone()); + let pair = connect_mock(options).await; + crate::support::announced(&subscriber.consume(), "bcast").await; + pair.client_transport.hold_fin_acknowledgements(); + let started = kio::clock::now(); + pair.client + .close() + .await + .unwrap_or_else(|err| panic!("{version}: {err}")); + assert_eq!(kio::clock::now(), started, "{version}"); + } +} + /// The versions whose SUBSCRIBE_FINISH lets a subscriber settle its tail, and FIN, at once. const SETTLED_TAIL: [&str; 3] = ["moq-lite-05", "moq-lite-06", "moq-lite-07-wip"];