diff --git a/js/hang/src/container/legacy.ts b/js/hang/src/container/legacy.ts index c4aea1ce7d..638a828c6f 100644 --- a/js/hang/src/container/legacy.ts +++ b/js/hang/src/container/legacy.ts @@ -120,7 +120,10 @@ function micros(timestamp: Duration): number { * still refuses a frame after the finish. */ export interface Finished { - /** Abort the track with an error; see {@link Producer.abort}. */ + /** + * Mirrors the Rust `container::Finished::abort`, and like it changes nothing a reader sees: every + * group finished, so readers still draining get the rest of the track and its clean end, not `err`. + */ abort(err: Error): void; } @@ -282,7 +285,10 @@ export class Producer { return this; } - /** Abort the current group and the track with `err`, which readers see in place of a clean end. */ + /** + * Abort the current group and the track with `err`, which readers see in place of a clean end. A + * no-op after {@link finish}: the track is already complete. + */ abort(err: Error) { this.#group?.abort(err); this.#group = undefined; diff --git a/js/json/src/window/producer.ts b/js/json/src/window/producer.ts index 4d9a25e04b..b2beb2e4da 100644 --- a/js/json/src/window/producer.ts +++ b/js/json/src/window/producer.ts @@ -16,7 +16,10 @@ export interface Finished { readonly window: T[]; /** Absolute index of the oldest retained record; see {@link Producer.offset}. */ readonly offset: number; - /** Abort the track with an error; see {@link Producer.abort}. */ + /** + * Mirrors the Rust `window::Finished::abort`, and like it changes nothing a reader sees: every group + * finished, so readers still draining get the rest of the window and its clean end, not `err`. + */ abort(err: Error): void; } @@ -192,7 +195,10 @@ export class Producer { return this; } - /** Abort the track with `err`, so readers see the cause rather than a complete window. Idempotent. */ + /** + * Abort the track with `err`, so readers see the cause rather than a complete window. Idempotent, + * and a no-op after {@link finish}: the window is already complete. + */ abort(err: Error): void { this.#close(err); } diff --git a/js/json/src/window/window.test.ts b/js/json/src/window/window.test.ts index b6a594bc1d..7c428fe4f7 100644 --- a/js/json/src/window/window.test.ts +++ b/js/json/src/window/window.test.ts @@ -644,3 +644,18 @@ test("finishing returns a window that reads but takes no edit", () => { // @ts-expect-error a finished window takes no record expect(() => finished.push({ n: 2 }, key())).toThrow("track is closed"); }); + +// Mirrors the Rust `aborting_a_finished_window_still_drains`: an abort after finish changes nothing a +// reader sees, since every group finished, so one still draining gets the rest and the clean end. +test("aborting a finished window still drains", async () => { + const track = new Track.Producer("test"); + const consumer = new Consumer({ track: track.subscribe() }); + const producer = new Producer({ track }); + producer.push({ n: 0 }, key()); + producer.push({ n: 1 }, key()); + expect((await consumer.next())?.value).toEqual({ push: { index: 0, value: { n: 0 } } }); + + producer.finish().abort(new NetError.Stream(StreamCode.Cancel)); + expect((await consumer.next())?.value).toEqual({ push: { index: 1, value: { n: 1 } } }); + expect(await consumer.next()).toBeUndefined(); +}); diff --git a/quest/m1/precut-fixes/window-finished-abort.md b/quest/m1/precut-fixes/window-finished-abort.md deleted file mode 100644 index 7864e2f5f3..0000000000 --- a/quest/m1/precut-fixes/window-finished-abort.md +++ /dev/null @@ -1,9 +0,0 @@ -# [S] An abort on a finished JS window reaches undrained readers - -## Goal - -Window.Finished.abort(error) releases the finished window and reports the error to readers still draining it, matching Rust. - -## Plan - -The handle from [moq-dev/moq#8395](https://github.com/moq-dev/moq/pull/8395) returns its producer after finish sets the terminal guard. Publishing two records, consuming one, then calling finish().abort(error) delivered the second and clean EOF. Exercise an undrained reader and normal finish through the public API. diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index 3aaa727425..a1913f851e 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -944,8 +944,9 @@ impl Finished { self.track.demand() } - /// Abort the track with `err`, so subscribers that have not drained it see the real cause; see - /// [`Producer::abort`]. + /// Abort the track with `err`; see [`Producer::abort`]. Readers still draining get the end the + /// track declared instead once every group below it finished: the clean end after + /// [`Producer::finish`], or the capture's error when the source failed. pub fn abort(self, err: moq_net::Error) { self.track.abort(err); } diff --git a/rs/moq-json/src/snapshot/producer.rs b/rs/moq-json/src/snapshot/producer.rs index 58a3b9e5bd..4709ad5f1c 100644 --- a/rs/moq-json/src/snapshot/producer.rs +++ b/rs/moq-json/src/snapshot/producer.rs @@ -266,7 +266,8 @@ impl Finished { take(&self.inner).track.inner.demand() } - /// Release the track's cache, reporting `err` to readers that have not drained it. + /// Abort the track with `err`; see [`moq_net::track::Finished::abort`]. Readers still draining + /// get the clean end instead once every group below the end finished. pub fn abort(self, err: moq_net::Error) -> Result<()> { take(&self.inner).abort(err.into()); Ok(()) diff --git a/rs/moq-json/src/stream/producer.rs b/rs/moq-json/src/stream/producer.rs index 21cd84b711..ae972fff55 100644 --- a/rs/moq-json/src/stream/producer.rs +++ b/rs/moq-json/src/stream/producer.rs @@ -149,7 +149,8 @@ impl Finished { self.inner.lock().unwrap().track.inner.demand() } - /// Release the track's cache, reporting `err` to readers that have not drained it. + /// Abort the track with `err`; see [`moq_net::track::Finished::abort`]. Readers still draining + /// get the clean end instead once every group below the end finished. pub fn abort(self, err: moq_net::Error) -> Result<()> { self.inner.lock().unwrap().track.abort(err); Ok(()) diff --git a/rs/moq-json/src/window/mod.rs b/rs/moq-json/src/window/mod.rs index 0360e0fc20..b601fa715d 100644 --- a/rs/moq-json/src/window/mod.rs +++ b/rs/moq-json/src/window/mod.rs @@ -285,6 +285,26 @@ mod test { let _ = Encoder::::new(config); } + /// An abort after finish changes nothing a reader sees: every group finished, so one still + /// draining gets the rest of the window and the clean end, not the error. + #[test] + fn aborting_a_finished_window_still_drains() { + let (mut producer, track) = producer(Config::default()); + let mut consumer = consumer(track); + producer.push(&rec(0), key()).unwrap(); + producer.push(&rec(1), key()).unwrap(); + let waiter = kio::Waiter::noop(); + let mut next = || match consumer.poll_next(&waiter) { + Poll::Ready(next) => next.unwrap().map(|event| event.value), + Poll::Pending => panic!("expected a window event or its end"), + }; + assert!(matches!(next(), Some(Event::Push { index: 0, .. }))); + + producer.finish().unwrap().abort(moq_net::Error::Cancel).unwrap(); + assert!(matches!(next(), Some(Event::Push { index: 1, .. }))); + assert!(next().is_none()); + } + fn consumer(track: moq_net::track::Subscriber) -> Consumer { Consumer::new(track, consumer::Config::default()) } diff --git a/rs/moq-json/src/window/producer.rs b/rs/moq-json/src/window/producer.rs index 2bd3dcbfb9..29e0e03ee4 100644 --- a/rs/moq-json/src/window/producer.rs +++ b/rs/moq-json/src/window/producer.rs @@ -191,7 +191,8 @@ impl Finished { self.inner.lock().unwrap().encoder.range() } - /// Release the track's cache, reporting `err` to readers that have not drained it. + /// Abort the track with `err`; see [`moq_net::track::Finished::abort`]. Readers still draining + /// get the clean end instead once every group below the end finished. pub fn abort(self, err: moq_net::Error) -> Result<()> { self.inner.lock().unwrap().abort(err.into()); Ok(()) diff --git a/rs/moq-mux/src/container/flv/import.rs b/rs/moq-mux/src/container/flv/import.rs index 99f9fa8049..cbf923e9ca 100644 --- a/rs/moq-mux/src/container/flv/import.rs +++ b/rs/moq-mux/src/container/flv/import.rs @@ -833,8 +833,8 @@ pub struct Finished { } impl Finished { - /// Release every track's cache, reporting `err` to readers that have not drained it; see - /// [`Import::abort`]. + /// Abort every track with `err`; see [`Import::abort`]. Readers still draining a track get its + /// clean end instead once every group below its end finished. pub fn abort(self, err: moq_net::Error) { self.import.abort(err); } diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index 33a9070163..adee5f6f85 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -1921,8 +1921,8 @@ pub struct Finished { } impl Finished { - /// Release every track's cache, reporting `err` to readers that have not drained it; see - /// [`Import::abort`]. + /// Abort every track with `err`; see [`Import::abort`]. Readers still draining a track get its + /// clean end instead once every group below its end finished. pub fn abort(self, err: moq_net::Error) { self.import.abort(err); } diff --git a/rs/moq-mux/src/container/mkv/import.rs b/rs/moq-mux/src/container/mkv/import.rs index ee3431f58a..ed5052ff84 100644 --- a/rs/moq-mux/src/container/mkv/import.rs +++ b/rs/moq-mux/src/container/mkv/import.rs @@ -1033,8 +1033,8 @@ pub struct Finished { } impl Finished { - /// Release every track's cache, reporting `err` to readers that have not drained it; see - /// [`Import::abort`]. + /// Abort every track with `err`; see [`Import::abort`]. Readers still draining a track get its + /// clean end instead once every group below its end finished. pub fn abort(self, err: moq_net::Error) { self.import.abort(err); } diff --git a/rs/moq-mux/src/container/producer.rs b/rs/moq-mux/src/container/producer.rs index ea869644cf..dd077aeee8 100644 --- a/rs/moq-mux/src/container/producer.rs +++ b/rs/moq-mux/src/container/producer.rs @@ -1179,8 +1179,9 @@ where self.producer.estimate() } - /// Release the track's cache, reporting `err` to readers that have not drained it; see - /// [`moq_net::track::Finished::abort`]. + /// Abort the track with `err`; see [`moq_net::track::Finished::abort`]. Readers still draining get the end + /// the track declared instead once every group below it finished: the clean end after [`Producer::finish`], + /// or the error [`Producer::fail`] gave. pub fn abort(self, err: moq_net::Error) { self.producer.abort(err); } diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 9b44923732..3cd3040fb4 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -2056,8 +2056,8 @@ impl Finished { self.import.stats() } - /// Release every track's cache, reporting `err` to readers that have not drained it; see - /// [`Import::abort`]. + /// Abort every track with `err`; see [`Import::abort`]. Readers still draining a track get its + /// clean end instead once every group below its end finished. pub fn abort(self, err: moq_net::Error) { self.import.abort(err); } diff --git a/rs/moq-mux/src/container/ts/passthrough.rs b/rs/moq-mux/src/container/ts/passthrough.rs index 04a03d3789..43f00a7f36 100644 --- a/rs/moq-mux/src/container/ts/passthrough.rs +++ b/rs/moq-mux/src/container/ts/passthrough.rs @@ -197,8 +197,8 @@ impl Finished { self.passthrough.track() } - /// Release the track's cache, reporting `err` to readers that have not drained it; see - /// [`Passthrough::abort`]. + /// Abort the track with `err`; see [`Passthrough::abort`]. Readers still draining get the clean end + /// instead once every group below the end finished. pub fn abort(self, err: moq_net::Error) { self.passthrough.abort(err); } diff --git a/rs/moq-mux/src/container/ts/republish.rs b/rs/moq-mux/src/container/ts/republish.rs index bbcaa29f9d..52b1592f1e 100644 --- a/rs/moq-mux/src/container/ts/republish.rs +++ b/rs/moq-mux/src/container/ts/republish.rs @@ -306,7 +306,8 @@ impl Finished { self.import.stats() } - /// Release every track's cache, reporting `err` to readers that have not drained it. + /// Abort every track with `err`. Readers still draining a track get its clean end instead once + /// every group below its end finished. pub fn abort(self, err: moq_net::Error) { self.import.abort(err); } diff --git a/rs/moq-mux/src/import/container.rs b/rs/moq-mux/src/import/container.rs index 3bcc382c9d..8dd6d7f092 100644 --- a/rs/moq-mux/src/import/container.rs +++ b/rs/moq-mux/src/import/container.rs @@ -175,7 +175,8 @@ pub struct Finished { } impl Finished { - /// Release every track's cache, reporting `err` to readers that have not drained it. + /// Abort every track with `err`. Readers still draining a track get its clean end instead once + /// every group below its end finished. pub fn abort(self, err: moq_net::Error) { match self.inner { FinishedImpl::Fmp4(finished) => finished.abort(err), diff --git a/rs/moq-mux/src/import/track.rs b/rs/moq-mux/src/import/track.rs index f1c87b14f5..3b5650ae7a 100644 --- a/rs/moq-mux/src/import/track.rs +++ b/rs/moq-mux/src/import/track.rs @@ -933,8 +933,8 @@ impl Finished { } } - /// Release the track's cache, reporting `err` to readers that have not drained it; see - /// [`moq_net::track::Finished::abort`]. + /// Abort the track with `err`; see [`moq_net::track::Finished::abort`]. Readers still draining get the clean end + /// instead once every group below the end finished. pub fn abort(self, err: moq_net::Error) { match self.inner { FinishedKind::Video(track) => track.abort(err), diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 01b416416d..d30b32c485 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -5460,7 +5460,8 @@ impl Finished { /// Abort the track with the given error; see [`Producer::abort`]. /// - /// Once every group below the end finished, readers still get the clean end. + /// Once every group below the end finished, readers still get the end the track declared: the + /// clean end after [`Producer::finish`], or the error [`Producer::fail`] gave. pub fn abort(self, err: Error) -> Result<()> { self.producer.abort(err) } diff --git a/rs/moq-video/src/encode/publisher.rs b/rs/moq-video/src/encode/publisher.rs index de1c3139fa..985c39e22f 100644 --- a/rs/moq-video/src/encode/publisher.rs +++ b/rs/moq-video/src/encode/publisher.rs @@ -254,7 +254,8 @@ impl Finished { self.track.demand() } - /// Abort the track with `err`, so subscribers that have not drained it see the real cause. + /// Abort the track with `err`; see [`moq_net::track::Finished::abort`]. Readers still draining + /// get the clean end instead once every group below the end finished. pub fn abort(self, err: moq_net::Error) { self.track.abort(err); }