From 035d5ec816bdfe7aa1983cc72ddb2484cff661d5 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 15:04:44 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m1/settled-abort.md Quest-Claim: dfc62673f9de2a77 From 9aaf02ed47379e74744948c4c01e1b0541e551bb Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 15:33:03 -0700 Subject: [PATCH 2/3] fix(net): an aborted copy doesn't hold a track's end open A track's declared end now judges each slot below it by the copy serving its end: a fetched head that finished settles the end over a live copy that was cut or evicted, and an aborted copy counts as nothing in flight. A later abort then keeps the clean end instead of turning it into an error. The outcome poll watches every copy, so a serving head aborting hands the wait back to the live group. js/net mirrors the aborted-copy rule; it caches one copy per group, so it has no fetched-head case. Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.test.ts | 19 +++++ js/net/src/track.ts | 11 ++- quest/m1/README.md | 1 - quest/m1/settled-abort.md | 29 ------- rs/moq-net/src/model/track.rs | 141 +++++++++++++++++++++++++++++++--- 5 files changed, 158 insertions(+), 43 deletions(-) delete mode 100644 quest/m1/settled-abort.md diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 7b081471727..953075c4a46 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -2848,6 +2848,25 @@ test("finish is not terminal: closed settles once the groups below the end finis expect(hooks.trackReleased(producer).peek()).toBeNull(); }); +// As in Rust, an aborted group below the end has nothing left in flight, cut or cancelled. +test("an aborted group below the end lets it settle", async () => { + for (const error of [new StreamError(StreamCode.Cancel), new Error("cut")]) { + const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI }); + const aborted = producer.appendGroup(); + aborted.writeFrame(textFrame("open")); + producer.appendGroup().finish(); + producer.finish(); + await settle(); + expect(producer.closed.peek()).toBeUndefined(); + + aborted.abort(error); + await settle(); + expect(producer.closed.peek()).toBeNull(); + producer.abort(new Error("boom")); + expect(producer.closed.peek()).toBeNull(); + } +}); + test("closed reports an abort before the end settles", () => { const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI }); producer.appendGroup().writeFrame(textFrame("open")); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 8103632fcc9..9d64a3fe695 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1330,6 +1330,8 @@ export class Producer { void group.closed.then((closed) => { stop(); if (closed === null) this.#finishLargest(group.sequence); + // An aborted group below the end no longer holds it open either. + else this.#settle(); }); // Give held mirrors the new live edge before pruning their timeline entry, // so their latency guard can preserve a terminal expiry verdict. @@ -1717,15 +1719,16 @@ export class Producer { } // Whether the declared end was reached, the pending tail owes nothing below it, and every - // cached group below it finished, so the track already holds everything it promised. An - // abort over a group the tail still owed cut it off, so readers get the error rather than - // a clean end without it. Mirrors the Rust `is_settled`. + // cached group below it closed, so nothing the end promised is still in flight: a finished + // group holds everything it will, and an aborted one delivers nothing more. An abort over a + // group the tail still owed cut it off, so readers get the error rather than a clean end + // without it. Mirrors the Rust `is_settled`. #settled(): boolean { const end = this.#state.end.peek(); if (end === undefined) return false; const final = finalSequence(end); if (this.#received < final || this.#owes(final)) return false; - return this.#cache.every(({ group }) => group.sequence >= final || group.closed.peek() === null); + return this.#cache.every(({ group }) => group.sequence >= final || group.closed.peek() !== undefined); } /** Append a frame as its own single-frame group. */ diff --git a/quest/m1/README.md b/quest/m1/README.md index 32a58c043cd..f5d345a871a 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -135,7 +135,6 @@ removed). - [Announcement takeover](/quest/m1/announce-takeover.md) - a new independent route explicitly continues instances served through its predecessor, with shared Rust/JS semantics and lite-07 wire - [Subscribe ranges](/quest/m1/subscribe-ranges/README.md) - a lite-07 SUBSCRIBE asks for past and live ranges with absolute or relative ends, in either order, and replaces FETCH; merged ranges never starve a subscriber; relays fill misses by range, including over moq-transport - [Drop set](/quest/m1/drop-set.md) - each cursor judges missing groups against its own budget, so a narrow reader drops promptly and a widening re-offers groups already reported dropped on lite-07 -- [Aborted copies don't hold an end open](/quest/m1/settled-abort.md) - a track's declared end settles when a fetched copy finishes a group whose live copy was cut or evicted - [Broadcast archives](/quest/m1/archive/README.md) - ANNOUNCE, TRACKS, and a SUBSCRIBE per track record every track under a prefix to any object_store, a host serves a stored broadcast back, and the catalog drops its archive pointer for a per-rendition `hls` field - [HLS recorder target](/quest/m1/hls-record-target.md) - the moq-cli HLS recorder takes its target duration from the declared `hls` duration - [Compressed tracks](/quest/m1/compress/README.md) - a track flagged `compress` is DEFLATEd per group by moq-net, forwarded compressed, and inflated once on the relay for peers that cannot signal it; `catalog.json.z` and moq-flate go away diff --git a/quest/m1/settled-abort.md b/quest/m1/settled-abort.md deleted file mode 100644 index 5446116c846..00000000000 --- a/quest/m1/settled-abort.md +++ /dev/null @@ -1,29 +0,0 @@ -# [S] An aborted live copy doesn't hold a track's end open - -## Goal - -A track whose live copy of a group was cut or evicted, while a fetched copy -still serves that group to its end, settles its declared end when the fetched -copy finishes, and a later abort of the live copy never turns that clean end -into an error. - -## Plan - -Found while landing [moq-dev/moq#7053](https://github.com/moq-dev/moq/pull/7053) -(planned 2026-10-28). `TrackState::is_settled` (`rs/moq-net/src/model/`) -checks only each slot's own `slot.group`. When that group was aborted but a -fetched copy covers it, the end stays unsettled, and a later abort makes the -clean end an error. - -Recommended (confirm in the PR): an aborted live copy counts as nothing in -flight, so `is_settled` judges the slot by whichever copy serves its end, the -same meaning `Handle::is_settled` and `Slot::cut()` already use. Write the -failing case first: cut or evict a slot's live group while a fetched copy -serves it, finish the copy, and check the track's end settles cleanly. Mirror -it in `js/net` if its track end has the same check. - -Public API: none. Wire: none. - -## Related - -- [Drop set](/quest/m1/drop-set.md) - reworks the cut and drop records next to this check diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index c583307886e..878cb3941ff 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -602,6 +602,15 @@ impl Slot { } } + /// Whether nothing this slot promised a reader is still in flight: the copy serving its + /// end finished, even one a transport bound cut short, or no copy is left serving it. An + /// aborted copy delivers nothing more, so a live group cut or evicted under a fetched + /// head is judged by the head. + fn is_settled(&self) -> bool { + let group = self.serving(); + group.is_aborted() || group.is_settled() + } + /// Every copy this slot holds, the live group first. fn copies(&self) -> impl Iterator { std::iter::once(&self.group).chain(self.head.as_deref()) @@ -2434,14 +2443,14 @@ impl TrackState { } /// Whether the declared end is reached, the pending tail owes nothing below it, and - /// every cached copy below it finished, even one a transport bound cut short, so - /// nothing the end promised is still in flight. + /// every cached slot below it settled ([`Slot::is_settled`]), so nothing the end + /// promised is still in flight. fn is_settled(&self) -> bool { let Some(fin) = self.final_sequence() else { return false; }; let reached = self.sealed || self.reaches(fin); - reached && !self.owes(fin) && self.lookup.range(..fin).all(|(_, slot)| slot.group.is_settled()) + reached && !self.owes(fin) && self.lookup.range(..fin).all(|(_, slot)| slot.is_settled()) } /// Record that the end settled before a write that could reopen it: an abort, a late @@ -2608,15 +2617,19 @@ impl TrackState { return Poll::Ready(Ok(())); } if self.is_complete() { - // An abort after the end settled keeps the clean end; until then a group below it - // may still abort, which would cut the track off, so wait for each to finish. One - // that aborts leaves the outcome to the track's own abort or drop. + // An abort after the end settled keeps the clean end; until then the track's abort + // would end a group below it still in flight, cutting the track off, so wait for + // each slot to settle ([`Slot::is_settled`]): its serving copy finishes or aborts. if self.abort.is_none() && let Some(fin) = self.final_sequence() { - for (_, slot) in self.lookup.range(..fin) { - // One poll, so a group finishing meanwhile is seen rather than slept on. - if !matches!(slot.group.poll_closed(waiter), Poll::Ready(Ok(()))) { + for (_, slot) in self.lookup.range(..fin).filter(|(_, slot)| !slot.is_settled()) { + // Watch every copy, since a serving head that aborts hands the slot back to + // the live group, then judge again so a copy closing meanwhile is seen. + for group in slot.copies() { + let _ = group.poll_closed(waiter); + } + if !slot.is_settled() { return Poll::Pending; } } @@ -13941,6 +13954,37 @@ mod test { assert!(matches!(res, Err(Error::Timeout))); } + /// A group aborted below the declared end, whether the publisher cut it or it was + /// cancelled, has nothing left in flight, so the end settles over it rather than waiting + /// for the slot to expire, and a later abort still ends clean. + #[test] + #[cfg(feature = "async")] + fn an_aborted_group_below_the_end_lets_it_settle() { + let waiter = kio::Waiter::noop(); + for (err, expected) in [(Error::App(5), &[0, 1][..]), (Error::Cancel, &[1][..])] { + let mut producer = track_producer("test", Info::timed()); + let mut consumer = producer.subscribe(replay()); + let mut aborted = producer.append_group().unwrap(); + aborted.write_frame(Timestamp::ZERO, b"x".as_slice()).unwrap(); + producer.append_group().unwrap().finish().unwrap(); + producer.finish_at(Position::group(2)).unwrap(); + assert!(producer.poll_closed(&waiter).is_pending(), "group 0 is still written"); + + aborted.abort(err.clone()).unwrap(); + assert!(matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), "{err:?}"); + // A cut is still handed out; a cancelled group stays a hole. + let sequences: Vec = expected.iter().map(|_| consumer.assert_group().sequence).collect(); + assert_eq!(sequences, expected, "{err:?}"); + + producer.clone().abort(Error::Timeout).unwrap(); + assert!(matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), "{err:?}"); + assert!( + matches!(consumer.recv_group().now_or_never(), Some(Ok(None))), + "{err:?}" + ); + } + } + /// A local session closing is a dead route to the origin, not finished content, so a /// replacement route can take over where it left off. #[test] @@ -16097,6 +16141,85 @@ mod test { (live, head) } + /// A live group cut or evicted under a fetched head that serves it to its end settles the + /// declared end once the head finishes, and a later abort keeps that clean end, whether + /// `closed()` saw it first or only the abort did. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn an_aborted_live_copy_settles_by_its_fetched_head() { + use std::sync::atomic::{AtomicBool, Ordering}; + + for err in [Error::Cancel, Error::Evicted] { + for polled in [false, true] { + let mut producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + + let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); + live.start_at(1).unwrap(); + live.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"delta")) + .unwrap(); + let _fetch = consumer.fetch_group(0, None); + let mut head = dynamic + .requested_fill_group() + .await + .unwrap() + .accept(Info::timed()) + .unwrap(); + for payload in [&b"snapshot"[..], b"delta"] { + head.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(payload)) + .unwrap(); + } + + live.abort(err.clone()).unwrap(); + producer.finish_at(Position::group(1)).unwrap(); + let woken = Arc::new(AtomicBool::new(false)); + let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); + if polled { + assert!(producer.poll_closed(&waiter).is_pending(), "{err:?}: the head still writes"); + } + head.finish().unwrap(); + if polled { + assert!(woken.load(Ordering::SeqCst), "{err:?}: the head finishing wakes the wait"); + assert!( + matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), + "{err:?}" + ); + } + + producer.clone().abort(Error::Timeout).unwrap(); + let noop = kio::Waiter::noop(); + assert!( + matches!(producer.poll_closed(&noop), Poll::Ready(Ok(()))), + "{err:?} polled={polled}" + ); + assert!( + matches!(consumer.poll_complete(&noop), Poll::Ready(Ok(()))), + "{err:?} polled={polled}" + ); + } + } + } + + /// A finished fetched head serves its group to the end, so the declared end settles while + /// the live copy beside it is still written, and that copy aborting later keeps it clean. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn a_finished_head_settles_the_end_over_an_open_live_copy() { + let mut producer = track_producer("test", Info::timed()); + let consumer = producer.consume(); + let noop = kio::Waiter::noop(); + let (live, _head) = live_group_with_head(&producer).await; + + producer.finish_at(Position::group(1)).unwrap(); + assert!(matches!(producer.poll_closed(&noop), Poll::Ready(Ok(())))); + + live.abort(Error::Cancel).unwrap(); + producer.clone().abort(Error::Timeout).unwrap(); + assert!(matches!(producer.poll_closed(&noop), Poll::Ready(Ok(())))); + assert!(matches!(consumer.poll_complete(&noop), Poll::Ready(Ok(())))); + } + /// A fetched head still writing holds the largest stream open after the live copy /// aborted and the track ended, so a later head frame is reported rather than lost. #[moq_clock::test] From 60e24ebe7a4d6ed899ee07230fe21337a110cf0d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 16:13:17 -0700 Subject: [PATCH 3/3] fix(net): judge a settled end by the copy serving each group Treating every aborted copy as settled let a session that died mid-group end its subscription cleanly, which the subscription integrity test rejects. Narrow the rule to the copy serving the slot: a fetched head serving the group to its end settles it over a cut, evicted, or still-written live copy, and an aborted live copy with no head left still holds the end open. js/net caches one copy per group, so it keeps its check. Co-Authored-By: Claude Opus 5.5 --- js/net/src/track.test.ts | 19 ---- js/net/src/track.ts | 11 +-- rs/moq-net/src/model/track.rs | 165 +++++++++++++++++++--------------- 3 files changed, 95 insertions(+), 100 deletions(-) diff --git a/js/net/src/track.test.ts b/js/net/src/track.test.ts index 953075c4a46..7b081471727 100644 --- a/js/net/src/track.test.ts +++ b/js/net/src/track.test.ts @@ -2848,25 +2848,6 @@ test("finish is not terminal: closed settles once the groups below the end finis expect(hooks.trackReleased(producer).peek()).toBeNull(); }); -// As in Rust, an aborted group below the end has nothing left in flight, cut or cancelled. -test("an aborted group below the end lets it settle", async () => { - for (const error of [new StreamError(StreamCode.Cancel), new Error("cut")]) { - const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI }); - const aborted = producer.appendGroup(); - aborted.writeFrame(textFrame("open")); - producer.appendGroup().finish(); - producer.finish(); - await settle(); - expect(producer.closed.peek()).toBeUndefined(); - - aborted.abort(error); - await settle(); - expect(producer.closed.peek()).toBeNull(); - producer.abort(new Error("boom")); - expect(producer.closed.peek()).toBeNull(); - } -}); - test("closed reports an abort before the end settles", () => { const producer = new TrackProducer("test").accept({ timescale: Timescale.MILLI }); producer.appendGroup().writeFrame(textFrame("open")); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 9d64a3fe695..8103632fcc9 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -1330,8 +1330,6 @@ export class Producer { void group.closed.then((closed) => { stop(); if (closed === null) this.#finishLargest(group.sequence); - // An aborted group below the end no longer holds it open either. - else this.#settle(); }); // Give held mirrors the new live edge before pruning their timeline entry, // so their latency guard can preserve a terminal expiry verdict. @@ -1719,16 +1717,15 @@ export class Producer { } // Whether the declared end was reached, the pending tail owes nothing below it, and every - // cached group below it closed, so nothing the end promised is still in flight: a finished - // group holds everything it will, and an aborted one delivers nothing more. An abort over a - // group the tail still owed cut it off, so readers get the error rather than a clean end - // without it. Mirrors the Rust `is_settled`. + // cached group below it finished, so the track already holds everything it promised. An + // abort over a group the tail still owed cut it off, so readers get the error rather than + // a clean end without it. Mirrors the Rust `is_settled`. #settled(): boolean { const end = this.#state.end.peek(); if (end === undefined) return false; const final = finalSequence(end); if (this.#received < final || this.#owes(final)) return false; - return this.#cache.every(({ group }) => group.sequence >= final || group.closed.peek() !== undefined); + return this.#cache.every(({ group }) => group.sequence >= final || group.closed.peek() === null); } /** Append a frame as its own single-frame group. */ diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index 878cb3941ff..ada45de82fc 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -602,13 +602,13 @@ impl Slot { } } - /// Whether nothing this slot promised a reader is still in flight: the copy serving its - /// end finished, even one a transport bound cut short, or no copy is left serving it. An - /// aborted copy delivers nothing more, so a live group cut or evicted under a fetched - /// head is judged by the head. + /// Whether the copy serving this slot finished, even one a transport bound cut short, so + /// nothing it promised a reader is still in flight. A fetched head serving the group to + /// its end is judged in place of the live copy beside it, which may still be written or + /// may have been cut or evicted. Once no head serves, an aborted live copy holds the end + /// open: a session that died mid-group delivered less than the end promised. fn is_settled(&self) -> bool { - let group = self.serving(); - group.is_aborted() || group.is_settled() + self.serving().is_settled() } /// Every copy this slot holds, the live group first. @@ -2617,9 +2617,10 @@ impl TrackState { return Poll::Ready(Ok(())); } if self.is_complete() { - // An abort after the end settled keeps the clean end; until then the track's abort - // would end a group below it still in flight, cutting the track off, so wait for - // each slot to settle ([`Slot::is_settled`]): its serving copy finishes or aborts. + // An abort after the end settled keeps the clean end; until then a group below it + // may still abort, which would cut the track off, so wait for each slot to settle + // ([`Slot::is_settled`]). One whose last serving copy aborts leaves the outcome to + // the track's own abort or drop. if self.abort.is_none() && let Some(fin) = self.final_sequence() { @@ -13954,37 +13955,6 @@ mod test { assert!(matches!(res, Err(Error::Timeout))); } - /// A group aborted below the declared end, whether the publisher cut it or it was - /// cancelled, has nothing left in flight, so the end settles over it rather than waiting - /// for the slot to expire, and a later abort still ends clean. - #[test] - #[cfg(feature = "async")] - fn an_aborted_group_below_the_end_lets_it_settle() { - let waiter = kio::Waiter::noop(); - for (err, expected) in [(Error::App(5), &[0, 1][..]), (Error::Cancel, &[1][..])] { - let mut producer = track_producer("test", Info::timed()); - let mut consumer = producer.subscribe(replay()); - let mut aborted = producer.append_group().unwrap(); - aborted.write_frame(Timestamp::ZERO, b"x".as_slice()).unwrap(); - producer.append_group().unwrap().finish().unwrap(); - producer.finish_at(Position::group(2)).unwrap(); - assert!(producer.poll_closed(&waiter).is_pending(), "group 0 is still written"); - - aborted.abort(err.clone()).unwrap(); - assert!(matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), "{err:?}"); - // A cut is still handed out; a cancelled group stays a hole. - let sequences: Vec = expected.iter().map(|_| consumer.assert_group().sequence).collect(); - assert_eq!(sequences, expected, "{err:?}"); - - producer.clone().abort(Error::Timeout).unwrap(); - assert!(matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), "{err:?}"); - assert!( - matches!(consumer.recv_group().now_or_never(), Some(Ok(None))), - "{err:?}" - ); - } - } - /// A local session closing is a dead route to the origin, not finished content, so a /// replacement route can take over where it left off. #[test] @@ -16141,66 +16111,113 @@ mod test { (live, head) } - /// A live group cut or evicted under a fetched head that serves it to its end settles the - /// declared end once the head finishes, and a later abort keeps that clean end, whether - /// `closed()` saw it first or only the abort did. + /// A live group with a fetched head of group 0 still writing, and the track's end + /// declared past it. The live copy ended as `live` says: aborted with it, else finished. + #[cfg(feature = "async")] + async fn live_group_under_a_writing_head(live: Option) -> (Producer, group::Producer) { + let mut producer = track_producer("test", Info::timed()); + let dynamic = producer.requests(); + let consumer = producer.consume(); + + let mut group = producer.create_group(group::Info { sequence: 0 }).unwrap(); + group.start_at(1).unwrap(); + group + .write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"delta")) + .unwrap(); + let _fetch = consumer.fetch_group(0, None); + let mut head = dynamic + .requested_fill_group() + .await + .unwrap() + .accept(Info::timed()) + .unwrap(); + for payload in [&b"snapshot"[..], b"delta"] { + head.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(payload)) + .unwrap(); + } + + match live { + Some(err) => group.abort(err).unwrap(), + None => group.finish().unwrap(), + } + producer.finish_at(Position::group(1)).unwrap(); + (producer, head) + } + + /// The fetched head serving a group to its end settles the declared end once it finishes, + /// whether the live copy beside it was cut, evicted, or finished, and a later abort keeps + /// that clean end, whether `closed()` saw it first or only the abort did. #[moq_clock::test] #[cfg(feature = "async")] - async fn an_aborted_live_copy_settles_by_its_fetched_head() { + async fn a_fetched_head_settles_the_end_once_it_finishes() { use std::sync::atomic::{AtomicBool, Ordering}; - for err in [Error::Cancel, Error::Evicted] { + for live in [Some(Error::Cancel), Some(Error::Evicted), None] { for polled in [false, true] { - let mut producer = track_producer("test", Info::timed()); - let dynamic = producer.requests(); - let consumer = producer.consume(); - - let mut live = producer.create_group(group::Info { sequence: 0 }).unwrap(); - live.start_at(1).unwrap(); - live.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(b"delta")) - .unwrap(); - let _fetch = consumer.fetch_group(0, None); - let mut head = dynamic - .requested_fill_group() - .await - .unwrap() - .accept(Info::timed()) - .unwrap(); - for payload in [&b"snapshot"[..], b"delta"] { - head.write_frame(Timestamp::ZERO, bytes::Bytes::from_static(payload)) - .unwrap(); - } - - live.abort(err.clone()).unwrap(); - producer.finish_at(Position::group(1)).unwrap(); + let (producer, mut head) = live_group_under_a_writing_head(live.clone()).await; let woken = Arc::new(AtomicBool::new(false)); let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); if polled { - assert!(producer.poll_closed(&waiter).is_pending(), "{err:?}: the head still writes"); + assert!( + producer.poll_closed(&waiter).is_pending(), + "{live:?}: the head still writes" + ); } head.finish().unwrap(); if polled { - assert!(woken.load(Ordering::SeqCst), "{err:?}: the head finishing wakes the wait"); assert!( - matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), - "{err:?}" + woken.load(Ordering::SeqCst), + "{live:?}: the head finishing wakes the wait" ); + assert!(matches!(producer.poll_closed(&waiter), Poll::Ready(Ok(()))), "{live:?}"); } producer.clone().abort(Error::Timeout).unwrap(); let noop = kio::Waiter::noop(); assert!( matches!(producer.poll_closed(&noop), Poll::Ready(Ok(()))), - "{err:?} polled={polled}" + "{live:?} polled={polled}" ); assert!( - matches!(consumer.poll_complete(&noop), Poll::Ready(Ok(()))), - "{err:?} polled={polled}" + matches!(producer.consume().poll_complete(&noop), Poll::Ready(Ok(()))), + "{live:?} polled={polled}" ); } } } + /// A fetched head aborting hands its group back to the live copy: a finished one settles + /// the end, while a cut one leaves nothing that delivered the group, so an abort still + /// cuts the track off. + #[moq_clock::test] + #[cfg(feature = "async")] + async fn an_aborted_head_hands_the_end_back_to_the_live_copy() { + use std::sync::atomic::{AtomicBool, Ordering}; + + for (live, settles) in [(None, true), (Some(Error::App(5)), false)] { + let (producer, head) = live_group_under_a_writing_head(live.clone()).await; + let woken = Arc::new(AtomicBool::new(false)); + let waiter = kio::Waiter::new(futures::task::waker(Arc::new(FlagWake(woken.clone())))); + assert!(producer.poll_closed(&waiter).is_pending(), "{live:?}"); + + head.abort(Error::Cancel).unwrap(); + assert!( + woken.load(Ordering::SeqCst), + "{live:?}: the head aborting wakes the wait" + ); + assert_eq!(producer.poll_closed(&waiter).is_ready(), settles, "{live:?}"); + producer.clone().abort(Error::Timeout).unwrap(); + let outcome = producer.poll_closed(&kio::Waiter::noop()); + match settles { + true => assert!(matches!(outcome, Poll::Ready(Ok(()))), "{live:?}: {outcome:?}"), + false => assert!( + matches!(outcome, Poll::Ready(Err(Error::Timeout))), + "{live:?}: {outcome:?}" + ), + } + } + } + /// A finished fetched head serves its group to the end, so the declared end settles while /// the live copy beside it is still written, and that copy aborting later keeps it clean. #[moq_clock::test]