From 511ceb65e3c2cce9c789ba3c13e29302dfe96d5b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 09:00:38 -0800 Subject: [PATCH 1/5] chore(quest): claim quest/m2/yield-defer.md Quest-Claim: e8cce37a4cf5ee5c From 098fe8e6b81fd544c4752fa9aba669baf9171b37 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 10:19:40 -0800 Subject: [PATCH 2/5] feat(net)!: lite-07 drops a group reset to make room, and YIELDED waits for lite-08 A publisher out of stream credit resets the lowest-priority Group Stream with TOO_FAR_BEHIND and names the group in SUBSCRIBE_DROP on every version, as lite-06 does. A reader that wants the rest FETCHes it. The YIELDED (0x3C) code, paused groups, and continuations leave the lite-07 draft, moq-net, and @moq/net, and come back with takeover in lite-08. The subscribe model loses its yield actions and the PausedIsOwed and ContinuesWithinHeld rules. test/stream-credit and stream_credit.rs check the drop-plus-fetch contract instead. Co-Authored-By: Claude Opus 5.5 --- .claude/skills/verify-subscribe/SKILL.md | 24 +- doc/concept/moq-lite.md | 9 + drafts/draft-lcurley-moq-lite.md | 27 +- js/net/api/index.api.md | 1 - js/net/src/error.test.ts | 4 - js/net/src/error.ts | 5 - js/net/src/integration.test.ts | 31 +- js/net/src/internal.ts | 13 +- js/net/src/lite/copy_fill.test.ts | 19 +- js/net/src/lite/group.test.ts | 68 +- js/net/src/lite/group.ts | 87 +- js/net/src/lite/publisher.test.ts | 50 +- js/net/src/lite/publisher.ts | 69 +- js/net/src/lite/subscriber.test.ts | 465 +-------- js/net/src/lite/subscriber.ts | 314 ++---- js/net/src/lite/tail.test.ts | 2 +- js/net/src/lite/version.ts | 18 - js/net/src/open.ts | 5 +- js/net/src/payload.ts | 9 +- js/net/src/resume.ts | 4 +- js/net/src/stream.ts | 47 +- js/net/src/track.ts | 14 +- quest/m1/README.md | 2 +- quest/m2/README.md | 4 +- quest/m2/ietf-records-per-track.md | 4 - quest/m2/lite07-finalize.md | 4 +- quest/m2/time-addressing/lite07.md | 21 +- quest/m2/yield-defer.md | 109 -- quest/m3/fetch-preempted-group.md | 51 + quest/m3/group-takeover.md | 4 +- rs/moq-net/benches/publisher.rs | 54 +- rs/moq-net/src/coding/writer.rs | 5 - rs/moq-net/src/error.rs | 8 - rs/moq-net/src/fuzz.rs | 2 +- rs/moq-net/src/ietf/error.rs | 1 - rs/moq-net/src/lite/mod.rs | 2 +- rs/moq-net/src/lite/publisher.rs | 324 +----- rs/moq-net/src/lite/subscriber.rs | 956 ++---------------- rs/moq-net/src/lite/version.rs | 11 - rs/moq-net/src/model/group.rs | 28 - rs/moq-net/src/model/track.rs | 11 - rs/moq-net/src/tail.rs | 14 - rs/moq-net/src/transport/boxed.rs | 8 - rs/moq-net/tests/integration/stream_credit.rs | 265 +---- rs/moq-net/verify/subscribe/Continuation.cfg | 20 - rs/moq-net/verify/subscribe/StreamWins.cfg | 2 - .../verify/subscribe/SubscribeAccounting.cfg | 8 +- .../verify/subscribe/SubscribeAccounting.tla | 246 +---- test/stream-credit/README.md | 37 +- test/stream-credit/driver.ts | 50 +- test/stream-credit/relay.toml | 1 - test/stream-credit/run.sh | 2 +- test/stream-credit/src/main.ts | 37 +- test/stream-credit/src/plan.ts | 2 + 54 files changed, 522 insertions(+), 3056 deletions(-) delete mode 100644 quest/m2/yield-defer.md create mode 100644 quest/m3/fetch-preempted-group.md delete mode 100644 rs/moq-net/verify/subscribe/Continuation.cfg diff --git a/.claude/skills/verify-subscribe/SKILL.md b/.claude/skills/verify-subscribe/SKILL.md index f9462e7456c..009c063cf8a 100644 --- a/.claude/skills/verify-subscribe/SKILL.md +++ b/.claude/skills/verify-subscribe/SKILL.md @@ -1,11 +1,11 @@ --- name: verify-subscribe -description: Model-check how a lite-07 subscription accounts for each group (`rs/moq-net/src/lite/publisher.rs` `TrackRun` and `GroupServe`, `lite/subscriber.rs` `ServeLoop::apply_response` and `buffer_update`, `src/tail.rs`) with TLA+. Use when a change touches SUBSCRIBE_DROP, SUBSCRIBE_SKIP, stale (OLD) drops and their update numbers, SUBSCRIBE_UPDATE re-offers, Group Stream resets and expiry, YIELDED resets and continuations of a paused group, duplicate Group Streams, or the subscriber's tail, or when a drop or re-offer bug needs every ordering of the Subscribe Stream against the Group Streams checked. +description: Model-check how a lite-07 subscription accounts for each group (`rs/moq-net/src/lite/publisher.rs` `TrackRun` and `GroupServe`, `lite/subscriber.rs` `ServeLoop::apply_response` and `buffer_update`, `src/tail.rs`) with TLA+. Use when a change touches SUBSCRIBE_DROP, SUBSCRIBE_SKIP, stale (OLD) drops and their update numbers, SUBSCRIBE_UPDATE re-offers, Group Stream resets and expiry, duplicate Group Streams, or the subscriber's tail, or when a drop or re-offer bug needs every ordering of the Subscribe Stream against the Group Streams checked. --- # Verify subscription accounting -`rs/moq-net/verify/subscribe/SubscribeAccounting.tla` models one group of one answered lite-07 subscription between the publisher's `TrackRun` and the subscriber's `TrackServe`. The draft promises that every sequence in range gets a Group Stream, a SUBSCRIBE_SKIP, or a SUBSCRIBE_DROP; that a stale (OLD) drop holds only under the update it names and the next SUBSCRIBE_UPDATE reopens it; and that a Group Stream wins over a stale drop, even one it overtook. Drops and skips travel in order on the Subscribe Stream, each Group Stream travels on its own, a reset can lose a stream's header, and a reset sent after the FIN may arrive after every byte. A publisher out of stream credit may reset a Group Stream with YIELDED, which pauses the group instead of accounting for it: the subscriber keeps the frames it read, and the publisher owes a continuation, a Group Stream that starts at the reset stream's own Frame Start, or a drop. A continuation may overtake the YIELDED reset, or even the header, of the stream it continues. Each model step is one poll of the code that handles one event. The track cursor's verdicts (hand the group out, give it up stale or final, skip it) and whether the cache still holds a fresh copy on an update are free choices, and so are when the publisher yields a stream and whether it continues a paused group or gives it up. With `Pressure`, so is when the cache pool's debt folds what each side keeps of the group: the publisher's record and the subscriber's tail. TLC checks every ordering under the bounds in the config. +`rs/moq-net/verify/subscribe/SubscribeAccounting.tla` models one group of one answered lite-07 subscription between the publisher's `TrackRun` and the subscriber's `TrackServe`. The draft promises that every sequence in range gets a Group Stream, a SUBSCRIBE_SKIP, or a SUBSCRIBE_DROP; that a stale (OLD) drop holds only under the update it names and the next SUBSCRIBE_UPDATE reopens it; and that a Group Stream wins over a stale drop, even one it overtook. Drops and skips travel in order on the Subscribe Stream, each Group Stream travels on its own, a reset can lose a stream's header, and a reset sent after the FIN may arrive after every byte. Each model step is one poll of the code that handles one event. The track cursor's verdicts (hand the group out, give it up stale or final, skip it) and whether the cache still holds a fresh copy on an update are free choices. With `Pressure`, so is when the cache pool's debt folds what each side keeps of the group: the publisher's record and the subscriber's tail. TLC checks every ordering under the bounds in the config. ## Rules @@ -14,22 +14,19 @@ Each rule is an INVARIANT; the harm says why it matters. | Rule | Holds when | Harm if broken | | --- | --- | --- | | `NoViolation` | No SUBSCRIBE_DROP names an update the subscriber has not sent. | The subscriber closes the session with a PROTOCOL_VIOLATION. | -| `ServedOnce` | The publisher opens a second Group Stream for a group only after resetting the first, and a continuation continues the group's latest stream, which it reset with YIELDED. | The group's bytes go out twice at once, or a continuation resumes a group nobody paused. | +| `ServedOnce` | The publisher opens a second Group Stream for a group only after resetting the first. | The group's bytes go out twice at once. | | `QuietIsSettled` | Once nothing is in flight and the publisher has nothing left to do, the subscriber's tail accounts for the group. | The subscriber waits out its grace for a group nobody will send, and a relay holds its readers there. | -| `PausedIsOwed` | While the publisher owes a paused group, the subscriber's paused copy stays owed, unless a drop or skip under the update in effect, a stream read to its FIN, or a reset of another code that arrived with its header accounts for it. | The tail settles on a YIELDED reset alone and the subscription ends before the continuation arrives. | -| `ContinuesWithinHeld` | A continuation that takes over a paused copy starts no later than the frames the copy holds (`Continuation.cfg`). | The subscriber treats the continuation as malformed, or a frame a reset discarded is never delivered. | -| `OneReader` | The subscriber reads at most one Group Stream of a group at a time; a continuation waits, unread, for the stream it continues to end. | Two streams write one group's frames at once. | +| `OneReader` | The subscriber reads at most one Group Stream of a group at a time; a re-offer waits, unread, for the stream it replaces to end. | Two streams write one group's frames at once. | | `StreamWins` | A group the subscriber read to its FIN is never recorded stale. | The subscriber owes a group it already has; the next update reopens it, and the publisher sends the whole group again or drops it again on every update. | ## Run ```sh quest gate just rs tla moq-net/verify/subscribe/SubscribeAccounting.tla -quest gate just rs tla moq-net/verify/subscribe/SubscribeAccounting.tla -config rs/moq-net/verify/subscribe/Continuation.cfg quest gate just rs tla moq-net/verify/subscribe/SubscribeAccounting.tla -config rs/moq-net/verify/subscribe/StreamWins.cfg ``` -The first checks every rule but `StreamWins` and `ContinuesWithinHeld` under `Pressure`, with one YIELDED reset and frames left out: about 77.7M distinct states, fifteen minutes. The second gives the group one frame and two YIELDED resets, without `Pressure` and over two updates, so it also checks where each continuation starts: about 29.6M distinct states, five minutes. Both pass. The third checks `StreamWins` without `Pressure` or YIELDED and fails on the open finding below. Nightly runs every config here and fails if a config with an `\* Open finding:` line passes. One group is enough: groups share only the order of the Subscribe Stream, and a second group multiplies the states (60M and more) without adding a race. +The first checks every rule but `StreamWins` under `Pressure`: about 13.2M distinct states, under two minutes, and it passes. The second checks `StreamWins` without `Pressure` and fails on the open finding below. Nightly runs every config here and fails if a config with an `\* Open finding:` line passes. One group is enough: groups share only the order of the Subscribe Stream, and a second group multiplies the states (60M and more) without adding a race. ## Open finding: a group read whole is named in a stale drop @@ -41,12 +38,12 @@ Follow Reading a failure in `rs/moq-net/verify/README.md`. Replay the path on th ## Coverage -Sync commit: `6aa4f9c1cad482aa86adcf3cf26bd7779697a01a`. Run `git log 6aa4f9c1ca.. -- rs/moq-net/src/lite/publisher.rs rs/moq-net/src/lite/subscriber.rs rs/moq-net/src/tail.rs rs/moq-net/src/model/track.rs` to see what changed since. Follow Coverage in `rs/moq-net/verify/README.md` after a change to a covered function. +Sync commit: `13e593cdb1c9bc6c12b6e0d142eadcaae716f470`. Run `git log 13e593cdb1.. -- rs/moq-net/src/lite/publisher.rs rs/moq-net/src/lite/subscriber.rs rs/moq-net/src/tail.rs rs/moq-net/src/model/track.rs` to see what changed since. Follow Coverage in `rs/moq-net/verify/README.md` after a change to a covered function. | Code | Model | | --- | --- | | `rs/moq-net/src/lite/publisher.rs` `TrackRun::poll_track` (`Recv::Group`, the re-offer pop), `serve`, `mark_served` | `CursorHand`, `PubReoffer`, `Serve` | -| `rs/moq-net/src/lite/publisher.rs` `TrackRun::give_way`, `shed` | a new group, or one shed, as `Serve`, then `StreamFail` with `old` whose header never arrives; a continuation as `StreamYield` of a stream whose header never arrives | +| `rs/moq-net/src/lite/publisher.rs` `TrackRun::give_way`, `shed` | a new group, or one shed, as `Serve`, then `StreamFail` with `old` whose header never arrives | | `rs/moq-net/src/lite/publisher.rs` `poll_forward_drop`, `forward_range`, `drop_range`, `replace_stale`, `sent_stale`, `hole` | `CursorGiveUp`, `ReplaceStale` | | `rs/moq-net/src/lite/publisher.rs` `TrackRun::poll_step` (outcomes, `redrop`) | `PubOutcomes`, `PubRedrop` | | `rs/moq-net/src/lite/publisher.rs` `TrackRun::update`, `owe`; `rs/moq-net/src/model/track.rs` `reoffer_stale`, `reoffer_groups` | `PubUpdate` | @@ -57,16 +54,11 @@ Sync commit: `6aa4f9c1cad482aa86adcf3cf26bd7779697a01a`. Run `git log 6aa4f9c1ca | `rs/moq-net/src/tail.rs` `Reading::open`, `Reading::finish`, `Tail::finished`, `updated`, `account`, `unaccount` | `acct`, `finished` | | `rs/moq-net/src/tail.rs` `Tail::rebill`, `overtaken`, `updated`; `rs/moq-net/src/lite/subscriber.rs` `ServeLoop::apply_response`; `rs/moq-net/src/model/record.rs` `Record::folded` | `TailFold`, `forgot`, `PubFold`, `folded` | | `rs/moq-net/src/model/track.rs` `Producer::give_up`, `skip_received` | `rec`, `cached` | -| `rs/moq-net/src/lite/publisher.rs` `GroupServe::settle_failed`, `TrackRun::open`; `rs/moq-net/src/transport/boxed.rs` `SendStream::preempted` | `StreamYield`, `PubContinue`, `PausedGiveUp` | -| `rs/moq-net/src/lite/subscriber.rs` `TrackEntry::pause`, `resume`, `dropped`, `read_yielding`, `end_yielding`, `YieldVerdict`, `GroupRecv::poll_serve`, `FrameIngest::dispatch` | the YIELDED branch of `SubReset`, `SubFrame`, `reading`, `sdrop`, `paused` | What the model asks of that code, each found by TLC as a broken rule without it: -- A YIELDED reset takes back only what its own stream's header added to the ledger (`reading`): `TrackEntry::open` reads whether the tail already accounted the group before the stream accounts it (`Yielding::owed`). A header that arrives once the ledger settled the group adds nothing, so a YIELDED stream whose header arrives after its continuation finished the group owes nothing again (`QuietIsSettled`). -- A continuation that takes over from a held position inherits that account (`TrackEntry::resume`), so its own YIELDED reset takes it back. -- A drop under the update in effect that reached a stream still being read ends its group once the YIELDED reset arrives (`TrackEntry::dropped`). The model then records the group stale; the code aborts the group with the drop's error, which the drop already accounted, so the tail settles either way. - A stream waits, held, while another stream of its group is still read, even one a final drop aborted, not only while the copy is open (`OneReader`). ## Not covered -The subscription starts answered and covers the group, so SUBSCRIBE_OK, SUBSCRIBE_STARTED, ranges and Range IDs, fills (`RangeFills`), floors, and SUBSCRIBE_FINISH with `drop_unserved` are left out. The tail's grace and the ledger's folding are left out, so a group `Tail::overtaken` reports is one whose header arrived: `QuietIsSettled` asks for the publisher's answer, never the grace. So are priority and order (`Steer`), the Subscribe Stream's FIN and cancellation, sessions closing, versions before lite-07, relays forwarding drops downstream, datagrams, and the copy's readers (`poll_drop`). A route switch appears only as the cursor handing out once more a group it gave up. FETCH is left out: one request and one response, ended by a FIN or a reset, with nothing to race. GROUP_END takeover is left out: only the YIELDED reset pauses a group, and the publisher yields only a stream still serving. A pass covers one group, four updates, four Group Streams, and one YIELDED reset, or two updates, two YIELDED resets, and one frame in `Continuation.cfg`. +The subscription starts answered and covers the group, so SUBSCRIBE_OK, SUBSCRIBE_STARTED, ranges and Range IDs, fills (`RangeFills`), floors, and SUBSCRIBE_FINISH with `drop_unserved` are left out. The tail's grace and the ledger's folding are left out, so a group `Tail::overtaken` reports is one whose header arrived: `QuietIsSettled` asks for the publisher's answer, never the grace. So are priority and order (`Steer`), the Subscribe Stream's FIN and cancellation, sessions closing, versions before lite-07, relays forwarding drops downstream, datagrams, and the copy's readers (`poll_drop`). A route switch appears only as the cursor handing out once more a group it gave up. FETCH is left out: one request and one response, ended by a FIN or a reset, with nothing to race. Frames are left out: no stream continues another, so what a stream carried never changes how its group is accounted. A pass covers one group, four updates, and four Group Streams. diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index f270c250a90..f5ab68cd417 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -291,6 +291,15 @@ A reader of a reported group, including one still arriving, ends with the publis Before moq-lite 07 that code is application-specific, so every reported group ends with `OLD`. Datagram delivery stays best effort. +A peer that grants fewer streams than its subscriptions have groups in flight, as a +browser does at about 100, gets them for the highest-priority groups. To make room +the publisher resets the lowest-priority group's stream with `TOO_FAR_BEHIND`, on +every moq-lite version, and from moq-lite 05 also reports that group as dropped. +Its reader ends with that error after the frames that arrived, and no later +stream carries the rest, even for a reader with no max delay. A reader that still +wants the group fetches it, which returns it whole on a stream the subscriber +opens, outside the cap. + Ranges are both a request and a filter. A subscriber reads only the groups its own ranges cover, resolved again whenever it replaces them, even when another subscriber of the same track asked for more. Widening them, including lowering diff --git a/drafts/draft-lcurley-moq-lite.md b/drafts/draft-lcurley-moq-lite.md index 48a1e21e76f..74dd953371f 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -216,7 +216,7 @@ A subscriber that has received frames 0 through `N-1` of group `G` and wants the This is what lets a subscriber move a Track to a different publisher partway through a Group instead of waiting for the next Group to start, which matters for Tracks whose current Group may stay open indefinitely. A Group can therefore be assembled from more than one publisher: each contributes a disjoint run of frames, and the subscriber concatenates them in index order. -A partial Group is only ever delivered to a subscriber that asked for one with FETCH, or as the [continuation](#yield) of a paused Group. +A partial Group is only ever delivered to a subscriber that asked for one with FETCH. A publisher that holds only part of a Group a subscription covers names it in a [SUBSCRIBE_DROP](#subscribe-drop) rather than deliver the part it holds. A Group is the unit of decodability: its earlier Frames may carry a keyframe or compression state the later Frames need. @@ -360,8 +360,6 @@ Sent when resetting a stream (RESET_STREAM), or when refusing to receive one (ST | ------- | ------------- | ----------- | | 0x3B | UNAUTHORIZED | The endpoint does not authorize this request, or no longer does, such as when the [scope](#auth-stream) does not cover it. The session stays up. | | ------- | ------------- | ----------- | -| 0x3C | YIELDED | The Group Stream was reset to make room for a higher-priority stream. The Group is not given up: it [continues](#yield) on a later stream. | -| ------- | ------------- | ----------- | Note that CANCELLED is 0x1, not 0x0: a stream reset with 0x0 is an INTERNAL_ERROR, not a routine cancellation. An endpoint terminating a stream because the session is ending SHOULD use SESSION_CLOSED rather than the session's own code, since the two spaces are disjoint. @@ -892,7 +890,7 @@ A frame MAY contain an empty payload, potentially indicating a gap in the group. The GROUP message's `Frame Start` gives the index of the first FRAME on the stream, so a stream carrying only part of a Group is self-describing. -A publisher MUST NOT send two Group Streams for the same Group Sequence on one subscription, except to re-offer a Group whose stream it reset (see [SUBSCRIBE_DROP](#subscribe-drop)), or to [continue](#yield) one. +A publisher MUST NOT send two Group Streams for the same Group Sequence on one subscription, except to re-offer a Group whose stream it reset (see [SUBSCRIBE_DROP](#subscribe-drop)). The re-offer can arrive before that reset: a subscriber holds a Group Stream for a sequence whose earlier stream is still arriving unread until that stream ends, then discards it if the earlier stream completed the Group, or reads it if the earlier stream was reset. Since the publisher reset the earlier stream, the subscriber MAY instead end that stream at once and read the later one, ignoring the frames it holds; a browser can lose a reset that reaches a stream before its application does. A subscriber assembling a Group from more than one publisher does so across separate subscriptions, and is responsible for concatenating the runs in index order and for ignoring any frame it has already received. @@ -901,16 +899,9 @@ Both the publisher and subscriber MAY reset the stream at any time. This is not a fatal error and the session remains active. The subscriber MAY cache the error and potentially retry later. -#### Yield {#yield} -A publisher out of the peer's stream credit MAY reset the lowest-priority Group Stream with YIELDED to make room for a higher-priority one. -A Group Stream of a Track carrying a MoqTransport Version under the `ietf` extension, whose object records name whole objects, is never reset with YIELDED. -Any other Group Stream may be, including one on a session with the extension: a continuation's records start at its `Frame Start`. - -A Group whose stream was reset with YIELDED is paused, not given up: the subscriber keeps the frames it received, and the publisher MUST later continue the Group on another Group Stream or name it in [SUBSCRIBE_DROP](#subscribe-drop). -A continuation is a Group Stream for the paused Group whose `Frame Start` is the reset stream's own `Frame Start`, since a reset can discard frames the transport acknowledged. -The subscriber ignores the frames it already holds, including the part of a frame the reset cut short, and appends the rest; a continuation that starts past the frames it holds whole is malformed. -A continuation MAY overtake the earlier stream's reset, and waits unread until that stream ends, or ends it, as for a re-offer. -A subscriber whose transport reports a Group Stream's reset without its error code, as a browser can, pauses the Group as for YIELDED: the publisher's continuation or SUBSCRIBE_DROP settles it. +A publisher out of the peer's stream credit MAY reset the lowest-priority Group Stream with TOO_FAR_BEHIND to make room for a higher-priority one. +That gives the Group up on the subscription: the publisher names it in [SUBSCRIBE_DROP](#subscribe-drop), and a subscriber that wants the rest of it FETCHes the Group from the first frame it lacks (see [Positions](#positions)). +The drop settles the Group even where the subscriber's transport loses the reset or its error code, as a browser can. ## Datagrams QUIC datagrams provide unreliable, unordered delivery for latency-sensitive content that does not need retransmission. @@ -2059,8 +2050,7 @@ Either way the Group Stream accounts for the sequence, and the next SUBSCRIBE_UP For every sequence in the subscription's range, the publisher MUST send a Group Stream or name it in SUBSCRIBE_SKIP or SUBSCRIBE_DROP. This includes streams reset before their headers were delivered, and streams the subscriber stopped (STOP_SENDING), which it may have discarded unread; such a drop carries the STOP_SENDING's Error Code. A drop naming a stopped duplicate of a Group that another Group Stream already completed changes nothing for that Group: its readers keep it whole. -A reliably delivered reset retaining the Group Stream header accounts for that group instead, unless it is YIELDED or answers a STOP_SENDING. -A [paused](#yield) Group is accounted for only once a continuation completes it, a reset of one other than YIELDED ends it, or a SUBSCRIBE_DROP names it. +A reliably delivered reset retaining the Group Stream header accounts for that group instead, unless it answers a STOP_SENDING. The publisher SHOULD name a sequence as soon as it gives it up, without waiting for SUBSCRIBE_FINISH. Before moq-lite-07, SUBSCRIBE_DROP carries an inclusive `Group End` in place of `Group Count`, where `Group Start = Group End + 1` is an empty range and a larger Group Start is a PROTOCOL_VIOLATION. @@ -2312,7 +2302,7 @@ A subscriber MUST handle gaps, potentially caused by congestion. The index of the first FRAME message on this stream within the group (see [Positions](#positions)). A plain absolute index; 0 means the stream carries the group from its beginning, which is the common case. A non-zero value means the leading frames are not on this stream. -The subscriber MUST NOT assume it will receive the missing frames on another stream; they are a gap unless a FETCH covers them, or the stream [continues](#yield) a paused Group. +The subscriber MUST NOT assume it will receive the missing frames on another stream; they are a gap unless a FETCH covers them. ## FRAME @@ -2467,8 +2457,7 @@ An application-specific payload. - Added a leading Type to datagrams: Track (0x0) or Padding (0x1), which either endpoint discards without changing session or subscription state. - Expiration also measures a group's arrival age, the time since its successor arrived at this hop, so a group of an untimed Track expires by arrival age alone. - A publisher resets the Group Stream of an expired, unfinished group with OLD rather than finishing it, even after sending every frame it holds. -- Added [YIELDED](#yield) (0x3C): a publisher out of stream credit resets the lowest-priority Group Stream with it, which pauses the Group for a later Group Stream to continue from where the reset stream started. -- A subscriber MAY end a Group Stream once a later stream for its Group arrives, rather than wait for its reset, and pauses a Group whose reset arrives without its error code as for YIELDED. +- A subscriber MAY end a Group Stream once a re-offer of its Group arrives, rather than wait for its reset. - FRAME leads with `Payload Length`, the payload size plus one, in place of the trailing `Message Length`; 0 is malformed. - A publisher SHOULD drop a datagram rather than block when the transport has no room for it, where it previously sent one only if the congestion controller could transmit it immediately. - Added an optional `Subscriber Max Age` to FETCH, encoded as milliseconds plus one with zero meaning absent. It can tighten, but never relax, `Publisher Max Age`. diff --git a/js/net/api/index.api.md b/js/net/api/index.api.md index 0a60027e0ec..ccf643817f6 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -646,7 +646,6 @@ export const StreamCode: ((code: number) => StreamCode) & { readonly TooFarBehind: StreamCode; readonly Unauthorized: StreamCode; readonly Unroutable: StreamCode; - readonly Yielded: StreamCode; }; export type StreamCode = number & { diff --git a/js/net/src/error.test.ts b/js/net/src/error.test.ts index b0eef03f9a0..fa73a4a18bf 100644 --- a/js/net/src/error.test.ts +++ b/js/net/src/error.test.ts @@ -249,7 +249,6 @@ test("stream codes match the draft's Stream Error Codes table", () => { FrameTooLarge: 0x38, TimestampMismatch: 0x39, Unauthorized: 0x3b, - Yielded: 0x3c, }); }); @@ -276,7 +275,6 @@ test("the code tables keep their registry ranges", () => { StreamCode.FrameTooLarge, StreamCode.TimestampMismatch, StreamCode.Unauthorized, - StreamCode.Yielded, ]; for (const code of Object.values(StreamCode)) { if (assignedLite.includes(code)) { @@ -404,7 +402,6 @@ test("toStreamCode and fromTransport agree on what a code means", () => { StreamCode.Unroutable, StreamCode.FrameTooLarge, StreamCode.Unauthorized, - StreamCode.Yielded, StreamCode(70), ]) { expect(toStreamCode(fromTransport(toTransport(code, "reset")))).toBe(code); @@ -438,7 +435,6 @@ test("toStreamCode: lite-only codes do not reach an IETF peer", () => { StreamCode.GroupTooLarge, StreamCode.Unroutable, StreamCode.FrameTooLarge, - StreamCode.Yielded, ]) { expect(toStreamCode(new StreamError(code), { version: Version.DRAFT_22 })).toBe(StreamCode.Internal); } diff --git a/js/net/src/error.ts b/js/net/src/error.ts index c4a9cedb871..48ee48fabfd 100644 --- a/js/net/src/error.ts +++ b/js/net/src/error.ts @@ -117,11 +117,6 @@ export const StreamCode = Object.freeze( Unroutable: 0x36 as StreamCode, /** The grant does not cover this request, or no longer does. The session stays up. */ Unauthorized: 0x3b as StreamCode, - /** - * The publisher reset a group's stream to make room for a higher-priority one, pausing the - * group for a later stream to continue rather than giving it up (lite-07). - */ - Yielded: 0x3c as StreamCode, /** A group grew past its cache budget and was aborted. */ GroupTooLarge: 0x32 as StreamCode, /** A frame's timedness or timestamp doesn't match its track's timescale. */ diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index b1ae87bc172..b38f5842472 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -2951,10 +2951,10 @@ function qmuxPair(protocol: string, maxStreamsUni: bigint): { client: Qmux; serv return { client, server }; } -// On lite-07 a group whose stream a newer one reset to make room only pauses: once credit returns, -// a new stream continues it, and its reader receives every frame without an error. Mirrors the Rust -// `a_reset_group_resumes_without_loss`. -test("integration: lite-07 resumes a group reset to make room without loss", async () => { +// On lite-07 a group whose stream a newer one reset to make room is dropped: its reader ends with +// TOO_FAR_BEHIND, and a reader that wants the rest FETCHes it whole. Mirrors the Rust +// `a_reset_group_is_fetched_whole`. +test("integration: lite-07 drops a group reset to make room, and a FETCH reads it whole", async () => { using clock = manual(); // Advance the clock until `work` settles: a waiting open makes room once it has waited about a // round trip. @@ -2980,29 +2980,30 @@ test("integration: lite-07 resumes a group reset to make room without loss", asy // Unfinished, so its stream holds the only credit until the newer group resets it. const older = producer.appendGroup(); older.writeFrame(textFrame("a")); - const paused = await settled(subscriber.recvGroup(), "the older group"); - if (!paused) throw new Error("no older group"); - expect(paused.sequence).toBe(0n); - expect(await settled(paused.readString(), "its first frame")).toBe("a"); + const reset = await settled(subscriber.recvGroup(), "the older group"); + if (!reset) throw new Error("no older group"); + expect(reset.sequence).toBe(0n); + expect(await settled(reset.readString(), "its first frame")).toBe("a"); const newer = producer.appendGroup(); newer.writeFrame(textFrame("new")); newer.finish(); const group = await settled(subscriber.recvGroup(), "the newer group taking the older one's credit"); expect(group?.sequence).toBe(1n); + expect(await settled(Promise.resolve(reset.closed), "the older group's end")).toMatchObject({ + code: StreamCode.TooFarBehind, + }); - // Written after the reset: a continuation carries them, behind the frame already held. older.writeFrame(textFrame("b")); - older.writeFrame(textFrame("c")); older.finish(); - const rest: string[] = []; + const fetched = await settled(remote.track("0").fetchGroup(0n), "the FETCH of the dropped group"); + const frames: string[] = []; for (;;) { - const frame = await settled(paused.readString(), "the paused group's continuation"); + const frame = await settled(fetched.readString(), "the fetched group"); if (frame === undefined) break; - rest.push(frame); + frames.push(frame); } - expect(rest).toEqual(["b", "c"]); - expect(await paused.closed).toBeNull(); + expect(frames).toEqual(["a", "b"]); } finally { subscriber.close(); remote.close(); diff --git a/js/net/src/internal.ts b/js/net/src/internal.ts index de11cacebdb..086ac82d599 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -527,7 +527,7 @@ export interface ResumeSource { /** A fresh reader for a group still cached by one of this reader's copies. */ cachedGroup(sequence: bigint, from?: number): GroupConsumer | undefined; /** See {@link hooks.reofferGroups}, answered by the serving copy. */ - reofferGroups(start: bigint, end: bigint, from?: number): { groups: GroupConsumer[]; givenUp: Run[] }; + reofferGroups(start: bigint, end: bigint): { groups: GroupConsumer[]; givenUp: Run[] }; /** * See {@link hooks.reofferStale}: the stale runs queued here, those this reader reported, which a * route change keeps, and those the serving copy reported. @@ -809,16 +809,9 @@ export const hooks: { * Fresh reads of the groups in `[start, end)` a subscriber's track still caches, its range still * admits, and its current budget no longer judges stale, each expiring against that budget, and * the runs still given up: for a wire whose update re-offers a group whose stream it gave up as - * stale, or a lite-07 continuation of a group whose stream yielded. Each read starts at frame - * `from`, so a group whose cache holds only frames past it is given up. Mirrors the Rust - * crate-private `track::Subscriber::reoffer_groups`. + * stale. Mirrors the Rust crate-private `track::Subscriber::reoffer_groups`. */ - reofferGroups: ( - subscriber: Subscriber, - start: bigint, - end: bigint, - from?: number, - ) => { groups: GroupConsumer[]; givenUp: Run[] }; + reofferGroups: (subscriber: Subscriber, start: bigint, end: bigint) => { groups: GroupConsumer[]; givenUp: Run[] }; /** Whether this subscriber's budget gives up on `sequence`, judged by its successor; see {@link expireGroup}. */ staleAt: (subscriber: Subscriber, sequence: bigint) => Expiry; /** A group the subscriber's track still caches, as a fresh read handle. */ diff --git a/js/net/src/lite/copy_fill.test.ts b/js/net/src/lite/copy_fill.test.ts index 38dede13033..45b4ea9cdc3 100644 --- a/js/net/src/lite/copy_fill.test.ts +++ b/js/net/src/lite/copy_fill.test.ts @@ -375,10 +375,9 @@ test("copy fill: a filled group nobody holds any more stops downloading", async } }); -// Lite-07: a filled group whose stream the publisher yielded, then gave up, ends with the drop the -// copy's subscription, which the fill rides, names it with: the group's own error, rather than -// waiting paused on a continuation that never comes. -test("copy fill: a yielded group its subscription's drop names ends", async () => { +// Lite-07: a filled group whose stream the session reset to make room ends with TOO_FAR_BEHIND, which +// the drop on the copy's subscription, which the fill rides, names too. +test("copy fill: a group reset to make room ends", async () => { const clock = new Manual(); const installed = clock.install(); const pair = createMockTransportPair(Lite.ALPN_07_WIP); @@ -418,10 +417,10 @@ test("copy fill: a yielded group its subscription's drop names ends", async () = const group = await history.recvGroup(); expect(group?.sequence).toBe(0n); expect(dec.decode((await group?.readFrame())?.payload)).toBe("old 0"); - const source = await archived; + await archived; - // The session resets the group's stream to make room, and the group is gone before a new - // stream could continue it, so the publisher drops it on the subscription the fill rides. + // The session resets the group's stream to make room, so the publisher drops it on the + // subscription the fill rides. const one = await history.recvGroup(); expect(one?.sequence).toBe(1n); while (await one?.readFrame()); @@ -430,15 +429,13 @@ test("copy fill: a yielded group its subscription's drop names ends", async () = const open = lines(pair.server).streams.list(); expect(open.length).toBe(1); open[0]?.preempt(); - source.abort(new StreamError(StreamCode(70), { message: "gone" })); - // Still paused without it, which the deadline, fired once every pending task settled, - // reports as elapsed. + // Within the deadline, which fires once every pending task settled. const ended = await clock .drive(timeout(Duration.millis(1000), Promise.resolve(group?.closed))) .catch((err: unknown) => err); expect(ended).toBeInstanceOf(StreamError); - expect((ended as StreamError).code).toBe(StreamCode(70)); + expect((ended as StreamError).code).toBe(StreamCode.TooFarBehind); history.close(); reader.close(); } finally { diff --git a/js/net/src/lite/group.test.ts b/js/net/src/lite/group.test.ts index 3edd11d2aac..6eb6bf88b7e 100644 --- a/js/net/src/lite/group.test.ts +++ b/js/net/src/lite/group.test.ts @@ -3,8 +3,6 @@ import { ProtocolViolation, StreamCode, StreamError } from "../error.ts"; import { Producer } from "../group.ts"; import { hooks } from "../internal.ts"; import { concat, encodeVarint, Reader } from "../stream.ts"; -import { Timestamp } from "../time.ts"; -import { SEGMENT_BYTES } from "../util/limits.ts"; import * as Varint from "../varint.ts"; import { frameDecoder, readFrames } from "./group.ts"; import { frameHeader } from "./publisher.ts"; @@ -162,63 +160,6 @@ test("a lite-07 FRAME with a Payload Length of 0 is malformed", async () => { expect((err as StreamError).code).toBe(StreamCode.MalformedTrack); }); -// One macrotask, so every reaction queued before it has run. -function nextTask(): Promise { - return new Promise((resolve) => { - const channel = new MessageChannel(); - channel.port1.onmessage = () => { - channel.port1.close(); - channel.port2.close(); - resolve(); - }; - channel.port2.postMessage(null); - }); -} - -// Whether `work` settles within a few event-loop turns. -async function settles(work: Promise): Promise { - let settled = false; - const done = () => { - settled = true; - }; - work.then(done, done); - for (let i = 0; i < 10 && !settled; i++) await nextTask(); - return settled; -} - -// A lite-07 continuation re-sending a held frame past one segment skips its payload. Once the group -// closes, the skip stops waiting on a peer that stopped sending, for a frame held whole and for the -// one a YIELDED reset cut short alike. -test.each([false, true])("a continuation's held-frame skip stops once the group closes (cut: %p)", async (cut) => { - const version = Version.DRAFT_07; - const size = SEGMENT_BYTES + 10; - const producer = new Producer(0n); - let partial: ReturnType | undefined; - if (cut) { - partial = producer.createFrame({ size, timestamp: Timestamp.unscaled(producer.sequence) }); - partial.write(new Uint8Array(5)); - } else producer.writeFrame({ payload: new Uint8Array([1]), timestamp: Timestamp.unscaled(producer.sequence) }); - - // The frame again, with fewer bytes than the skip needs, then nothing: the stream never ends. - let controller!: ReadableStreamDefaultController; - const stream = new Reader( - new ReadableStream({ - start: (c) => { - controller = c; - }, - }), - undefined, - version, - ); - controller.enqueue(concat([frameHeader(version, undefined, undefined, size, true), new Uint8Array(2)])); - const done = readFrames(stream, producer, { scale: 0, lengthFirst: true }, { index: 0, partial }); - expect(await settles(done)).toBe(false); - - producer.abort(new StreamError(StreamCode.Cancel)); - expect(await settles(done)).toBe(true); - await done; -}); - // Stream end at the Payload Length ends a lite-07 group; within the rest of the header, it cut a frame. test("a lite-07 stream ending at the Payload Length ends the group", async () => { const version = Version.DRAFT_07; @@ -242,14 +183,7 @@ test("a lite-07 frame numbered past the safe integer range is refused", async () const start = Number.MAX_SAFE_INTEGER - 1; const producer = new Producer(0n); hooks.startGroup(producer, start); - const err = await readFrames( - streamOf([frame, frame], version), - producer, - { scale: SCALE, lengthFirst: true }, - { - index: start, - }, - ).then( + const err = await readFrames(streamOf([frame, frame], version), producer, { scale: SCALE, lengthFirst: true }).then( () => undefined, (err: unknown) => err, ); diff --git a/js/net/src/lite/group.ts b/js/net/src/lite/group.ts index d22e9a1de1c..cc0a47a05d6 100644 --- a/js/net/src/lite/group.ts +++ b/js/net/src/lite/group.ts @@ -1,9 +1,8 @@ import { race } from "@moq/signals"; import { FrameTooLarge, StreamCode, StreamError } from "../error.ts"; -import type { Producer as FrameProducer } from "../frame.ts"; import type * as netGroup from "../group.ts"; import { hooks } from "../internal.ts"; -import { decodePayload, fillFrame, receiveFrame, skipPayload, type WirePayload } from "../payload.ts"; +import { decodePayload, receiveFrame, type WirePayload } from "../payload.ts"; import type { Cursor, Reader, Writer } from "../stream.ts"; import * as Time from "../time.ts"; import { toNumber } from "../util/bigint.ts"; @@ -131,35 +130,8 @@ export function frameDecoder({ scale, compress = false, lengthFirst = false }: F }; } -/** - * Where a lite-07 Group Stream is in its group, which a YIELDED reset pauses and a later stream - * continues: frames the group holds already are skipped, and the frame the reset cut short is - * completed. Mirrors the Rust `FrameIngest` on a yielding stream. - * - * @internal - */ -export interface Ingest { - /** The group index of the next frame on the stream, counted from its `Frame Start`. */ - index: number; - /** - * The frame a YIELDED reset cut short, holding the group's writer: kept on a pause, and - * completed by the continuation. - */ - partial?: FrameProducer; -} - -/** - * Write a group stream's frames into `producer` until the stream ends or the producer closes. - * - * With `ingest`, a stream continuing a paused group skips the frames the group holds, and - * completes the one a YIELDED reset cut short; ending before completing it is malformed. - */ -export async function readFrames( - stream: Reader, - producer: netGroup.Producer, - format: FrameFormat, - ingest?: Ingest, -): Promise { +/** Write a group stream's frames into `producer` until the stream ends or the producer closes. */ +export async function readFrames(stream: Reader, producer: netGroup.Producer, format: FrameFormat): Promise { const decode = frameDecoder(format); for (;;) { @@ -167,64 +139,13 @@ export async function readFrames( // per batch rather than once per frame. Only the group's own stream ends it: a track // that closes first has already closed (or aborted) this group through its cache. const frame = stream.tryDecode(decode) ?? (await race([stream.decodeMaybe(decode), producer.closed])); - if (frame === undefined && ingest?.partial) - throw malformed("a continuation ended before completing the frame it resumed"); if (!frame || frame instanceof Error) return; - if (ingest) { - const index = ingest.index++; - const held = hooks.groupFrameCount(producer); - if (index < held) { - if (!(await skipHeld(stream, producer, frame, ingest, index + 1 === held))) return; - continue; - } - } if (frame.payload) hooks.writeGroupFrame( producer, { payload: frame.payload, timestamp: Time.received(frame.timestamp, producer.sequence) }, frame.inflated, ); - else if (ingest) { - const timestamp = Time.received(frame.timestamp, producer.sequence); - const open = producer.createFrame({ size: frame.size, timestamp }); - // Kept until it completes, so a YIELDED reset cutting it short leaves it for the continuation. - ingest.partial = open; - if (!(await fillFrame(stream, producer, open))) return; - ingest.partial = undefined; - } else if (!(await receiveFrame(stream, producer, frame))) return; + else if (!(await receiveFrame(stream, producer, frame))) return; } } - -// A continuation re-sending a frame the group holds: discard it, or for the frame a YIELDED reset -// cut short (the `last` one held), discard the bytes held and complete it. Resolves false if the -// group closed first. -async function skipHeld( - stream: Reader, - producer: netGroup.Producer, - frame: WireFrame, - ingest: Ingest, - last: boolean, -): Promise { - const partial = last ? ingest.partial : undefined; - if (!partial) return frame.payload !== undefined || (await skipUnlessClosed(stream, producer, frame.size)); - // The same frame, so the same size: a publisher reusing the group's identity is refused - // rather than written past. - if (partial.size !== frame.size) throw malformed("a continuation resent a frame at another size"); - const skip = partial.size - partial.remaining; - if (frame.payload) { - partial.write(frame.payload.subarray(skip)); - partial.finish(); - } else { - if (!(await skipUnlessClosed(stream, producer, skip))) return false; - if (!(await fillFrame(stream, producer, partial))) return false; - } - ingest.partial = undefined; - return true; -} - -// Read past `size` bytes of a held payload, as `fillFrame` reads one in: resolves false if the group -// closed first, so a peer that stops sending holds neither the stream nor its task. -async function skipUnlessClosed(stream: Reader, producer: netGroup.Producer, size: number): Promise { - await race([skipPayload(stream, size), producer.closed]); - return producer.closed.peek() === undefined; -} diff --git a/js/net/src/lite/publisher.test.ts b/js/net/src/lite/publisher.test.ts index a4f33c9dc93..0bc4cee2bd7 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -937,15 +937,15 @@ test("lite-07 SUBSCRIBE_START names each resolved range start", async () => { } }); -// Lite-07: a group whose stream the session reset to make room continues on a new stream from the -// reset one's Frame Start, even where the cache holds only the group's tail from there, rather than -// being dropped as OLD. -test("lite-07 continues a yielded cached tail from its Frame Start", async () => { - const version = Version.DRAFT_07; - const sub = await servedSubscription({ version, startGroup: 0n, startFrame: 5 }); +// A group whose stream the session reset to make room is a final drop on every version: the +// publisher names it in SUBSCRIBE_DROP with TOO_FAR_BEHIND and opens no second stream for it. +test.each([ + { name: "lite-06", version: Version.DRAFT_06 }, + { name: "lite-07", version: Version.DRAFT_07 }, +])("$name drops a group reset to make room", async ({ version }) => { + const sub = await servedSubscription({ version, startGroup: 0n }); try { - // A received tail: the cache holds the group from frame 5, where the subscription starts. - const group = hooks.receiveGroup(sub.track, { sequence: 0n, start: 5 }); + const group = sub.track.createGroup(0n); group.writeFrame(textFrame("a", Timestamp.fromMillis(0))); expect(await sub.servedHeader()).toBe(0n); expect("start" in (await decodeSubscribeResponse(sub.client.reader, version))).toBe(true); @@ -957,36 +957,16 @@ test("lite-07 continues a yielded cached tail from its Frame Start", async () => group.writeFrame(textFrame("b", Timestamp.fromMillis(1))); group.finish(); - expect(await sub.servedGroup()).toEqual({ sequence: 0n, frameStart: 5, payloads: ["a", "b"] }); - - // Responses arrive in order, so a drop naming the continued group would precede the end. + const resp = await decodeSubscribeResponse(sub.client.reader, version); + if (!("drop" in resp)) throw new Error("expected SUBSCRIBE_DROP"); + expect([resp.drop.start, resp.drop.end]).toEqual([0n, 1n]); + // Before lite-07 a drop's code is the application's. + if (version === Version.DRAFT_07) expect(resp.drop.error).toBe(Number(StreamCode.TooFarBehind)); + // Responses arrive in order, and nothing serves the group again. sub.track.finish(); const after = await decodeSubscribeResponse(sub.client.reader, version); expect(Object.keys(after)).toEqual(["finish"]); - } finally { - await sub.close(); - } -}); - -// Lite-07: a yielded group the publisher aborts before its continuation is dropped with the -// publisher's error, not as stale. Mirrors the Rust `lite07_yielded_group_aborted_drops_with_its_error`. -test("lite-07 drops a yielded group the publisher aborted with its error", async () => { - const version = Version.DRAFT_07; - const sub = await servedSubscription({ version, startGroup: 0n }); - try { - const group = sub.track.createGroup(0n); - group.writeFrame(textFrame("a", Timestamp.fromMillis(0))); - expect(await sub.servedHeader()).toBe(0n); - expect("start" in (await decodeSubscribeResponse(sub.client.reader, version))).toBe(true); - - const [open] = lines(sub.quic).streams.list(); - if (!open) throw new Error("the group stream is not open"); - open.preempt(); - group.abort(new StreamError(StreamCode(70), { message: "cut" })); - - const resp = await decodeSubscribeResponse(sub.client.reader, version); - if (!("drop" in resp)) throw new Error("expected SUBSCRIBE_DROP"); - expect([resp.drop.start, resp.drop.end, resp.drop.error]).toEqual([0n, 1n, 70]); + expect(lines(sub.quic).streams.list()).toEqual([]); } finally { await sub.close(); } diff --git a/js/net/src/lite/publisher.ts b/js/net/src/lite/publisher.ts index 2bcc23536ed..abf84551655 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -98,7 +98,6 @@ import { hasRoutes, hasSkip, hasTrackStream, - hasYield, keepsSubscribeOpen, resolvesStart, Version, @@ -1618,19 +1617,6 @@ export class Publisher { const pendingDrops: { start: bigint; end: bigint; error: Error }[] = []; // The expired groups an update re-offers, still cached and in range, served again in turn. const reoffered: group.Consumer[] = []; - // Lite-07: a fresh read of the group `serving` served, whose stream the session reset to make - // room, for a new stream to continue it at the same frames; undefined once the subscription's - // budget or the cache gave it up. The Rust publisher keeps an unread clone of the reset - // stream's cursor instead, which a read handle here cannot make. - const continuation = (serving: RunGroup): RunGroup | undefined => { - const sequence = serving.group.sequence; - // From the reset stream's Frame Start, which a cache holding only the group's tail can serve. - const [resumed, ...extra] = hooks.reofferGroups(track, sequence, sequence + 1n, serving.start).groups; - for (const other of extra) other.close(); - if (!resumed) return undefined; - return { ...serving, group: resumed, givenUp: new GivenUp(resumed) }; - }; - // Cancels groups still queued for a stream slot. Only the subscriber leaving counts: // a track that ran out of groups still has to flush the ones already queued, and the // caller FINs the subscribe stream to say so. @@ -1993,44 +1979,19 @@ export class Publisher { start: range.start, end: range.end, }; - // The FIN waits for every stream to finish or reset, including one that has not opened yet, - // and a paused group's continuation. + // The FIN waits for every stream to finish or reset, including one that has not opened yet. + // A stream reset to make room fails the group like any other reset, so it is dropped. const task = (async () => { - let serving = options; - for (;;) { - const writer = await this.#openGroup(serving); - let failed: Error | undefined; - try { - failed = writer instanceof Error ? writer : await this.#serveGroup(writer, serving); - } finally { - serving.givenUp.close(); - } - if (!failed) return; - // Lite-07: the session reset the stream to make room, which only pauses the group. - // The subscriber keeps what arrived, and a new stream continues the group from - // the reset one's Frame Start, owing no drop. Mirrors the Rust `GroupServe::settle_failed`. - if (!(writer instanceof Error) && writer.preempted && hasYield(this.version)) { - // The group's own failure, such as the publisher's abort or an eviction, ends - // it with that error, as the Rust continuation reads it off the group. Read - // before the release replaces it with the reset's. - const fate = serving.group.closed.peek(); - hooks.releaseGroupReader(serving.group, failed); - const resumed = fate instanceof Error ? undefined : continuation(serving); - if (resumed) { - console.debug( - `continuing paused group: track=${track.name} sequence=${group.sequence} frame_start=${serving.start}`, - ); - serving = resumed; - continue; - } - // Given up meanwhile, such as by the subscription's budget: the usual stale drop. - failed = fate instanceof Error ? fate : new StreamError(StreamCode.Old); - } - if (emitRange) { - pendingDrops.push({ start: group.sequence, end: group.sequence + 1n, error: failed }); - drops.update((revision) => revision + 1); - } - return; + const writer = await this.#openGroup(options); + let failed: Error | undefined; + try { + failed = writer instanceof Error ? writer : await this.#serveGroup(writer, options); + } finally { + options.givenUp.close(); + } + if (failed && emitRange) { + pendingDrops.push({ start: group.sequence, end: group.sequence + 1n, error: failed }); + drops.update((revision) => revision + 1); } })(); groups.add(task); @@ -2492,8 +2453,6 @@ export class Publisher { sendOrder: place.rank, cancel: race([unsubscribed, abandoned]), group: true, - // Lite-07: a reset to make room pauses the group, which a new stream continues. - yields: hasYield(this.version), }); } finally { opened.resolve(); @@ -2598,9 +2557,7 @@ export class Publisher { } catch (err: unknown) { const e = error(err); stream.reset(e); - // A reset that pauses the group leaves the reader to the caller, which reads its fate - // first. A reset stream is never preempted after, so the caller sees the same flag. - if (!(stream.preempted && hasYield(this.version))) hooks.releaseGroupReader(group, e); + hooks.releaseGroupReader(group, e); return e; } finally { priority.remove(stream); diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 6ca2d3ec1e5..05f63e1273e 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1419,218 +1419,13 @@ function handStream(version: Version) { }; } -// Lite-07: a stream reset with YIELDED pauses its group, keeping the frame it cut short, and a later -// stream continues it from the paused stream's Frame Start, whether it lands after that reset or -// overtakes it, which ends the paused stream at once, since a browser can lose its reset. The -// group's reader gets every frame once, then its end. Mirrors the Rust -// `a_yielded_group_continues_without_loss`, which waits for the overtaken reset instead. -test.each([false, true])("a yielded group continues without loss (overtakes: %p)", async (overtakes) => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, toHop(1n)); - const track = subscriber - .consume(Path.from("room")) - .track("video") - .subscribe({ delay: { max: Duration.millis(60_000) } }); - try { - await opened(0); - await answerTrackInfo(streams[0], version, 1000); - await (await opened(1)).reading; - - // Frame 1 streams in past one segment, so the reset cuts it short partway. - const big = new Uint8Array(SEGMENT_BYTES + 7).map((_, i) => i % 251); - const frames = [ - { delta: 300, payload: new TextEncoder().encode("zero") }, - { delta: 10, payload: big }, - { delta: 10, payload: new TextEncoder().encode("three") }, - ]; - const first = handStream(version); - first.write(await liteFrame(frames[0].delta, frames[0].payload)); - first.write(await liteFrame(frames[1].delta, frames[1].payload, SEGMENT_BYTES / 2)); - const second = handStream(version); - for (const { delta, payload } of frames) second.write(await liteFrame(delta, payload)); - second.finish(); - - const header = new Group({ subscribe: 0n, sequence: 3n, frameStart: 0 }); - const yielded = subscriber.runGroup(header, first.reader); - const group = await track.recvGroup(); - expect(group?.sequence).toBe(3n); - expect((await group?.readFrame())?.payload).toEqual(frames[0].payload); - - let continued: Promise; - if (overtakes) { - const stop = spyOn(first.reader, "stop"); - continued = subscriber.runGroup(header, second.reader); - await yielded; - expect(stop).toHaveBeenCalled(); - } else { - first.reset(StreamCode.Yielded); - await yielded; - await drainReactions(); - expect(group?.closed.peek()).toBeUndefined(); - continued = subscriber.runGroup(header, second.reader); - } - await continued; - - for (const { payload } of frames.slice(1)) { - const frame = await group?.readFrame(); - expect(frame?.payload.byteLength).toBe(payload.byteLength); - expect(Buffer.compare(frame?.payload ?? new Uint8Array(), payload)).toBe(0); - } - expect(await group?.readFrame()).toBeUndefined(); - expect(await group?.closed).toBeNull(); - } finally { - track.close(); - subscriber.close(); - } -}); - -// Lite-07: a continuation is reset with YIELDED too, pausing the group again, and a third stream -// finishes it. Each pause keeps what arrived, and the reader gets every frame once. -test("a group yielded twice continues without loss", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, toHop(1n)); - const track = subscriber - .consume(Path.from("room")) - .track("video") - .subscribe({ delay: { max: Duration.millis(60_000) } }); - try { - await opened(0); - await answerTrackInfo(streams[0], version, 1000); - await (await opened(1)).reading; - - const frames = ["zero", "one", "two"].map((text, i) => ({ - delta: i === 0 ? 300 : 10, - payload: new TextEncoder().encode(text), - })); - const header = new Group({ subscribe: 0n, sequence: 3n, frameStart: 0 }); - const group = (async () => track.recvGroup())(); - - // Each stream starts at frame 0 and carries one frame more than the last before its reset. - for (const sent of [1, 2]) { - const stream = handStream(version); - for (const { delta, payload } of frames.slice(0, sent)) stream.write(await liteFrame(delta, payload)); - const running = subscriber.runGroup(header, stream.reader); - await drainReactions(); - stream.reset(StreamCode.Yielded); - await running; - await drainReactions(); - expect((await group)?.closed.peek(), `after ${sent} frames`).toBeUndefined(); - } - const last = handStream(version); - for (const { delta, payload } of frames) last.write(await liteFrame(delta, payload)); - last.finish(); - await subscriber.runGroup(header, last.reader); - - const reader = await group; - expect(reader?.sequence).toBe(3n); - for (const { payload } of frames) expect((await reader?.readFrame())?.payload).toEqual(payload); - expect(await reader?.readFrame()).toBeUndefined(); - expect(await reader?.closed).toBeNull(); - } finally { - track.close(); - subscriber.close(); - } -}); - -// Lite-07: a drop can overtake the YIELDED reset of the stream it names. The group ends with the -// drop's error rather than waiting for a continuation the publisher never sends. Mirrors the Rust -// `a_yielded_group_a_drop_named_ends`. -test("a yielded group a drop named ends", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, toHop(1n)); - const track = subscriber - .consume(Path.from("room")) - .track("video") - .subscribe({ delay: { max: Duration.millis(60_000) } }); - try { - await opened(0); - await answerTrackInfo(streams[0], version, 1000); - const request = await opened(1); - await request.reading; - await respond(request, version, [{ start: new SubscribeStart([0n]) }]); - - const first = handStream(version); - first.write(await liteFrame(300, new Uint8Array([1]))); - first.write(await liteFrame(10, new Uint8Array([2, 3]), 1)); - const yielded = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n }), first.reader); - const group = await track.recvGroup(); - expect(group?.sequence).toBe(3n); - - // The publisher gave the group up after pausing it, and its drop lands first. - await respond(request, version, [{ drop: new SubscribeDrop({ start: 3n, end: 4n, error: StreamCode.Old }) }]); - await drainReactions(); - first.reset(StreamCode.Yielded); - await yielded; - - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); - const read = await group?.readFrame().then( - () => undefined, - (err: unknown) => err, - ); - expect(read).toBeInstanceOf(StreamError); - expect((read as StreamError).code).toBe(StreamCode.Old); - } finally { - track.close(); - subscriber.close(); - } -}); - -// Lite-07: a stale drop that overtook the YIELDED reset of the stream it names holds only under the -// update it names: once the subscriber sends another, the reset pauses the group, which the -// publisher then continues or drops again. Mirrors the Rust -// `a_yielded_group_pauses_once_an_update_supersedes_its_stale_drop`. -test("a yielded group pauses once an update supersedes its stale drop", async () => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, toHop(1n)); - const track = subscriber - .consume(Path.from("room")) - .track("video") - .subscribe({ delay: { max: Duration.millis(60_000) } }); - try { - await opened(0); - await answerTrackInfo(streams[0], version, 1000); - const request = await opened(1); - await request.reading; - await respond(request, version, [{ start: new SubscribeStart([0n]) }]); - - const first = handStream(version); - first.write(await liteFrame(300, new Uint8Array([1]))); - const yielded = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n }), first.reader); - const group = await track.recvGroup(); - expect(group?.sequence).toBe(3n); - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); - - await respond(request, version, [{ drop: new SubscribeDrop({ start: 3n, end: 4n, error: StreamCode.Old }) }]); - await drainReactions(); - track.update({ priority: 1, delay: { max: Duration.millis(60_000) } }); - await drainReactions(); - first.reset(StreamCode.Yielded); - await yielded; - await drainReactions(); - - expect(group?.closed.peek()).toBeUndefined(); - // The continuation picks the group up where the reset left it. - const second = handStream(version); - second.write(await liteFrame(10, new Uint8Array([2]))); - second.finish(); - await subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n, frameStart: 1 }), second.reader); - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([2])); - expect(await group?.readFrame()).toBeUndefined(); - } finally { - track.close(); - subscriber.close(); - } -}); - -// Lite-07: a stale drop that overtook the header of the stream it names holds only under the update -// it names: once the subscriber sends another, a YIELDED reset of that stream pauses the group. -// Mirrors the Rust `a_yielded_group_pauses_once_an_update_supersedes_a_stale_drop_before_its_header`. -test("a yielded group pauses once an update supersedes a stale drop before its header", async () => { - const version = Version.DRAFT_07; +// A Group Stream the publisher reset to make room for a higher-priority one is a final drop on every +// version: the group ends with TOO_FAR_BEHIND, keeping what arrived, and the drop that follows the +// reset changes nothing. Mirrors the Rust `a_group_reset_to_make_room_ends`. +test.each([ + { name: "lite-06", version: Version.DRAFT_06 }, + { name: "lite-07", version: Version.DRAFT_07 }, +])("$name: a group reset to make room ends", async ({ version }) => { const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, toHop(1n)); const track = subscriber @@ -1644,29 +1439,28 @@ test("a yielded group pauses once an update supersedes a stale drop before its h await request.reading; await respond(request, version, [{ start: new SubscribeStart([0n]) }]); - await respond(request, version, [{ drop: new SubscribeDrop({ start: 3n, end: 4n, error: StreamCode.Old }) }]); - await drainReactions(); const first = handStream(version); - first.write(await liteFrame(300, new Uint8Array([1]))); - const yielded = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n }), first.reader); + const frame = hasLengthFirst(version) + ? await liteFrame(300, new Uint8Array([1])) + : await Writer.collect(version, async (w) => { + await w.u62(600n); + await w.u53(1); + await w.write(new Uint8Array([1])); + }); + first.write(frame); + const running = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n }), first.reader); const group = await track.recvGroup(); expect(group?.sequence).toBe(3n); expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); - track.update({ priority: 1, delay: { max: Duration.millis(60_000) } }); - await drainReactions(); - first.reset(StreamCode.Yielded); - await yielded; + first.reset(StreamCode.TooFarBehind); + await running; + expect(await group?.closed).toMatchObject({ code: StreamCode.TooFarBehind }); + await respond(request, version, [ + { drop: new SubscribeDrop({ start: 3n, end: 4n, error: StreamCode.TooFarBehind }) }, + ]); await drainReactions(); - - expect(group?.closed.peek()).toBeUndefined(); - // The continuation picks the group up where the reset left it. - const second = handStream(version); - second.write(await liteFrame(10, new Uint8Array([2]))); - second.finish(); - await subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n, frameStart: 1 }), second.reader); - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([2])); - expect(await group?.readFrame()).toBeUndefined(); + expect(await group?.closed).toMatchObject({ code: StreamCode.TooFarBehind }); } finally { track.close(); subscriber.close(); @@ -1674,9 +1468,9 @@ test("a yielded group pauses once an update supersedes a stale drop before its h }); // Lite-07: Firefox reports a reset that lands before the page read the stream's data as a bare -// TypeError, dropping its code. The publisher owes that group a continuation or a drop, so the -// group pauses as on YIELDED, and whichever comes settles it. -test.each(["continued", "dropped"] as const)("a reset without its code pauses the group (%s)", async (then) => { +// TypeError, dropping its code. The group ends with it, as with any other reset; the drop that +// follows names the reason. +test("a reset without its code ends the group", async () => { const version = Version.DRAFT_07; const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, toHop(1n)); @@ -1698,28 +1492,7 @@ test.each(["continued", "dropped"] as const)("a reset without its code pauses th expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); first.error(new TypeError("Error in input stream")); await running; - await drainReactions(); - expect(group?.closed.peek()).toBeUndefined(); - - if (then === "continued") { - const second = handStream(version); - second.write(await liteFrame(300, new Uint8Array([1]))); - second.write(await liteFrame(10, new Uint8Array([2]))); - second.finish(); - await subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n }), second.reader); - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([2])); - expect(await group?.readFrame()).toBeUndefined(); - expect(await group?.closed).toBeNull(); - } else { - await respond(request, version, [ - { drop: new SubscribeDrop({ start: 3n, end: 4n, error: StreamCode.NotFound }) }, - ]); - const err = await group?.readFrame().then( - () => undefined, - (e: unknown) => e, - ); - expect((err as StreamError).code).toBe(StreamCode.NotFound); - } + expect(await group?.closed).toBeInstanceOf(Error); } finally { track.close(); subscriber.close(); @@ -1838,83 +1611,6 @@ test("a stream whose gap the grace gave up on still holds the end", async () => } }); -// Lite-07: a recovery holds its FIN for a group a reset without its code paused, as a subscription's -// end does, rather than end the continuation the publisher still owes it. -test("a recovery waits for the continuation of a group a reset without its code paused", async () => { - const clock = new Manual(); - const installed = clock.install(); - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, toHop(1n)); - const consumed = subscriber.consume(Path.from("room")); - const live = consumed.track("video").subscribe({ delay: { max: null } }); - let history: ReturnType["subscribe"]> | undefined; - try { - await opened(0); - await answerTrackInfo(streams[0], version, 1000); - const request = await opened(1); - await request.reading; - await respond(request, version, [{ start: new SubscribeStart([0n]) }]); - // The subscription delivers groups 0 and 1, which age out of the cache below the live edge, - // so it never sends them again. - for (const sequence of [0n, 1n, 10n]) { - const stream = handStream(version); - stream.finish(); - await subscriber.runGroup(new Group({ subscribe: 0n, sequence }), stream.reader); - } - await clock.advance(DEFAULT_EXPIRY.mul(2)); - - // A reader asks for them again, which a recovery of their own answers. - history = consumed.track("video").subscribe({ - delay: { max: null }, - order: "ascending", - ranges: [{ start: { absolute: 0n }, end: { absolute: 2n } }], - }); - const zeroRead = history.recvGroup(); - const recovery = await opened(2); - await recovery.reading; - await respond(recovery, version, [{ start: new SubscribeStart([0n]) }]); - // Over once it FINs, or once the fills cancel it with nothing left to read. - let over = false; - void Promise.race([recovery.finished, recovery.aborted]).then(() => { - over = true; - }); - - const first = handStream(version); - first.write(await liteFrame(0, new Uint8Array([1]))); - void subscriber.runGroup(new Group({ subscribe: 1n, sequence: 0n }), first.reader).catch(() => {}); - const zero = await zeroRead; - expect(zero?.sequence).toBe(0n); - expect((await zero?.readFrame())?.payload).toEqual(new Uint8Array([1])); - const one = handStream(version); - one.finish(); - await subscriber.runGroup(new Group({ subscribe: 1n, sequence: 1n }), one.reader); - await drainReactions(); - expect(over).toBe(false); - // Firefox reports the YIELDED reset as a bare TypeError, which pauses the group. - first.error(new TypeError("Error in input stream")); - await drainReactions(); - await clock.advance(GRACE.div(2)); - expect(over).toBe(false); - - // The continuation completes the group, which lets the recovery end. - const second = handStream(version); - second.write(await liteFrame(0, new Uint8Array([1]))); - second.write(await liteFrame(10, new Uint8Array([2]))); - second.finish(); - await subscriber.runGroup(new Group({ subscribe: 1n, sequence: 0n }), second.reader); - expect((await zero?.readFrame())?.payload).toEqual(new Uint8Array([2])); - expect(await zero?.readFrame()).toBeUndefined(); - await drainReactions(); - expect(over).toBe(true); - } finally { - history?.close(); - live.close(); - subscriber.close(); - installed.close(); - } -}); - // Lite-07: an update reopens a stale drop, so a stream that drop excused holds the end again // until it finishes, even once the grace the end started before the update has passed. test("an update restores the hold a stale drop excused", async () => { @@ -1962,9 +1658,9 @@ test("an update restores the hold a stale drop excused", async () => { } }); -// Lite-07: a continuation judged against a stale drop while it waits out the copy it replaces holds -// the end if an update reopened that drop meanwhile, as the copy it replaces would have. -test("an update while a continuation waits keeps its hold on the end", async () => { +// Lite-07: a re-offer judged against a stale drop while it waits out the copy it replaces holds the +// end if an update reopened that drop meanwhile, as the copy it replaces would have. +test("an update while a re-offer waits keeps its hold on the end", async () => { const clock = new Manual(); const installed = clock.install(); const version = Version.DRAFT_07; @@ -1987,8 +1683,8 @@ test("an update while a continuation waits keeps its hold on the end", async () const group = await track.recvGroup(); expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); - // SUBSCRIBE_FINISH starts the end's grace. The continuation arrives under the stale drop, and - // the update lands while it waits out the first copy. + // SUBSCRIBE_FINISH starts the end's grace. The re-offer arrives under the stale drop, and the + // update lands while it waits out the first copy. await respond(request, version, [{ finish: new SubscribeFinish(1n) }]); await drainReactions(); const second = handStream(version); @@ -2014,89 +1710,32 @@ test("an update while a continuation waits keeps its hold on the end", async () } }); -// Lite-07: a malformed FRAME on a Group Stream ends only that stream and its group, never the -// session: a Payload Length of 0, or a continuation that resends the cut frame at another size, ends -// before completing it, or starts past the frames held. Mirrors the Rust `UniState::Group`. -test.each(["zero length", "resized", "unfinished", "past held"] as const)( - "a malformed lite-07 group stream ends only its group (%s)", - async (fault) => { - const version = Version.DRAFT_07; - const { quic, streams, opened } = fakeSession(); - const subscriber = new Subscriber(quic, version, toHop(1n)); - const track = subscriber - .consume(Path.from("room")) - .track("video") - .subscribe({ delay: { max: Duration.millis(60_000) } }); - try { - await opened(0); - await answerTrackInfo(streams[0], version, 1000); - await (await opened(1)).reading; - - const zero = new TextEncoder().encode("zero"); - // Past one segment, so the reset cuts it short partway. - const cut = new Uint8Array(SEGMENT_BYTES + 7); - const header = new Group({ subscribe: 0n, sequence: 3n, frameStart: 0 }); - const first = handStream(version); - first.write(await liteFrame(300, zero)); - if (fault === "zero length") first.write(await Writer.collect(version, (w) => w.u53(0))); - else first.write(await liteFrame(10, cut, SEGMENT_BYTES / 2)); - let running = subscriber.runGroup(header, first.reader); - const group = await track.recvGroup(); - expect((await group?.readFrame())?.payload).toEqual(zero); - - if (fault !== "zero length") { - first.reset(StreamCode.Yielded); - await running; - await drainReactions(); - expect(group?.closed.peek()).toBeUndefined(); - const second = handStream(version); - if (fault === "past held") { - // Only frame 0 is held whole, so frame 2 would leave a hole. - running = subscriber.runGroup( - new Group({ subscribe: 0n, sequence: 3n, frameStart: 2 }), - second.reader, - ); - } else { - second.write(await liteFrame(300, zero)); - if (fault === "resized") second.write(await liteFrame(10, new Uint8Array(cut.byteLength + 1))); - running = subscriber.runGroup(header, second.reader); - } - second.finish(); - } - - // The stream ends without the session: nothing escapes for the connection to close it with. - await expect(running).resolves.toBeUndefined(); - expect(await group?.closed).toMatchObject({ code: StreamCode.MalformedTrack }); - } finally { - track.close(); - subscriber.close(); - } - }, -); - -// Before lite-07 a YIELDED reset is no pause: the group ends with it, as with any other reset. -test("a lite-06 group reset with YIELDED ends", async () => { - const version = Version.DRAFT_06; +// Lite-07: a malformed FRAME on a Group Stream, such as a Payload Length of 0, ends only that stream +// and its group, never the session. Mirrors the Rust `UniState::Group`. +test("a malformed lite-07 group stream ends only its group", async () => { + const version = Version.DRAFT_07; const { quic, streams, opened } = fakeSession(); const subscriber = new Subscriber(quic, version, toHop(1n)); - const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + const track = subscriber + .consume(Path.from("room")) + .track("video") + .subscribe({ delay: { max: Duration.millis(60_000) } }); try { await opened(0); await answerTrackInfo(streams[0], version, 1000); await (await opened(1)).reading; + + const zero = new TextEncoder().encode("zero"); const first = handStream(version); - const frame = await Writer.collect(version, async (w) => { - await w.u62(600n); - await w.u53(1); - await w.write(new Uint8Array([1])); - }); - first.write(frame); - const yielded = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), first.reader); + first.write(await liteFrame(300, zero)); + first.write(await Writer.collect(version, (w) => w.u53(0))); + const running = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n, frameStart: 0 }), first.reader); const group = await track.recvGroup(); - first.reset(StreamCode.Yielded); - await yielded; - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); - expect(await group?.closed).toMatchObject({ code: StreamCode.Yielded }); + expect((await group?.readFrame())?.payload).toEqual(zero); + + // The stream ends without the session: nothing escapes for the connection to close it with. + await expect(running).resolves.toBeUndefined(); + expect(await group?.closed).toMatchObject({ code: StreamCode.MalformedTrack }); } finally { track.close(); subscriber.close(); @@ -2104,8 +1743,8 @@ test("a lite-06 group reset with YIELDED ends", async () => { }); // A lite-07 re-offer can overtake the reset of the stream it replaces. It proves that reset, which -// a browser can lose, so it ends that copy at once and continues the group from the frames it -// holds: a reader already handed the group reads on, and a later one reads it whole. +// a browser can lose, so it ends that copy at once and takes the group over: a reader already handed +// the group reads on, and a later one reads it whole. test("a re-offer ends the copy still arriving and continues its group", async () => { const version = Version.DRAFT_07; const { quic, streams, opened } = fakeSession(); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 87c757209da..ae892911c87 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -23,7 +23,6 @@ import { unauthorized, } from "../error.ts"; import { fillChanged, type Judged, rejectGroups } from "../fill.ts"; -import type { Producer as FrameProducer } from "../frame.ts"; import * as netGroup from "../group.ts"; import { Cost, type Hop, MAX_HOPS, routesEqual, UNKNOWN_HOP, WireRoute } from "../hop.ts"; import * as Instance from "../instance.ts"; @@ -82,7 +81,7 @@ import { closeViolation } from "./error.ts"; import { Fetch as FetchMessage } from "./fetch.ts"; import { dropped, passing, RangeFills, type RecoveryHandle, type Refusal, type Refused, refuseRun } from "./fills.ts"; import { type Carrier, type Claim, Floors } from "./floors.ts"; -import { type Group as GroupMessage, type Ingest, readFrames } from "./group.ts"; +import { type Group as GroupMessage, readFrames } from "./group.ts"; import { Probe } from "./probe.ts"; import { ProbeLevel, type Setup } from "./setup.ts"; import { StreamId } from "./stream.ts"; @@ -121,7 +120,6 @@ import { hasSynced, hasTrackStream, hasTracks, - hasYield, keepsSubscribeOpen, resolvesStart, updateSupported, @@ -257,109 +255,38 @@ interface SubscribeEntry { fillsChanged: Signal; // Until the SUBSCRIBE's first response (lite-05+); see `Unanswered`. unanswered?: Unanswered; - // Lite-07: the groups a YIELDED reset paused, once one did or a stream waits on one. - paused?: Pauses; - // Lite-07: whether a YIELDED reset pauses the group each stream is reading, by sequence, one - // per stream, so stream credit bounds them. - yielding?: Map; + // Lite-07: the drops naming the group each stream is reading, by sequence, one per stream, so + // stream credit bounds them. + reading?: Map; } -// Lite-07: the groups whose stream the publisher reset with YIELDED, each held open for a later -// stream to continue, by sequence. Mirrors the Rust `Paused`. -interface Pauses { - groups: Map; - // How many were held after the last sweep for groups the cache gave up on, which the next - // waits for twice as many. - swept: number; - // Counts the pauses, waking a stream held behind a copy still arriving once that copy pauses. - changed: Signal; -} - -// A group a YIELDED reset paused: what arrived stays, and its stream still counts toward the -// subscription's end until a continuation replaces it or a drop names the group. -interface PausedGroup { - group: netGroup.Producer; - // The frame the reset cut short, holding the group's writer. - partial?: FrameProducer; - // Marks the paused stream read, letting go of the subscription's end. - read: () => void; -} - -// Whether a YIELDED reset of a stream being read pauses its group. -interface Yielding { - verdict: YieldVerdict; - // The stream's hold on the subscription's end, which a drop naming its group excuses while - // that drop stands: see `excused`. +// Lite-07: a Group Stream being read, and the drops that name its group. A browser can lose the +// reset of a stream the publisher gave up on (Chromium drops one that reaches a stream before the +// page took it), so a drop that stands lets the subscription's end wait for the stream at most the +// grace. +interface Reading { + // The stream's hold on the subscription's end, which a standing drop excuses: see `excused`. counted: Counted; - // End the stream as reset with YIELDED: a later stream for its group proves the publisher - // reset it, even where the browser lost that reset. + // A final drop named the group, before or after its header. + dropped: boolean; + // The update a stale drop naming the group was sent under, which only that update upholds. + staleUnder?: bigint; + // End the stream as the reset it was: a re-offer of its group proves the publisher reset it, + // even where the browser lost that reset. supersede: () => void; } -// Stale drops expire on the next update. An already accounted header stays accounted, and -// `dropped` says whether a final drop overtook it, rather than a gap the grace gave up on. -type YieldVerdict = - | { kind: "owed" } - | { kind: "accounted"; dropped: boolean } - | { kind: "stale"; under: bigint; error: Error } - | { kind: "accounted-stale"; dropped: boolean; under: bigint; error: Error } - | { kind: "dropped"; error: Error }; - // Whether a drop accounts for the group under `update`, so the subscription's end waits for its -// stream at most the grace: a browser can lose that stream's reset. Only a drop excuses it: a stream -// whose header came after the grace gave up on its gap is still delivering, and no reset is owed. -// A stale drop expires; one over a final drop falls back to it, as `yieldError` does. -function excused(verdict: YieldVerdict, update: bigint): boolean { - switch (verdict.kind) { - case "owed": - return false; - case "accounted": - return verdict.dropped; - case "stale": - return verdict.under >= update; - case "accounted-stale": - return verdict.dropped || verdict.under >= update; - case "dropped": - return true; - } -} - -// The verdict a header starts with: owed, or accounted by a final drop (`dropped`), a stale drop -// under `staleUnder`, or a gap the grace gave up on. A stale drop over a final one falls back to it. -function headerVerdict(owed: boolean, dropped: boolean, staleUnder: bigint | undefined): YieldVerdict { - if (owed) return { kind: "owed" }; - if (staleUnder === undefined) return { kind: "accounted", dropped }; - const error = new StreamError(StreamCode.Old); - return dropped - ? { kind: "accounted-stale", dropped, under: staleUnder, error } - : { kind: "stale", under: staleUnder, error }; +// stream at most the grace. Only a drop excuses it: a stream whose header came after the grace gave +// up on its gap is still delivering, and no reset is owed. A stale drop expires on the next update. +function excused(reading: Reading, update: bigint): boolean { + return reading.dropped || (reading.staleUnder !== undefined && reading.staleUnder >= update); } -// Excuse the stream's hold on the end, or restore it, as its verdict now says. -function judge(yielding: Yielding, update: bigint): void { - if (excused(yielding.verdict, update)) yielding.counted.excuse(); - else yielding.counted.restore(); -} - -function yieldError(verdict: YieldVerdict, update: bigint): Error | undefined { - switch (verdict.kind) { - case "owed": - return undefined; - case "accounted": - return new StreamError(StreamCode.Old); - case "stale": - return verdict.under < update ? undefined : verdict.error; - case "accounted-stale": - return verdict.under < update ? new StreamError(StreamCode.Old) : verdict.error; - case "dropped": - return verdict.error; - } -} - -// The paused groups of `entry`, created on first use. -function pausesOf(entry: SubscribeEntry): Pauses { - entry.paused ??= { groups: new Map(), swept: 0, changed: new Signal(0) }; - return entry.paused; +// Excuse the stream's hold on the end, or restore it, as its drops now say. +function judge(reading: Reading, update: bigint): void { + if (excused(reading, update)) reading.counted.excuse(); + else reading.counted.restore(); } // A subscribe stream's phases. Every SUBSCRIBE_UPDATE and our FIN go out on it. @@ -2237,7 +2164,6 @@ export class Subscriber { // Nothing more is re-offered on this subscription. this.#reoffers.get(producer)?.set([]); this.#reoffers.delete(producer); - this.#endPaused(entry); entry.tail.close(); entry.unanswered?.held.close(); entry.datagramDemand?.(); @@ -2927,7 +2853,7 @@ export class Subscriber { }, ); // An excused stream is waited for at most the grace, as at a subscription's end: a browser - // can lose its reset, while a group it paused waits on the continuation still owed. + // can lose its reset. const stopped = new Once(); const covered = () => entry.tail.covers(groups.start, groups.end); try { @@ -2962,7 +2888,6 @@ export class Subscriber { } finally { left.abort(); this.#subscribes.delete(id); - this.#endPaused(entry); entry.tail.close(); } // What nothing accounted for is refused, as nothing more arrives on it. @@ -3021,8 +2946,7 @@ export class Subscriber { } else if ("drop" in resp) { const { start, end } = resp.drop; const cause = resp.drop.cause(this.version); - // A paused group the drop names ends there: no continuation follows. - this.#dropPaused(entry, start, end, cause); + this.#dropped(entry, start, end); // Final, so a stream it overtook is told from one whose gap the grace gave up on. entry.tail.drop(start, end); giveUp(start, end, dropped(cause)); @@ -3312,10 +3236,9 @@ export class Subscriber { if (stale && update < entry.update) continue; this.#reoffered(entry.track, start, end); // Lite-07 tells a stream a final drop overtook from one whose gap the grace gave up on. - if (!stale && hasYield(this.version)) entry.tail.drop(start, end); + if (!stale && hasSkip(this.version)) entry.tail.drop(start, end); else entry.tail.account(start, end); - // A paused group the drop names ends there: no continuation follows. - this.#dropPaused(entry, start, end, error, stale ? update : undefined); + this.#dropped(entry, start, end, stale ? update : undefined); // Judged under the budget this subscriber sent with that update. const hole = stale ? staleHole(entry.maxDelay ?? 0) : { error }; // A group whose stream finished under this update was served over a stale drop, so it @@ -3554,7 +3477,7 @@ export class Subscriber { entry.maxDelay = update.maxDelay; entry.tail.updated(); // A stream a stale drop excused holds the end again: that verdict no longer stands. - for (const yielding of entry.yielding?.values() ?? []) judge(yielding, entry.update); + for (const reading of entry.reading?.values() ?? []) judge(reading, entry.update); const held = hooks.supersedeStale(entry.track, judged([...ranges, ...fillRanges])); // A lite-07 publisher re-offers every group it reported stale on any update, and drops // again under the new number what is still stale, so the tail owes every stale run again. @@ -3669,25 +3592,21 @@ export class Subscriber { } const { track, timescale, tail } = entry; - // Lite-07: a YIELDED reset pauses the group, for a later stream to continue. - const yields = hasYield(this.version); + // Lite-07: a drop can name the group while its stream is read; see `Reading`. + const drops = hasSkip(this.version); // Asked before this stream accounts for the group: a drop that overtook it named it. let owed = !tail.covers(group.sequence, group.sequence + 1n); // A final drop that overtook this stream, rather than a gap the grace gave up on. const dropped = !owed && tail.dropped(group.sequence); - // A stale drop that overtook this stream: the next update supersedes that verdict, so a - // YIELDED reset after it pauses the group. - const unowedUnder = + // A stale drop that overtook this stream, which only the current update upholds. + const staleUnder = !owed && entry.stale?.some((run) => run.start <= group.sequence && group.sequence < run.end) ? entry.update : undefined; let producer: netGroup.Producer | undefined; let arriving: Arriving | undefined; - let ingest: Ingest | undefined; - let yielding: Yielding | undefined; + let reading: Reading | undefined; const counted = tail.open(group.sequence); - // Until the stream is read, or a pause hands it to the paused group. - let read: (() => void) | undefined = counted.done; this.#reoffered(track, group.sequence, group.sequence + 1n); try { @@ -3705,35 +3624,21 @@ export class Subscriber { // of the stream it replaces, waits unread, which flow control bounds. Created once that copy // ends, it is refused as a duplicate if the copy finished and takes the group if it failed, // continuing the reads of that copy, which wait while it is held. Its own reset ends the - // wait; a clean end leaves it on the copy alone. A lite-07 copy a YIELDED reset pauses is - // continued by this stream instead. + // wait; a clean end leaves it on the copy alone. for (;;) { - const paused = yields ? this.#resume(entry, group) : undefined; - if (paused) { - // The same copy continues, so its readers need no hold. - arriving?.release(); - arriving = undefined; - producer = paused.group; - ingest = { index: group.frameStart, partial: paused.partial }; - // The publisher owes a paused group its continuation, which may yield again. - owed = true; - break; - } // Kept held past a failure until this copy takes the group, below. const next = hooks.arrivingGroup(track, group.sequence); if (next === undefined) break; arriving?.release(); arriving = next; - // Served again once that copy fails or pauses, so owed again. + // Served again once that copy fails, so owed again. owed = true; // A later stream for the group means the publisher reset the earlier one on this // subscription, so it ends now rather than wait on a reset the browser may have lost: // Chromium drops a RESET_STREAM that reaches a stream it has not handed to the page. - entry.yielding?.get(group.sequence)?.supersede(); + entry.reading?.get(group.sequence)?.supersede(); const settled = arriving.settled; - const waits: unknown[] = [settled, stream.closed.then(() => settled)]; - if (yields) waits.push(Signal.race(pausesOf(entry).changed)); - await race(waits); + await race([settled, stream.closed.then(() => settled)]); } producer ??= hooks.receiveGroup(track, { sequence: group.sequence, @@ -3743,33 +3648,33 @@ export class Subscriber { // Released once this copy took the group, so the failed copy's readers find it. arriving?.release(); let superseded: Promise | undefined; - if (yields) { - ingest ??= { index: group.frameStart }; + if (drops) { let supersede = () => {}; superseded = new Promise((resolve) => { supersede = resolve; }); - yielding = { - verdict: headerVerdict(owed, dropped, unowedUnder), + // A copy served again is owed whatever dropped the earlier one. + reading = { counted, + dropped: !owed && dropped, + staleUnder: owed ? undefined : staleUnder, supersede, }; - entry.yielding ??= new Map(); - entry.yielding.set(group.sequence, yielding); - // A stale drop that still stands may already account for the group. - judge(yielding, entry.update); + entry.reading ??= new Map(); + entry.reading.set(group.sequence, reading); + judge(reading, entry.update); } const format = { scale: timescale, compress: entry.compress, lengthFirst: hasLengthFirst(this.version) }; - const reading = readFrames(stream, producer, format, ingest); - const reads = entry.filling ? untilAbandoned(producer, reading) : reading; + const frames = readFrames(stream, producer, format); + const reads = entry.filling ? untilAbandoned(producer, frames) : frames; if (!superseded) await reads; else if ((await race([reads, superseded.then(() => SUPERSEDED)])) === SUPERSEDED) { // Stopping makes the read end at the next frame boundary as if finished, so wait it out - // and treat the stream as the YIELDED reset it was: the later stream starts no later. + // and treat the stream as the stale reset a re-offer follows: the later stream takes over. stream.stop(new StreamError(StreamCode.Cancel)); await reads.catch(() => {}); - throw new StreamError(StreamCode.Yielded, { message: "continued on a later stream" }); + throw new StreamError(StreamCode.Old, { message: "re-offered on a later stream" }); } producer.finish(); @@ -3785,124 +3690,25 @@ export class Subscriber { throw err; } const e = await sessionCause(this.#quic, err); - // The publisher made room for a higher-priority stream: the group pauses, keeping what - // arrived and the subscription's end waiting, until a later stream continues it or a drop - // names it. Mirrors the Rust `GroupRecv::poll_serve`. - // A reset the browser lost the code of pauses the group too: a lite-07 publisher follows - // every reset with a continuation or a drop, unless a reliable reset kept the group's - // header, after which nothing follows, so the end waits for it at most the grace. Not a - // failure the session's close caused, which sessionCause swapped in. - const uncoded = stream.uncoded && e === err; - if (producer && ingest && yielding && (toStreamCode(e) === StreamCode.Yielded || uncoded)) { - this.#endYielding(entry, group.sequence, yielding); - if (uncoded) counted.excuse(); - // A drop named the group first: the publisher gave it up, so nothing continues it. One - // that overtook the stream's header carried a stale verdict. - // A stale drop an update sent since superseded no longer ends the group. - const given = yieldError(yielding.verdict, entry.update); - if (given) { - producer.abort(given); - return; - } - console.debug(`group paused: subscribe=${group.subscribe} sequence=${group.sequence}`); - this.#pause(entry, group.sequence, { group: producer, partial: ingest.partial, read }); - read = undefined; - return; - } producer?.abort(e); stream.stop(e); } finally { arriving?.release(); - if (yielding) this.#endYielding(entry, group.sequence, yielding); - read?.(); + if (reading && entry.reading?.get(group.sequence) === reading) entry.reading.delete(group.sequence); + counted.done(); } } - // Continue the group a YIELDED reset paused with the stream whose header is `hdr`, or undefined - // if none with its sequence is paused, or the cache gave it up meanwhile. Mirrors the Rust - // `TrackEntry::resume`. - #resume(entry: SubscribeEntry, hdr: GroupMessage): PausedGroup | undefined { - const paused = entry.paused?.groups.get(hdr.sequence); - if (!paused) return undefined; - entry.paused?.groups.delete(hdr.sequence); - // The stream that paused it is replaced, so it no longer holds the end. - paused.read(); - if (paused.group.closed.peek() !== undefined) return undefined; - // A continuation starting past the frames held whole would leave a hole in the group, which - // ends with only this stream, as a malformed FRAME does. - const held = hooks.groupFrameCount(paused.group) - (paused.partial ? 1 : 0); - if (hdr.frameStart > held) { - const malformed = new StreamError(StreamCode.MalformedTrack, { - message: `group ${hdr.sequence} continued at frame ${hdr.frameStart}, past the ${held} held`, - }); - paused.group.abort(malformed); - throw malformed; - } - return paused; - } - - // Pause the group `sequence`, whose stream the publisher reset with YIELDED, for a later stream - // to continue. Groups the cache gave up on meanwhile are swept out as the held ones double, so - // sweeping costs constant time per pause. Mirrors the Rust `TrackEntry::pause`. - #pause(entry: SubscribeEntry, sequence: bigint, paused: PausedGroup): void { - const registry = pausesOf(entry); - if (registry.groups.size >= Math.max(registry.swept, 1) * 2) { - for (const [held, group] of registry.groups) { - if (!(group.group.closed.peek() instanceof Error)) continue; - registry.groups.delete(held); - group.read(); - } - registry.swept = registry.groups.size; - } - // One the cache gave up on already, which a recreated group replaces. - registry.groups.get(sequence)?.read(); - registry.groups.set(sequence, paused); - registry.changed.update((count) => count + 1); - } - - // Stop tracking `yielding`, the stream reading `sequence`, unless a later stream took its place. - #endYielding(entry: SubscribeEntry, sequence: bigint, yielding: Yielding): void { - if (entry.yielding?.get(sequence) === yielding) entry.yielding.delete(sequence); - } - - // A SUBSCRIBE_DROP named `[start, end)` with `error`, under update `staleUnder` when it is a - // lite-07 stale verdict: a paused group ends there, and one a stream is reading ends with it if - // a YIELDED reset would otherwise pause it, since no continuation follows, unless an update - // supersedes a stale drop first. Mirrors the Rust `TrackEntry::dropped`. - #dropPaused(entry: SubscribeEntry, start: bigint, end: bigint, error: Error, staleUnder?: bigint): void { - for (const [sequence, yielding] of entry.yielding ?? []) { - if (sequence < start || sequence >= end) continue; - const verdict = yielding.verdict; - if (staleUnder === undefined) yielding.verdict = { kind: "dropped", error }; - else if (verdict.kind !== "dropped") - yielding.verdict = - verdict.kind === "accounted" || verdict.kind === "accounted-stale" - ? { kind: "accounted-stale", dropped: verdict.dropped, under: staleUnder, error } - : { kind: "stale", under: staleUnder, error }; - // A drop that stands accounts for the group, so the end no longer waits on its stream - // past the grace: a browser that lost the stream's reset would otherwise hold it forever. - judge(yielding, entry.update); - } - const paused = entry.paused; - if (!paused) return; - for (const [sequence, group] of paused.groups) { + // A SUBSCRIBE_DROP named `[start, end)`, under update `staleUnder` when it is a lite-07 stale + // verdict: a stream still reading a group it names holds the subscription's end at most the grace + // while the drop stands. Mirrors the Rust `TrackEntry::dropped`. + #dropped(entry: SubscribeEntry, start: bigint, end: bigint, staleUnder?: bigint): void { + for (const [sequence, reading] of entry.reading ?? []) { if (sequence < start || sequence >= end) continue; - paused.groups.delete(sequence); - group.group.abort(error); - group.read(); - } - } - - // The subscription ended: its paused groups end as cancelled with it, even while a held stream - // still watches for them. - #endPaused(entry: SubscribeEntry): void { - const paused = entry.paused; - if (!paused) return; - for (const group of paused.groups.values()) { - group.group.abort(new StreamError(StreamCode.Cancel)); - group.read(); + if (staleUnder === undefined) reading.dropped = true; + else reading.staleUnder = staleUnder; + judge(reading, entry.update); } - paused.groups.clear(); } /** diff --git a/js/net/src/lite/tail.test.ts b/js/net/src/lite/tail.test.ts index eb67e1f148f..a4a60b8d418 100644 --- a/js/net/src/lite/tail.test.ts +++ b/js/net/src/lite/tail.test.ts @@ -51,7 +51,7 @@ function groupStream(subscriber: Subscriber, sequence: bigint) { return { write: (payload: string) => controller.enqueue(frame(payload, subscriber.version)), finish: () => controller.close(), - // With a code, as a publisher resets: lite-07 pauses a group whose reset lost its code. + // With a code, as a publisher resets. reset: () => controller.error(toTransport(StreamCode.Cancel, "reset")), handled, }; diff --git a/js/net/src/lite/version.ts b/js/net/src/lite/version.ts index 56dc6455bdc..18d51ae3b04 100644 --- a/js/net/src/lite/version.ts +++ b/js/net/src/lite/version.ts @@ -759,21 +759,3 @@ export function hasLengthFirst(version: Version): boolean { return true; } } - -/** - * Whether a Group Stream reset with YIELDED pauses its group, which a later stream continues. - * Added in lite-07; an earlier publisher out of stream credit gives the reset group up. - */ -export function hasYield(version: Version): boolean { - switch (version) { - case Version.DRAFT_01: - case Version.DRAFT_02: - case Version.DRAFT_03: - case Version.DRAFT_04: - case Version.DRAFT_05: - case Version.DRAFT_06: - return false; - default: - return true; - } -} diff --git a/js/net/src/open.ts b/js/net/src/open.ts index 4bed9c0b40c..5a820ce3292 100644 --- a/js/net/src/open.ts +++ b/js/net/src/open.ts @@ -247,10 +247,7 @@ export interface OpenStream { * transport cannot tell. */ acknowledged(): Promise; - /** - * Reset the stream to make room for a higher one, failing its writer as too far behind, or on - * lite-07 as yielded, pausing its group for a later stream to continue. - */ + /** Reset the stream to make room for a higher one, failing its writer as too far behind. */ preempt(): void; } diff --git a/js/net/src/payload.ts b/js/net/src/payload.ts index c7c1537945f..a967c351568 100644 --- a/js/net/src/payload.ts +++ b/js/net/src/payload.ts @@ -43,14 +43,7 @@ export async function receiveFrame( hooks.writeGroupFrame(producer, { payload: frame.payload, timestamp }, inflated); return true; } - return fillFrame(stream, producer, producer.createFrame({ size: frame.size, timestamp })); -} - -/** - * Stream the rest of `open`, a frame of `producer`, from `stream`, then finish it. Resolves false - * if the group closed first, which already failed the frame. - */ -export async function fillFrame(stream: Reader, producer: group.Producer, open: frame.Producer): Promise { + const open = producer.createFrame({ size: frame.size, timestamp }); while (open.remaining > 0) { const chunk = await race([stream.readChunk(Math.min(open.remaining, SEGMENT_BYTES)), producer.closed]); if (!(chunk instanceof Uint8Array)) return false; diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index ac9a55e89f7..ba0e80ca95d 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -754,10 +754,10 @@ class Reader implements ResumeSource { * handed out as any group of that copy is, and the runs still given up: all of them while no * copy serves. Mirrors the Rust `Reader::reoffer_groups`. */ - reofferGroups(start: bigint, end: bigint, from = 0): { groups: GroupConsumer[]; givenUp: Run[] } { + reofferGroups(start: bigint, end: bigint): { groups: GroupConsumer[]; givenUp: Run[] } { const serving = this.#serving(); if (!serving) return { groups: [], givenUp: [{ start, end }] }; - const { groups, givenUp } = hooks.reofferGroups(serving.sub, start, end, from); + const { groups, givenUp } = hooks.reofferGroups(serving.sub, start, end); return { groups: groups.map((group) => this.#handOut(serving, group)), givenUp }; } diff --git a/js/net/src/stream.ts b/js/net/src/stream.ts index 6f96afb9e6f..cd0a1e35e41 100644 --- a/js/net/src/stream.ts +++ b/js/net/src/stream.ts @@ -1,4 +1,4 @@ -import { fromTransport, StreamCode, Stream as StreamError, TooFarBehind, toStreamCode, toTransport } from "./error.ts"; +import { fromTransport, StreamCode, TooFarBehind, toStreamCode, toTransport } from "./error.ts"; import { sharedStreamCode } from "./ietf/error.ts"; import type { IetfVersion } from "./ietf/version.ts"; import { Version as Lite, type Version as LiteVersion } from "./lite/version.ts"; @@ -156,13 +156,6 @@ export interface TryOpenOptions extends Omit { /** List the stream among the session's open group streams until it is closed or reset. */ group?: boolean; - - /** - * Whether a reset to make room for a higher stream pauses the group rather than giving it up - * (lite-07): the reset carries YIELDED instead of TOO_FAR_BEHIND, and {@link Writer.preempted} - * says it happened. - */ - yields?: boolean; } /** A send stream that reports what it sent, as W3C WebTransport's `WebTransportSendStream.getStats()` does. */ @@ -264,7 +257,6 @@ export class Reader { #closed?: Promise; // The decode that last ran short and how far, so a retry can wait for those bytes. #short?: { decode: (c: Cursor) => unknown; err: Short }; - #uncoded = false; version: StreamVersion; // Either stream or buffer MUST be provided. @@ -290,9 +282,7 @@ export class Reader { // Every read of this stream funnels through here, so decoding the peer's reset code // once is enough to keep the raw transport error out of every caller (and every app). const result = await reader.read().catch((err: unknown) => { - const e = fromTransport(err, { version: asIetf(this.version) }); - if (!(e instanceof StreamError)) this.#uncoded = true; - throw e; + throw fromTransport(err, { version: asIetf(this.version) }); }); if (result.done) { @@ -505,14 +495,6 @@ export class Reader { return !(await this.#fill()); } - /** - * Whether a read failed underneath without a stream code: Firefox reports a reset that lands - * before the page read the stream's data as a bare `TypeError`, dropping the peer's code. - */ - get uncoded(): boolean { - return this.#uncoded; - } - // The transport error is built only while the stream is still open. After a FIN the // reader is gone, so this allocates nothing and sends nothing. stop(reason: unknown) { @@ -730,10 +712,6 @@ export class Writer implements OpenStream { #aborted = false; /** The session's open group streams, while this one is among them. */ #listed?: OpenStreams; - /** A reset to make room yields, pausing the group, rather than giving it up as too far behind. */ - #yields = false; - /** The session reset it to make room for a higher-ranked open. */ - #preempted = false; // Scratch buffer for each primitive write, sized for the longest (a 9-byte leading-ones varint). #scratch: ArrayBuffer; @@ -866,25 +844,9 @@ export class Writer implements OpenStream { return this.#closed; } - /** - * Reset the stream to make room for a higher one, failing its writer as too far behind, or on - * a stream that {@link TryOpenOptions.yields}, as yielded. - */ + /** Reset the stream to make room for a higher one, failing its writer as too far behind. */ preempt() { - this.#preempted = true; - this.reset( - this.#yields - ? new StreamError(StreamCode.Yielded, { message: "yielded to a higher stream" }) - : new TooFarBehind(), - ); - } - - /** - * Whether the session reset this stream to make room for a higher-ranked open: a flag of this - * side's own, never read off an error code the peer could send too. - */ - get preempted(): boolean { - return this.#preempted; + this.reset(new TooFarBehind()); } reset(reason: unknown) { @@ -942,7 +904,6 @@ export class Writer implements OpenStream { const writable = await session.uni.open(read, options.cancel); if (!writable) return undefined; const writer = Writer.#opened(writable, options.version, sendOrder, session.streams); - writer.#yields = options.yields === true; if (options.group) { writer.#listed = session.streams; session.streams.add(writer); diff --git a/js/net/src/track.ts b/js/net/src/track.ts index 0ce09c6b669..3ed26a3f3f6 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -3248,11 +3248,11 @@ export class Subscriber { return true; } - // Fresh reads from frame `from` of the cached groups in `[start, end)` this cursor admits and no - // longer judges stale, each expiring against the budget, and the runs still given up. A resumed - // reader asks its serving route's copy. Mirrors the Rust `reoffer_groups`. - #reofferGroups(start: bigint, end: bigint, from = 0): { groups: GroupConsumer[]; givenUp: Run[] } { - if (this.#resume) return this.#resume.reofferGroups(start, end, from); + // Fresh reads of the cached groups in `[start, end)` this cursor admits and no longer judges + // stale, each expiring against the budget, and the runs still given up. A resumed reader asks its + // serving route's copy. Mirrors the Rust `reoffer_groups`. + #reofferGroups(start: bigint, end: bigint): { groups: GroupConsumer[]; givenUp: Run[] } { + if (this.#resume) return this.#resume.reofferGroups(start, end); const groups: GroupConsumer[] = []; let givenUp: Run[] = [{ start, end }]; const producer = this.#state.producer; @@ -3264,7 +3264,7 @@ export class Subscriber { for (const sequence of cached) { const admitted = (cap === undefined || sequence < cap) && this.#window.contains(sequence); if (!admitted || this.#staleAt(sequence, drift)) continue; - const group = cachedMirror(producer, sequence, from); + const group = cachedMirror(producer, sequence, 0); if (!group) continue; groups.push(this.#guard(group)); givenUp = removeRun(givenUp, sequence, sequence + 1n); @@ -3926,7 +3926,7 @@ export class Subscriber { hooks.forgetPastBudget = (subscriber, delivered) => subscriber.#forgetPastBudget(delivered); hooks.wire = (subscriber) => subscriber.#wireRecord(); hooks.reofferStale = (subscriber) => subscriber.#reofferStale(); - hooks.reofferGroups = (subscriber, start, end, from) => subscriber.#reofferGroups(start, end, from); + hooks.reofferGroups = (subscriber, start, end) => subscriber.#reofferGroups(start, end); hooks.deliveredRuns = (subscriber) => subscriber.#delivered.runs; hooks.handedOut = (subscriber, sequence) => subscriber.#delivered.has(sequence); hooks.hasUnreadGroup = (subscriber, start, end, delivered) => diff --git a/quest/m1/README.md b/quest/m1/README.md index 691309b210e..f51cf4c82f7 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -25,7 +25,7 @@ because a downstream iGPU transcode trial needs it now. Decided 2026-11-13 (maintainer, /quest-run): stream-credit stays in m1 and no longer gates m2, since takeover, its only wire change, moved to m3. Its YIELDED continuations leave lite-07 again in m2 -([yield-defer](/quest/m2/yield-defer.md), 2026-11-16). +([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965), 2026-11-16). Decided 2026-11-12 (maintainer, /quest-audit before the cut): diff --git a/quest/m2/README.md b/quest/m2/README.md index 42df4d4cfa6..6d0b1832961 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -20,8 +20,8 @@ judged "ship as is". Everything below is breaking by this milestone's rule: a removal, a rename, or a wire, catalog, or format change that is free until 0.22 publishes it. -- **lite-07 wire.** [YIELDED continuations](/quest/m2/yield-defer.md) leave - for lite-08; [the ietf extension](/quest/m2/ietf-records-per-track.md) +- **lite-07 wire.** YIELDED continuations left for lite-08 + ([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)); [the ietf extension](/quest/m2/ietf-records-per-track.md) costs nothing on a moq-lite track; [the path escape](/quest/m2/path-escape.md) is finished; [Subscriber Min Age](/quest/m2/min-age-join.md) stays and gains its reader, reversing 2026-11-14; and diff --git a/quest/m2/ietf-records-per-track.md b/quest/m2/ietf-records-per-track.md index 8ce3d627882..d02067ed375 100644 --- a/quest/m2/ietf-records-per-track.md +++ b/quest/m2/ietf-records-per-track.md @@ -73,9 +73,6 @@ Guidance for the PR: as over one without; a moq-transport track still crosses two relays with its Object IDs, gaps, and properties (the path the 2026-11-09 decision kept, which #8843 noted "is not exercised end to end": add that test). -- With [YIELDED deferred](/quest/m2/yield-defer.md), the "never reset with - YIELDED" rule for such tracks is already gone; if this lands first, leave - that sentence to it. - Run `just test interop --all`. Public API: `setup::Extensions` and both `with_extensions` leave moq-net, @@ -85,5 +82,4 @@ with the `ietf` extension only; a moq-lite track loses its records. ## Related -- [YIELDED deferred](/quest/m2/yield-defer.md) - deletes the extension's yield exception - [moq-transport time addressing](/quest/m2/ietf-time-addressing.md) - changes what moq-transport state a track carries diff --git a/quest/m2/lite07-finalize.md b/quest/m2/lite07-finalize.md index 3646a454881..877099cb35e 100644 --- a/quest/m2/lite07-finalize.md +++ b/quest/m2/lite07-finalize.md @@ -167,8 +167,7 @@ route key lane's early signal above end when Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit, which also audited the lite-07 draft for what it could lose): -- ✅ [YIELDED continuations](/quest/m2/yield-defer.md) leave lite-07 for - lite-08, where they are designed with takeover (rejected: keep them). +- ✅ YIELDED continuations leave lite-07 for lite-08 ([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)), where they are designed with takeover (rejected: keep them). - ✅ The `ietf` extension stays, and [rides only moq-transport tracks](/quest/m2/ietf-records-per-track.md) (rejected: as it is, where every frame on a relay link carries a record). @@ -197,7 +196,6 @@ the version that ships them. ## Required -- [YIELDED deferred](/quest/m2/yield-defer.md) - removes reset code 0x3C and continuations from the lite-07 wire - [ietf records per track](/quest/m2/ietf-records-per-track.md) - changes the extension's framing on relay links - [Path escape](/quest/m2/path-escape.md) - defines a path segment when a path carries an escape - [Min Age join](/quest/m2/min-age-join.md) - makes Subscriber Min Age normative diff --git a/quest/m2/time-addressing/lite07.md b/quest/m2/time-addressing/lite07.md index 0f03820d24d..04ddb1f6853 100644 --- a/quest/m2/time-addressing/lite07.md +++ b/quest/m2/time-addressing/lite07.md @@ -85,10 +85,18 @@ draft already requires for delivered or finally dropped groups, and the `Update` numbers, re-offers, reopen rules, and their model configs go. Also check whether GROUP's previous-start link makes SUBSCRIBE_SKIP redundant. The review wrote that YIELDED continuations would keep their hold; on -2026-11-16 they left lite-07 too ([YIELDED deferred](/quest/m2/yield-defer.md)), +2026-11-16 they left lite-07 too ([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)), so after both PRs a group has a second stream on one subscription only as a duplicate, and no "held unread until the earlier stream ends" rule -remains for this rewrite to preserve. +remains for this rewrite to preserve. That PR kept the rule, and the one +that lets a subscriber end the earlier stream once a re-offer arrives, for +re-offers alone: delete both from the draft, `lite/subscriber.rs` (the +`Hold` wait in `GroupRecv::poll_serve`), and `js/net` with the re-offers. +It also trimmed `rs/moq-net/verify/subscribe` to its OLD-drop rules and +the tail (`ServedOnce`, `QuietIsSettled`, `OneReader`, `StreamWins`); with +re-offers gone, re-derive it around final drops, duplicate streams, and the +tail, or delete it with its skill and nightly job (recommended: re-derive, +since tail accounting stays). Decided 2026-11-14 (maintainer, pick on [moq-dev/moq#8764](https://github.com/moq-dev/moq/pull/8764)): ✅ (a) OLD drops are final on their subscription: this rewrite deletes `Update`, the reopen and re-offer paths, and their model configs (rejected: (b) keep reopening and finish the finished-across-updates quest, deleted 2026-11-16). @@ -108,13 +116,11 @@ is free only until lite-07 freezes: "forever" is refused by JS's 53-bit read. Both constants go on lite-07. - ✅ GROUP loses `Frame Start` (rejected: keep it and state the one case that sets it). A subscription delivers whole groups, FETCH has no GROUP - header, and the last case that set it, a continuation, leaves with - [YIELDED](/quest/m2/yield-defer.md). The Rust publisher says "A lite-07 + header, and the last case that set it, a continuation, left with + YIELDED ([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)). The Rust publisher says "A lite-07 range covers whole groups, so it has none", and JS sets `frameStart` only from a lite-06 SUBSCRIBE's bound. Before deleting it, grep both - publishers for a lite-07 path that opens a Group Stream past frame 0; if - yield-defer has not landed, the continuation is one, and the field goes - with whichever PR lands second. lite-06 keeps its field. + publishers for a lite-07 path that opens a Group Stream past frame 0. lite-06 keeps its field. - ✅ The Padding Stream (0x2) and Padding Datagram (0x1) types are removed; the datagram `Type` stays (rejected: keep the datagram one, as the 2026-10-30 decision preferred; keep both). Both are received and @@ -187,7 +193,6 @@ TRACKS_ADD change, and the Padding stream and datagram types go. ## Related -- [YIELDED deferred](/quest/m2/yield-defer.md) - removes continuations, the last use of GROUP's `Frame Start` - [A group a subscriber gives up on is always reported](/quest/m2/group-drops.md) - reads the spans SUBSCRIBE_DROP names - [Refresh a track](/quest/m2/refresh-interval.md) - raises the `Wanted Group` this turns into a start time - [A cold join reaches back](/quest/m2/min-age-join.md) - gives SUBSCRIBE's `Subscriber Min Age` its reader diff --git a/quest/m2/yield-defer.md b/quest/m2/yield-defer.md deleted file mode 100644 index 2acfc71aa2c..00000000000 --- a/quest/m2/yield-defer.md +++ /dev/null @@ -1,109 +0,0 @@ -# [L] lite-07 freezes without YIELDED continuations - -## Goal - -On lite-07 a Group Stream reset to make room for a higher-priority one is a -final drop of its group on that subscription, as on lite-06: the publisher -names the group in SUBSCRIBE_DROP, and a reader that still wants the rest -FETCHes it from the frame it reached. No group pauses, no stream continues -another, and a group has two streams on one subscription only as a -duplicate. The YIELDED (0x3C) code, paused groups, and continuations leave -the draft, `rs/moq-net`, and `js/net`, and come back in lite-08 with -[takeover](/quest/m3/group-takeover.md), designed as one feature. FRAME -still leads with `Payload Length`. - -## Plan - -Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): - -- ✅ Defer YIELDED continuations to lite-08 with takeover (rejected: keep - them but stop pausing a group on a reset that arrives without its code; - keep as is). This reverses the 2026-11-04 pick that stream credit lands - before the cut, and the 2026-11-12 pick on - [moq-dev/moq#8545](https://github.com/moq-dev/moq/pull/8545) of YIELDED - over "TOO_FAR_BEHIND plus a drop". What changed since: OLD drops became - final on 2026-11-14, which made "ask again yourself" the rule for every - other lost group; takeover, the other half, moved past the cut on - 2026-11-13; and the facts below. - -Facts found by the audit: - -- The named consumer is a lossless reader of several hundred tracks under a - browser's stream cap, which is later work downstream. Nothing planned for - 0.22 reads that way. -- Browsers lose the signal the pause depends on. Chromium drops a reset that - lands right after the stream's data and Firefox drops its code - ([browser reset reports](/quest/m4/browser-reset-reports.md)), so the draft - tells a subscriber to pause on any reset without a code (`#yield`), and - only JS does: [moq-dev/moq#8673](https://github.com/moq-dev/moq/pull/8673) - was "JS only, Rust unchanged". A drop on the Subscribe Stream needs no code - from the reset. -- No relay link yielded until - [moq-dev/moq#8843](https://github.com/moq-dev/moq/pull/8843) on - 2026-11-15, because every relay link negotiated object records - (`GroupServe::yields`, `rs/moq-net/src/lite/publisher.rs`). Fixing that - uncovered a panic in the paused group's writer and a protocol violation on - continuations. -- A continuation restarts at the reset stream's own `Frame Start`, so it - resends the frames the reader holds. That is what a FETCH from a frame - index returns without the resend, and `resume::Recover` - (`rs/moq-net/src/model/resume.rs`) already issues one. A FETCH reply rides - a stream the subscriber opened, outside the incoming cap that caused the - reset. -- Cost, by `grep -ci 'yield\|paus\|continu'`: 188 lines in - `lite/publisher.rs` and 264 in `lite/subscriber.rs`, plus - `tests/integration/stream_credit.rs` (857 lines), the `test/stream-credit` - browser harness, `Continuation.cfg`, and three of the seven rules in - `rs/moq-net/verify/subscribe/SubscribeAccounting.tla`. The other four - rules cover reopened OLD drops, which - [time addressing](/quest/m2/time-addressing/lite07.md) deletes. - -What goes: - -- Draft: the Yield section, the YIELDED row (0x3C), the paused clauses in - SUBSCRIBE_DROP's accounting rule, the continuation exception to the - duplicate-stream rule with its "held unread" and "MAY end the earlier - stream" rules, the `ietf` extension's never-yields sentence, and both - changelog lines. The reset rule reads as lite-06's: the publisher resets - the lowest-priority Group Stream and drops the group. -- Rust: `StreamError::Yielded` and `has_yield` (`rs/moq-net/src/error.rs`, - `lite/version.rs`); the publisher's `TrackRun.paused`, `paused_tasks`, - `continuing`, `GroupServe.rewind`, `give_way`, `shed`, `pause`, - `best_paused`, and `drop_given_up_paused`; the subscriber's `PausedGroup`, - `YieldVerdict`, `TrackEntry.paused`, `yielding`, `yield_streams`, and - `Duplicate.paused`. The priority scheduling that picks which stream to - reset stays. -- JS: the same in `js/net/src/lite/{publisher,subscriber}.ts`, `open.ts`, - and `error.ts`, including the paths that infer a pause from a reset without - a code or from a later stream for the same group. -- `verify/subscribe`: with this and OLD-final, none of the seven rules - describes shipped behaviour. Re-derive the model around what remains - (final drops, duplicate streams, the tail) or delete it with its skill - (`.claude/skills/verify-subscribe`) and nightly job; recommended: - re-derive, since tail accounting stays. Whichever PR lands second, this - or time addressing's, does it. - -Guidance for the PR: - -- Land after or with [time addressing](/quest/m2/time-addressing/lite07.md) - if both are open, since both rewrite SUBSCRIBE_DROP's accounting text; - neither blocks the other. -- Keep `test/stream-credit` and `stream_credit.rs`, rewritten to the new - contract: more live tracks than the cap, the highest-priority groups get - the streams, every group the reader misses is named in a drop, and a - reader that FETCHes a dropped group gets it whole. Run it on lite-06 and - lite-07 in Chromium and Firefox, where both now behave alike. -- Check that the ordered reader and `Recover` do FETCH a group dropped as - TOO_FAR_BEHIND when the reader has no max age, and say in - `doc/concept/moq-lite.md` what a lossless reader under a stream cap sees. -- Run `just test interop --all`. - -Public API: `StreamError::Yielded` leaves Rust and `@moq/net`. Wire: -lite-07-wip loses reset code 0x3C and the continuation rules; lite-01..06 -are unchanged. - -## Related - -- [The lite-07 wire addresses groups by start](/quest/m2/time-addressing/lite07.md) - rewrites the same SUBSCRIBE_DROP accounting text and deletes the model's other four rules -- [Takeover](/quest/m3/group-takeover.md) - designs paused groups and continuations again, with takeover, for lite-08 -- [Browser reset reports](/quest/m4/browser-reset-reports.md) - the browser bugs that made the pause depend on a lost signal diff --git a/quest/m3/fetch-preempted-group.md b/quest/m3/fetch-preempted-group.md new file mode 100644 index 00000000000..ef4978a5030 --- /dev/null +++ b/quest/m3/fetch-preempted-group.md @@ -0,0 +1,51 @@ +# [M] A reader that still wants a group reset to make room fetches it + +## Goal + +A reader whose budget still wants a group, such as an ordered reader with no +max age, gets that group whole even when the publisher reset its Group +Stream to make room for a higher-priority one. Today it does not: the +reader's group ends with `TOO_FAR_BEHIND` after the frames that arrived, and +the next group follows, on every moq-lite version. Rust and `@moq/net` +behave alike. + +## Plan + +Found by [moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965), which +made the reset to make room a final drop on lite-07 too and left a reader to +fetch the group itself. `test/stream-credit` does that fetch by hand in the +tab (`test/stream-credit/src/main.ts`), and every fetched group comes back +whole, so the publisher side is done. + +Where the gap is: + +- `resume::Recover` (`rs/moq-net/src/model/resume.rs`, the verdict paths in + `Recover::poll_serving` and the drop recording) ends a group with a route's + non-retryable error, and `Error::retryable` (`rs/moq-net/src/error.rs`) + excludes `TooFarBehind`. The + `a_verdict_is_never_asked_of_its_own_route` test pins that. +- `js/net/src/resume.ts` `routeFailed` does the same. + +Recommended: a reader fetches a group it still wants whatever code the drop +or reset carried, rather than matching on `TooFarBehind`. A drop judges the +route's subscription, not whether the group exists, as the comment in +`Recover::poll_serving` already says. Only a verdict the reader's own budget would +reach, such as a stale (OLD) drop judged against the budget it sent, ends +the group. Model the change with the verify-resume skill first, since it +changes which errors end a group. + +Tests: `rs/moq-net/tests/integration/stream_credit.rs` +`a_reset_group_is_fetched_whole` drops its manual `fetch_group` and reads +the group whole through the ordered reader; `test/stream-credit` drops the +tab's fetch and asserts the top two tiers read every group whole. Run +`just test interop --all`. + +This also gives [takeover](/quest/m3/group-takeover.md)'s "Measure first" +the drop plus FETCH baseline it compares a continuation against. + +Public API: none. Wire: none. + +## Related + +- [Takeover](/quest/m3/group-takeover.md) - measures this against a continuation before lite-08 brings one back +- [Browser reset reports](/quest/m4/browser-reset-reports.md) - why a browser reader learns of the reset from the drop, not the stream diff --git a/quest/m3/group-takeover.md b/quest/m3/group-takeover.md index 766dfd50a5d..47148702991 100644 --- a/quest/m3/group-takeover.md +++ b/quest/m3/group-takeover.md @@ -14,7 +14,7 @@ reset as a final drop that the reader FETCHes around. Decided 2026-11-16 (maintainer, /quest-plan of the pre-cut audit): ✅ paused groups and continuations leave lite-07 -([YIELDED deferred](/quest/m2/yield-defer.md)) and are designed again here, +([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)) and are designed again here, with takeover, as one lite-08 feature (rejected: keep YIELDED in lite-07 and add takeover later; keep it but stop pausing on a reset without a code). lite-07 would have frozen the half that still needs a new stream per @@ -127,7 +127,7 @@ marker, and multi-group streams. ## Related -- [YIELDED deferred](/quest/m2/yield-defer.md) - removes paused groups and continuations from lite-07 +- [A reader fetches a group reset to make room](/quest/m3/fetch-preempted-group.md) - the drop plus FETCH that Measure first compares a continuation against - [YIELDED reset](https://github.com/moq-dev/moq/pull/8545) - the lite-07-wip design this starts from - [The lite-07 wire addresses groups by start](/quest/m2/time-addressing/lite07.md) - freezes the GROUP_END this must not collide with - [Finalize lite-07](/quest/m2/lite07-finalize.md) - freezes lite-07 without takeover or continuations diff --git a/rs/moq-net/benches/publisher.rs b/rs/moq-net/benches/publisher.rs index d62cd59b075..920486d547d 100644 --- a/rs/moq-net/benches/publisher.rs +++ b/rs/moq-net/benches/publisher.rs @@ -7,23 +7,10 @@ //! up as a slope over `skipped`. `adjacent` gives up the groups of one range instead, each //! with a different error from its neighbor, so the record holds one entry per group. //! -//! `publisher_paused_frames` holds `paused` unfinished paused groups and writes `frames` -//! frames to them per wake, one group each in turn, before the run checks its paused groups -//! for one the budget or the cache gave up. A wake re-polls only the groups written to, so -//! the cost per frame stays flat over `paused`. -//! //! Run with `cargo bench -p moq-net --features fuzz --bench publisher`. -#![expect( - clippy::disallowed_methods, - clippy::disallowed_types, - reason = "custom timings measure the real time the work took, not the ambient clock" -)] - use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; -use std::time::{Duration, Instant}; - -use moq_net::fuzz::{CoverageBench, PausedBench}; +use moq_net::fuzz::CoverageBench; /// Ranges the source gave up before the wake. const SKIPPED: [u64; 4] = [1, 32, 1024, 8192]; @@ -51,42 +38,5 @@ fn bench_covered_before_start(c: &mut Criterion) { group.finish(); } -/// Paused groups the run holds. -const PAUSED: [u64; 4] = [1, 32, 1024, 8192]; - -/// Frames written to paused groups per wake. -const FRAMES: [usize; 3] = [1, 16, 256]; - -/// Wakes per fixture: each frame stays in its group, so the fixture is rebuilt, untimed, -/// before it holds too many. -const WAKES: u64 = 1024; - -fn bench_paused_frames(c: &mut Criterion) { - let mut group = c.benchmark_group("publisher_paused_frames"); - for paused in PAUSED { - for frames in FRAMES { - group.throughput(Throughput::Elements(frames as u64)); - group.bench_function(BenchmarkId::new(format!("paused={paused}"), frames), |b| { - b.iter_custom(|iters| { - let mut total = Duration::ZERO; - let mut left = iters; - while left > 0 { - let mut run = PausedBench::new(paused); - let wakes = left.min(WAKES); - let start = Instant::now(); - for _ in 0..wakes { - run.wake(frames); - } - total += start.elapsed(); - left -= wakes; - } - total - }); - }); - } - } - group.finish(); -} - -criterion_group!(benches, bench_covered_before_start, bench_paused_frames); +criterion_group!(benches, bench_covered_before_start); criterion_main!(benches); diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index 9236294e349..64ab528a52c 100644 --- a/rs/moq-net/src/coding/writer.rs +++ b/rs/moq-net/src/coding/writer.rs @@ -308,11 +308,6 @@ impl Writer { Poll::Ready(res) } - /// The stream, until the writer finishes, aborts, or closes it. - pub(crate) fn stream(&self) -> Option<&S> { - self.stream.as_ref() - } - /// Set the priority within the stream's send group, or session-wide when ungrouped. /// /// Streams with HIGHER values are transmitted first. This is the transport trait's diff --git a/rs/moq-net/src/error.rs b/rs/moq-net/src/error.rs index 7690a5e522a..ce179f91d5f 100644 --- a/rs/moq-net/src/error.rs +++ b/rs/moq-net/src/error.rs @@ -247,11 +247,6 @@ pub enum StreamError { #[error("unauthorized")] Unauthorized, - /// The publisher reset a group's stream to make room for a higher-priority one, pausing - /// the group for a later stream to continue rather than giving it up. - #[error("yielded")] - Yielded, - /// An application-chosen code, offset into the 64+ range on the wire. #[error("app code={0}")] App(u16), @@ -284,7 +279,6 @@ impl StreamError { Self::FrameTooLarge => 0x38, Self::TimestampMismatch => 0x39, Self::Unauthorized => 0x3b, - Self::Yielded => 0x3c, Self::App(app) => *app as u32 + 64, Self::Unknown(code) => *code, } @@ -317,7 +311,6 @@ impl StreamError { 0x38 => Self::FrameTooLarge, 0x39 => Self::TimestampMismatch, 0x3b => Self::Unauthorized, - 0x3c => Self::Yielded, code @ 64.. => match u16::try_from(code - 64) { Ok(app) => Self::App(app), Err(_) => Self::Unknown(code), @@ -884,7 +877,6 @@ mod tests { (StreamError::FrameTooLarge, 0x38), (StreamError::TimestampMismatch, 0x39), (StreamError::Unauthorized, 0x3b), - (StreamError::Yielded, 0x3c), (StreamError::App(7), 0x40 + 7), ] { assert_eq!(err.to_code(), spec, "{err:?} moved off its assigned code"); diff --git a/rs/moq-net/src/fuzz.rs b/rs/moq-net/src/fuzz.rs index 39fb2b122e6..93eaed2cc1c 100644 --- a/rs/moq-net/src/fuzz.rs +++ b/rs/moq-net/src/fuzz.rs @@ -20,7 +20,7 @@ pub use request::request; pub use session::{session_ietf, session_lite}; /// Lite-07 publisher run loops for the publisher bench. -pub use crate::lite::{CoverageBench, PausedBench}; +pub use crate::lite::CoverageBench; use bytes::Bytes; diff --git a/rs/moq-net/src/ietf/error.rs b/rs/moq-net/src/ietf/error.rs index dd314fd3aac..6b23f7942d4 100644 --- a/rs/moq-net/src/ietf/error.rs +++ b/rs/moq-net/src/ietf/error.rs @@ -590,7 +590,6 @@ mod tests { StreamError::GroupTooLarge, StreamError::TimestampMismatch, StreamError::Unauthorized, - StreamError::Yielded, StreamError::ControlTimeout, StreamError::App(7), StreamError::Unknown(0x1234), diff --git a/rs/moq-net/src/lite/mod.rs b/rs/moq-net/src/lite/mod.rs index ccc08dc1b63..ab1c5e0515c 100644 --- a/rs/moq-net/src/lite/mod.rs +++ b/rs/moq-net/src/lite/mod.rs @@ -33,7 +33,7 @@ mod probe; #[cfg(feature = "async")] mod publisher; #[cfg(all(feature = "async", any(test, feature = "fuzz")))] -pub use publisher::{CoverageBench, PausedBench}; +pub use publisher::CoverageBench; #[cfg(feature = "async")] mod replay; #[cfg(feature = "async")] diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index f60f510f90d..5547bff35fe 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -2553,7 +2553,6 @@ impl Request for SubscribeServe { stamp, outcomes: Default::default(), ietf, - object_records: ietf && track.info().ietf().is_some(), }; let latest = match shared.version { @@ -4929,9 +4928,6 @@ struct Subscription { outcomes: Arc, /// Whether the session negotiated the `ietf` extension, so streams carry relay state. ietf: bool, - /// Whether the track carries moq-transport object records on `ietf` streams, which a - /// continuation cannot resume. - object_records: bool, } /// Each group keeps its own steering cursor, without a session or send-group owner. @@ -4947,9 +4943,6 @@ struct GroupContext { outcomes: Arc, /// Whether the session negotiated the `ietf` extension, so streams carry relay state. ietf: bool, - /// Whether the track carries moq-transport object records on `ietf` streams, which a - /// continuation cannot resume. - object_records: bool, } impl GroupContext { @@ -4972,40 +4965,6 @@ struct GroupOutcomes { unencodable: Mutex>, /// Groups whose stream failed, with why, for the control stream to name. dropped: Mutex>, - /// Lite-07: groups whose stream the session reset to make room, for the run loop to - /// continue on a new stream. - paused: Mutex>, -} - -/// A group whose stream the session reset with YIELDED to make room for a higher-ranked -/// open: the subscriber keeps what arrived, and a new stream continues the group. -struct Paused { - /// An unread copy of the reset stream's cursor, at its `Frame Start`: the reset may have - /// discarded frames the transport acknowledged, so the continuation starts there. - group: group::Consumer, - frame_start: u64, -} - -/// Waits for the subscription's budget or the cache to give the paused group up. Each -/// paused group wakes only its own task, so a frame written to one re-polls just that one. -impl kio::Task for Paused { - type Output = Error; - - fn poll(&mut self, waiter: &kio::Waiter) -> Poll { - if let Some(err) = poll_given_up(&mut self.group, waiter) { - return Poll::Ready(err); - } - // A finished group this cursor has nothing left of is no loss once the cache releases - // it, but holding it past that would outlive the cache's bound on the paused groups. - self.group.poll_aborted(waiter) - } -} - -/// Where a paused group waits in [`TrackRun::paused_tasks`], and where its continuation starts. -#[derive(Clone, Copy)] -struct PausedKey { - key: kio::TaskKey, - frame_start: u64, } impl Subscription { @@ -5018,7 +4977,6 @@ impl Subscription { stamp: self.stamp, outcomes: self.outcomes.clone(), ietf: self.ietf, - object_records: self.object_records, } } @@ -5130,16 +5088,6 @@ struct TrackRun { // The expired groups an update re-offers, still cached and in range, each served again // on its own stream as stream credit allows. reoffered: std::collections::VecDeque, - // Lite-07: the paused groups by start, each continued on its own stream ahead of - // re-offers and new groups, the highest-ranked first. Credit returns with each pause, so - // sustained congestion can pause every group in turn: one the budget or the cache gives - // up while it waits is dropped then, which bounds them by what the cache holds, and a - // group paused again replaces its entry. - paused: std::collections::BTreeMap, - // Lite-07: the paused groups themselves, each waiting to be given up. - paused_tasks: kio::Tasks, - // Lite-07: the group `opening` holds continues a paused one. - continuing: bool, end_sent: bool, // Lite-05/06: the track's end, declared while the start was still unresolved. Their // SUBSCRIBE_START is the first message, and a SUBSCRIBE_FINISH ahead of it says no group @@ -5212,9 +5160,6 @@ impl TrackRun { update: 0, redrop: RangeSet::new(), reoffered: Default::default(), - paused: Default::default(), - paused_tasks: kio::Tasks::new(), - continuing: false, end_sent: false, held_end: None, opening: None, @@ -5492,7 +5437,6 @@ impl TrackRun { // The track side takes groups again. Idle::disarm(&mut self.idle); let mut opening = self.opening.take().expect("polled above"); - self.continuing = false; match res { Ok(()) => self.children.push(opening), Err(err) => opening.settle_failed(&err), @@ -5513,15 +5457,6 @@ impl TrackRun { return Poll::Ready(Err(err)); } - let paused = std::mem::take(&mut *self.ctx.outcomes.paused.lock().unwrap()); - if !paused.is_empty() { - // The track side continues them. - Idle::disarm(&mut self.idle); - for paused in paused { - self.pause(paused); - } - } - self.drop_given_up_paused(waiter); let dropped = std::mem::take(&mut *self.ctx.outcomes.dropped.lock().unwrap()); if !dropped.is_empty() { for (key, err) in dropped { @@ -5608,24 +5543,6 @@ impl TrackRun { return Poll::Ready(Ok(ControlFlow::Continue(()))); } - // A paused group takes the next stream credit, ahead of re-offers and new groups, the - // highest-ranked first. It is owed already, and the subscriber holds its group open until - // it continues. A group waiting for credit it cannot make room for gives way to one that - // outranks it. - let order = self.ctx.steer_current().order; - let stuck = self.stuck(); - if let Some((best, rank)) = self.best_paused(order) - && (self.opening.is_none() || stuck.is_some_and(|waiting| rank > waiting)) - { - let PausedKey { key, .. } = self.paused.remove(&best).expect("best is paused"); - let Paused { group, frame_start } = self.paused_tasks.remove(key).expect("a paused group waits"); - tracing::debug!(subscribe = self.ctx.id, track = %self.ctx.track_name, start = group.start, frame_start, "continuing paused group"); - self.give_way(); - self.open(group, frame_start); - self.continuing = true; - return Poll::Ready(Ok(ControlFlow::Continue(()))); - } - // A group an update re-offers takes the next stream credit, ahead of new ones, unless a // verdict sent since accounted for it. if self.opening.is_none() @@ -5651,6 +5568,8 @@ impl TrackRun { } // One cursor drives the whole subscription: poll the cap-aware arrival-order group. + let order = self.ctx.steer_current().order; + let stuck = self.stuck(); let emit_boundary = self.start.emits_range() && !self.end_sent; // A group waiting for credit it cannot make room for must not hold back one that could: // a group that outranks it takes its place. Newest first sheds the backlog, so the cursor @@ -5726,7 +5645,7 @@ impl TrackRun { } } Recv::Finished => { - // A continuation waiting for credit is still in flight too. + // A group waiting for credit is still in flight too. if !self.children.is_empty() || self.opening.is_some() { return Poll::Pending; } @@ -6120,24 +6039,15 @@ impl TrackRun { (held.peek().is_some() && held.all(|held| held > waiting)).then_some(waiting) } - /// Take the group waiting for stream credit out of line, for one that outranks it: a - /// continuation goes back among the paused, and a new group is given up as one expired - /// under the subscription's budget is, which a lite-07 update may re-offer. Either way at - /// most one group per subscription waits for credit. + /// Take the group waiting for stream credit out of line, for one that outranks it, giving + /// it up as one expired under the subscription's budget is, which a lite-07 update may + /// re-offer. At most one group per subscription waits for credit. fn give_way(&mut self) { let Some(mut waiting) = self.opening.take() else { return; }; tracing::debug!(subscribe = self.ctx.id, track = %self.ctx.track_name, key = waiting.key, "giving way to a higher-ranked group"); - match std::mem::take(&mut self.continuing) { - true => { - self.pause(Paused { - group: waiting.group.clone(), - frame_start: waiting.frame_start, - }); - } - false => waiting.settle_failed(&Error::Old), - } + waiting.settle_failed(&Error::Old); } /// Give up a group the cursor handed out without a stream, as one expired under the @@ -6148,47 +6058,6 @@ impl TrackRun { } } - /// Hold a paused group until its continuation, replacing the entry of an earlier pause. - fn pause(&mut self, paused: Paused) { - let start = paused.group.start; - let frame_start = paused.frame_start; - let key = self.paused_tasks.insert(paused); - if let Some(old) = self.paused.insert(start, PausedKey { key, frame_start }) { - self.paused_tasks.remove(old.key); - } - } - - /// The start and rank of the highest-ranked paused group in `order`, the next to - /// continue. The rank orders groups by start first, so it is an end of the map. - fn best_paused(&self, order: crate::track::Order) -> Option<(u64, i64)> { - let (&start, paused) = match order { - crate::track::Order::Descending => self.paused.last_key_value(), - crate::track::Order::Ascending => self.paused.first_key_value(), - }?; - Some((start, crate::priority::in_group(start, paused.frame_start, order))) - } - - /// Drop each paused group the subscription's budget or the cache gave up while it waited - /// to continue, as a group waiting for credit would be. Only the woken groups are polled. - fn drop_given_up_paused(&mut self, waiter: &kio::Waiter) { - if self.paused_tasks.is_empty() { - return; - } - let mut dropped = Vec::new(); - let _ = self.paused_tasks.poll_each(waiter, |paused, err| { - dropped.push((paused.group.start, StreamError::from(&err))); - }); - if dropped.is_empty() { - return; - } - for (start, _) in &dropped { - self.paused.remove(start); - } - if self.ctx.version.has_track_stream() { - self.ctx.outcomes.dropped.lock().unwrap().extend(dropped); - } - } - /// Open a group machine streaming `group` from `frame_start`, which waits for stream /// credit before the cursor hands out another. fn open(&mut self, group: group::Consumer, frame_start: u64) { @@ -6309,7 +6178,6 @@ impl CoverageBench { stamp: None, outcomes: Default::default(), ietf: false, - object_records: false, }; let mut this = Self { run: TrackRun::new(ctx, subscriber, bounds, 0, steer), @@ -6345,58 +6213,6 @@ impl CoverageBench { } } -/// A lite-07 subscription's run loop holding paused groups that are still being written, for -/// the `publisher` bench: each wake writes frames to some of them, then checks the paused -/// groups for one given up and picks the next to continue. -#[cfg(any(test, feature = "fuzz"))] -pub struct PausedBench { - coverage: CoverageBench, - groups: Vec, - next: usize, -} - -#[cfg(any(test, feature = "fuzz"))] -impl PausedBench { - /// Pause `paused` unfinished groups, each holding one frame. - pub fn new(paused: u64) -> Self { - let mut coverage = CoverageBench::new(2, 0); - let mut groups = Vec::new(); - // Past the subscribed ranges, so the run itself never serves them. - for start in 100..100 + paused { - let mut group = coverage.track.create_group(group::Info { start }).unwrap(); - group - .write_frame(crate::Timestamp::unscaled(start), b"x".to_vec()) - .unwrap(); - coverage.run.pause(Paused { - group: group.consume(), - frame_start: 0, - }); - groups.push(group); - } - let mut this = Self { - coverage, - groups, - next: 0, - }; - this.wake(0); - this - } - - /// Write one frame to each of the next `frames` paused groups in turn, then wake once. - pub fn wake(&mut self, frames: usize) { - for _ in 0..frames { - let group = &mut self.groups[self.next]; - let key = crate::Timestamp::unscaled(group.start); - group.write_frame(key, b"x".to_vec()).unwrap(); - self.next = (self.next + 1) % self.groups.len(); - } - let run = &mut self.coverage.run; - run.drop_given_up_paused(&kio::Waiter::noop()); - let order = run.ctx.steer_current().order; - assert!(run.best_paused(order).is_some(), "no paused group is given up"); - } -} - /// Where a group stream is ranked. enum Rank { /// In the subscription's send group, by its key in the subscription's order. @@ -6433,12 +6249,6 @@ struct GroupServe { frame_start: u64, /// The stream stops at a subscription's end frame, so its FIN cannot end the group. capped: bool, - /// Lite-07 on a track without the `ietf` extension's object records: an unread copy of `group` at - /// `frame_start`, which a new stream continues from if the session resets this one to - /// make room. - rewind: Option, - /// The session reset the stream to make room, pausing the group. - paused: bool, // Lite05+ delta-encodes per-frame timestamps within the group. The first // frame's delta is absolute (against an implicit prev value of 0), every // subsequent delta is signed against the previous frame. @@ -6492,10 +6302,7 @@ impl GroupServe { false => group, }; let meter = group.delegate(); - let rewind = Self::yields(&ctx).then(|| group.clone()); Self { - rewind, - paused: false, ctx, rank, open: Default::default(), @@ -6545,14 +6352,9 @@ impl GroupServe { priority: handle.send_order().into(), }, }; - // A higher-priority open waiting for credit can reset this stream. Lite-07 pauses the - // group, for a later stream to continue. Otherwise the reader fell too far behind the - // publisher's streams, and the control stream drops the group. - let preempt = match self.rewind.is_some() { - true => StreamError::Yielded, - false => StreamError::TooFarBehind, - }; - let preempt = self.ctx.version.encode_stream_code(&preempt); + // A higher-priority open waiting for credit can reset this stream: the reader fell too + // far behind the publisher's streams, and the control stream drops the group. + let preempt = self.ctx.version.encode_stream_code(&StreamError::TooFarBehind); let stream = match ready!(session.poll_open_group(&mut self.open, waiter, options, preempt)) { Ok(stream) => stream, Err(err) => { @@ -6819,18 +6621,11 @@ impl GroupServe { match writer.finish() { Ok(()) => self.state = GroupState::Closed { writer }, // The writer drops here: the Drop reset stands in for the - // abort, exactly like the old `finish()?`. A preempt from - // another task can land between the last flush and the FIN, - // which then fails, and only pauses the group too. - Err(err) => { - self.paused = self.preempted(&writer); - return Poll::Ready(Err(err)); - } + // abort, exactly like the old `finish()?`. + Err(err) => return Poll::Ready(Err(err)), } } Err(err) => { - // Read before the abort releases the stream. - self.paused = self.preempted(&writer); writer.abort(&err); return Poll::Ready(Err(err)); } @@ -6877,34 +6672,12 @@ impl kio::Task for GroupServe { } impl GroupServe { - /// Whether a stream reset to make room pauses its group: lite-07, unless the track - /// carries the `ietf` extension's object records, which a continuation cannot resume. - fn yields(ctx: &GroupContext) -> bool { - ctx.version.has_yield() && !ctx.object_records - } - - /// Whether the session reset `writer`'s stream to make room, pausing a group that yields. - fn preempted(&self, writer: &Writer) -> bool { - self.rewind.is_some() && writer.stream().is_some_and(|stream| stream.preempted()) - } - /// Account for a group that failed with `err`: a reset can lose its header, and a group - /// that never got a stream has none, so the control stream names it with that error. A - /// group the session paused is continued instead. + /// that never got a stream has none, so the control stream names it with that error. fn settle_failed(&mut self, err: &Error) { if !self.ctx.version.has_track_stream() { return; } - if self.paused - && let Some(group) = self.rewind.take() - { - let paused = Paused { - group, - frame_start: self.frame_start, - }; - self.ctx.outcomes.paused.lock().unwrap().push(paused); - return; - } self.ctx .outcomes .dropped @@ -7121,7 +6894,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7168,7 +6940,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7236,7 +7007,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( Arc::new(broadcast::Info::default()), @@ -7311,7 +7081,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( Arc::new(broadcast::Info::default()), @@ -7382,7 +7151,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7433,7 +7201,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7501,7 +7268,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7681,7 +7447,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7754,7 +7519,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7804,7 +7568,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7853,7 +7616,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let track = track::Producer::new( @@ -7930,7 +7692,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite06, Some(crate::Timescale::MILLI)), outcomes: Default::default(), ietf: false, - object_records: false, }; let bounds = Bounds::new(&[lite::Range::span(Some(0), 0, None, None)], false); (TrackRun::new(ctx, track, bounds, 0, steer), writer, log) @@ -10201,7 +9962,6 @@ mod serve_group_test { stamp: Stamp::new(Version::Lite07, Some(crate::Timescale::MILLI)), outcomes: outcomes.clone(), ietf: false, - object_records: false, }; let bounds = Bounds::new(&[lite::Range::span(Some(start_group), 0, None, None)], false); Self { @@ -12863,62 +12623,4 @@ mod tests { assert!(groups[1].1 > groups[0].1, "the publisher's priority breaks the tie"); assert!(groups[2].1 > groups[1].1, "the subscriber's priority dominates"); } - - /// A group paused again waits once, at its new start, and is dropped once when given up. - #[test] - fn a_group_paused_again_replaces_its_wait() { - let mut bench = PausedBench::new(1); - let run = &mut bench.coverage.run; - run.pause(Paused { - group: bench.groups[0].consume(), - frame_start: 1, - }); - assert_eq!(run.paused_tasks.len(), 1, "the earlier wait is cancelled"); - assert_eq!(run.paused[&100].frame_start, 1); - - bench.groups.remove(0).abort(Error::Cancel).unwrap(); - let run = &mut bench.coverage.run; - run.drop_given_up_paused(&kio::Waiter::noop()); - assert!(run.paused.is_empty() && run.paused_tasks.is_empty()); - let dropped = run.ctx.outcomes.dropped.lock().unwrap(); - assert_eq!(dropped.iter().map(|(start, _)| *start).collect::>(), [100]); - } - - /// The next paused group to continue is the newest when descending, the oldest when - /// ascending, and only the woken group leaves when it is given up. - #[test] - fn the_best_paused_group_follows_the_order() { - let mut bench = PausedBench::new(3); - let best = |run: &TrackRun, order| run.best_paused(order).map(|(start, _)| start); - assert_eq!(best(&bench.coverage.run, track::Order::Descending), Some(102)); - assert_eq!(best(&bench.coverage.run, track::Order::Ascending), Some(100)); - - bench.groups.remove(2).abort(Error::Cancel).unwrap(); - bench.wake(0); - let run = &bench.coverage.run; - assert_eq!(best(run, track::Order::Descending), Some(101)); - assert_eq!(run.paused_tasks.len(), 2); - } - - /// A paused group with nothing left to send is dropped once the cache releases it. - #[test] - fn a_released_paused_group_with_nothing_unread_is_dropped() { - let mut bench = PausedBench::new(1); - let run = &mut bench.coverage.run; - let empty = bench.coverage.track.create_group(group::Info { start: 200 }).unwrap(); - let empty = empty.close().unwrap(); - run.pause(Paused { - group: empty.consume(), - frame_start: 0, - }); - run.drop_given_up_paused(&kio::Waiter::noop()); - assert!(run.paused.contains_key(&200), "a finished group waits to continue"); - - empty.abort(Error::Evicted).unwrap(); - run.drop_given_up_paused(&kio::Waiter::noop()); - assert!(!run.paused.contains_key(&200)); - assert_eq!(run.paused_tasks.len(), 1, "only the released group leaves"); - let dropped = run.ctx.outcomes.dropped.lock().unwrap(); - assert_eq!(dropped.iter().map(|(start, _)| *start).collect::>(), [200]); - } } diff --git a/rs/moq-net/src/lite/subscriber.rs b/rs/moq-net/src/lite/subscriber.rs index 2dcfa8c5857..12f51645285 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -179,19 +179,11 @@ struct TrackEntry { /// Where the subscription's groups go, once its TRACK_INFO says how to read them. A /// SUBSCRIBE sent alongside its TRACK registers before. copy: Option, - // Parked and paused groups hold group and frame writers, which reach the whole track + // Parked groups hold group and frame writers, which reach the whole track // model: the cells prove `Send` once here, so the session driver's proof stays within // the default recursion limit. /// The group streams that arrived before the copy, while any wait. parked: Option>>, - /// Lite-07: the groups a YIELDED reset paused, once one did or a stream waits on one. - paused: Option>>, - /// Lite-07: the groups a stream that a YIELDED reset would pause is reading, one per - /// stream, so stream credit bounds them. - yielding: BTreeMap, - /// Lite-07: the last token handed to a stream in `yielding`, so a stream ending removes - /// only its own record, never that of a stream that replaced it. - yield_streams: u64, /// The groups received so far, so the subscription's end can wait for the ones owed. tail: kio::Producer, /// Lite-05/06: where the feed starts, from SUBSCRIBE_START. @@ -237,79 +229,6 @@ struct Parked { next: u64, } -/// Lite-07: the groups whose stream the publisher reset with YIELDED, each held open for a -/// later stream to continue, by key. -#[derive(Default)] -struct Paused { - groups: BTreeMap, - /// How many were held after the last sweep for groups the cache gave up on, which the - /// next waits for twice as many, so sweeping costs constant time per pause. - swept: usize, -} - -/// A group a YIELDED reset paused: what arrived stays, and its stream still counts toward -/// the subscription's end until a continuation replaces it or a drop names the group. -struct PausedGroup { - group: crate::recv::Group, - /// The frame the reset cut short, holding the group's writer. - partial: Option, - reading: Reading, -} - -impl PausedGroup { - /// Where a continuation of `group` may start at the latest: past every frame held whole, - /// and no further than the frame `partial` the reset cut short. - fn resumes_by_parts(group: &crate::recv::Group, partial: Option<&frame::Producer>) -> u64 { - group.frame_count() as u64 - u64::from(partial.is_some()) - } -} - -/// Whether a YIELDED reset of a stream being read pauses its group. -struct Yielding { - /// The stream reading the group, as [`TrackEntry::read_yielding`] numbered it. - stream: u64, - verdict: YieldVerdict, -} - -/// A stale verdict expires on the next update; a final drop never does. A group already -/// accounted before its header stays accounted even if a later stale drop expires. -enum YieldVerdict { - Owed, - Accounted, - Stale { under: u64, error: Error }, - AccountedStale { under: u64, error: Error }, - Dropped(Error), -} - -impl YieldVerdict { - fn updated(self, updates: u64) -> Self { - match self { - Self::Stale { under, .. } if under < updates => Self::Owed, - Self::AccountedStale { under, .. } if under < updates => Self::Accounted, - verdict => verdict, - } - } - - fn drop_with(&mut self, error: Error, under: Option) { - *self = match under { - Some(_) if matches!(self, Self::Dropped(_)) => return, - Some(under) if matches!(self, Self::Accounted | Self::AccountedStale { .. }) => { - Self::AccountedStale { under, error } - } - Some(under) => Self::Stale { under, error }, - None => Self::Dropped(error), - }; - } - - fn error(self) -> Option { - match self { - Self::Owed => None, - Self::Accounted => Some(Error::Stream(crate::StreamError::Old)), - Self::Stale { error, .. } | Self::AccountedStale { error, .. } | Self::Dropped(error) => Some(error), - } - } -} - /// What a group stream's header opened in its subscription's copy. enum Open { Group(Opened), @@ -319,10 +238,6 @@ enum Open { /// A group stream's group, opened in its subscription's copy for the stream to fill. struct Opened { group: crate::recv::Group, - /// A continuation's frame its YIELDED reset cut short, which it completes. Boxed, as - /// most groups have none. - partial: Option>, - verdict: YieldVerdict, track: track::Producer, framing: lite::Framing, reading: Reading, @@ -334,8 +249,6 @@ struct Opened { struct Duplicate { /// The copy still arriving, whose readers wait while this holds it. arriving: track::Hold, - /// Lite-07: where a YIELDED reset of the arriving copy pauses it, which this continues. - paused: Option>, track: track::Producer, /// Counted already, and kept open while held, since the stream may still carry the group. reading: Reading, @@ -343,13 +256,6 @@ struct Duplicate { impl Drop for TrackEntry { fn drop(&mut self) { - // Paused groups end as cancelled with the subscription, even while a held stream - // still watches for them. - if let Some(paused) = &self.paused - && let Ok(mut paused) = paused.write() - { - paused.groups.clear(); - } // Groups the accept opened for streams that never took them end as cancelled with // the subscription, rather than dropped unfinished. let Some(parked) = &self.parked else { @@ -391,9 +297,6 @@ impl TrackEntry { Self { copy, parked: None, - paused: None, - yielding: BTreeMap::new(), - yield_streams: 0, tail, start: None, first: None, @@ -492,23 +395,14 @@ impl TrackEntry { /// Open the group of a stream whose header is `hdr` in the copy. fn open(&mut self, hdr: &lite::Group, version: Version, now: crate::time::Instant) -> Result { - if self.is_paused(hdr.key) { - let reading = Reading::open(&self.tail, Some(hdr.key), now); - return self.resume(hdr, reading).expect("checked paused above"); - } let Some(Accepted { producer, .. }) = &mut self.copy else { return Err(Error::Cancel); }; let group_info = group::Info { start: hdr.key }; - // A stale drop that overtook this stream, which the group replaces: the next update - // supersedes that verdict, so a YIELDED reset after it pauses the group. - let stale = producer.holds_stale(hdr.key); // Stats (groups/frames/bytes) are counted in the model as the group // is written, through the tagged `track::Producer`. The track counts // it as content once its first frame lands. let received = producer.create_group_at(group_info, hdr.frame_start); - // Asked before this stream accounts for the group: a drop that overtook it named it. - let owed = !self.tail.read().accounts(hdr.key); // The subscription's end waits until this stream is read. Counted // whatever the cache said, since the publisher counts every stream it // opens (a resubscribe resends a cached group), and only after the @@ -534,142 +428,7 @@ impl TrackEntry { } } } - let verdict = if owed { - YieldVerdict::Owed - } else if stale { - YieldVerdict::Stale { - under: self.tail.read().updates(), - error: Error::Stream(crate::StreamError::Old), - } - } else { - YieldVerdict::Accounted - }; - self.place(hdr, version, received, reading, verdict) - } - - /// Whether the track carries moq-transport object records, which a continuation cannot resume. - fn has_records(&self) -> bool { - self.copy.as_ref().is_some_and(|copy| copy.framing.ietf.is_some()) - } - - /// Whether a YIELDED reset paused the group `key`, which the cache still holds. One the - /// cache gave up on is let go here, releasing the subscription's end its stream held, since - /// the stream asking opens the group afresh. - fn is_paused(&mut self, key: u64) -> bool { - let Some(registry) = &self.paused else { - return false; - }; - let Ok(mut state) = registry.write() else { - return false; - }; - match state.groups.get(&key) { - None => false, - Some(paused) if !paused.group.handle().is_aborted() => true, - Some(_) => { - let given_up = state.groups.remove(&key); - drop(state); - drop(given_up); - false - } - } - } - - /// Continue the group a YIELDED reset paused with the stream whose header is `hdr`, - /// counted by `reading`, or `None` if no group with its key is paused. - fn resume(&mut self, hdr: &lite::Group, reading: Reading) -> Option> { - let paused = self.paused.as_ref()?.write().ok()?.groups.remove(&hdr.key)?; - let Some(Accepted { producer, framing }) = &self.copy else { - return Some(Err(Error::Cancel)); - }; - // The stream that paused it is replaced, so it no longer holds the end. - let PausedGroup { - group, - partial, - reading: replaced, - } = paused; - drop(replaced); - // A continuation starting past what the group holds would leave a hole in it. - if hdr.frame_start > PausedGroup::resumes_by_parts(&group, partial.as_ref()) { - tracing::warn!( - group = hdr.key, - frame_start = hdr.frame_start, - "continuation past the frames held" - ); - let _ = group.abort(Error::ProtocolViolation); - return Some(Err(Error::ProtocolViolation)); - } - Some(Ok(Open::Group(Opened { - group, - partial: partial.map(Box::new), - verdict: YieldVerdict::Owed, - track: producer.clone(), - framing: *framing, - reading, - }))) - } - - /// Pause `group`, whose stream the publisher reset with YIELDED, for a later stream to - /// continue. Groups the cache gave up on meanwhile are swept out as the held ones double. - fn pause(&mut self, key: u64, paused: PausedGroup) { - let registry = self.paused.get_or_insert_with(Default::default); - let Ok(mut state) = registry.write() else { - return; - }; - if state.groups.len() >= state.swept.max(1) * 2 { - state.groups.retain(|_, paused| !paused.group.handle().is_aborted()); - state.swept = state.groups.len(); - } - state.groups.insert(key, paused); - } - - /// Track a stream reading `key`, returning its token for [`Self::end_yielding`]. - fn read_yielding(&mut self, key: u64, verdict: YieldVerdict) -> u64 { - self.yield_streams += 1; - let stream = self.yield_streams; - self.yielding.insert(key, Yielding { stream, verdict }); - stream - } - - /// Stop tracking the stream reading `key`, returning whether a YIELDED reset pauses it. - /// A stream that a later one for the same group replaced finds nothing, leaving the - /// later stream's record in place. A stale drop an update sent since superseded no - /// longer ends the group. - fn end_yielding(&mut self, key: u64, stream: u64) -> Option { - let mut yielding = match self.yielding.get(&key) { - Some(yielding) if yielding.stream == stream => self.yielding.remove(&key)?, - _ => return None, - }; - yielding.verdict = yielding.verdict.updated(self.tail.read().updates()); - Some(yielding) - } - - /// A SUBSCRIBE_DROP named `groups` with `err`, `stale` when it is a lite-07 stale verdict: a - /// paused group ends there, and one a stream is reading ends with it if a YIELDED reset - /// would otherwise pause it, since no continuation follows, unless an update supersedes a - /// stale drop first. - fn dropped(&mut self, groups: std::ops::Range, err: &Error, stale: bool) { - let updates = stale.then(|| self.tail.read().updates()); - for (_, yielding) in self.yielding.range_mut(groups.clone()) { - yielding.verdict.drop_with(err.clone(), updates); - } - let Some(registry) = &self.paused else { - return; - }; - let ended: Vec<_> = match registry.write() { - Ok(mut state) => { - let keys: Vec = state.groups.range(groups).map(|(key, _)| *key).collect(); - keys.into_iter().filter_map(|key| state.groups.remove(&key)).collect() - } - Err(_) => return, - }; - for paused in ended { - let _ = paused.group.abort(err.clone()); - } - } - - /// Watch for a YIELDED reset pausing a copy still arriving. - fn watch_paused(&mut self) -> kio::Consumer { - self.paused.get_or_insert_with(Default::default).consume() + self.place(hdr, version, received, reading) } /// Open the group of a held stream again, once the copy it waited on failed. @@ -678,8 +437,7 @@ impl TrackEntry { return Err(Error::Cancel); }; let received = producer.create_group_at(group::Info { start: hdr.key }, hdr.frame_start); - // Served again, so owed again. - self.place(hdr, version, received, reading, YieldVerdict::Owed) + self.place(hdr, version, received, reading) } /// Give a stream what the cache answered for its header. @@ -689,7 +447,6 @@ impl TrackEntry { version: Version, received: Result, reading: Reading, - verdict: YieldVerdict, ) -> Result { let Some(Accepted { producer, framing }) = &self.copy else { return Err(Error::Cancel); @@ -699,7 +456,6 @@ impl TrackEntry { Ok(track::Received::Held(arriving)) => { return Ok(Open::Duplicate(Duplicate { arriving, - paused: None, track: producer.clone(), reading, })); @@ -722,8 +478,6 @@ impl TrackEntry { }; Ok(Open::Group(Opened { group: crate::recv::Group::new(group), - partial: None, - verdict, track: producer.clone(), framing: *framing, reading, @@ -1738,15 +1492,6 @@ impl GroupRecv { self.park_wait = None; let ietf: bool = header.0; let hdr: lite::Group = header.1; - // Lite-07 on a track without object records: a YIELDED reset pauses the group. - let yields: bool = self.subscriber.version.has_yield() - && !(ietf - && self - .subscriber - .subscribes - .lock() - .get(&hdr.subscribe) - .is_some_and(|entry| entry.has_records())); if let (Err(err), Some((_, ticket))) = (&accepted, &parked) && let Some(entry) = self.subscriber.subscribes.lock().get_mut(&hdr.subscribe) { @@ -1768,7 +1513,7 @@ impl GroupRecv { } // A duplicate of a copy still arriving waits, unread, for that copy to settle. let opened: Opened = wait!(Hold => loop { - let mut duplicate = match open.take() { + let duplicate = match open.take() { Some(Open::Group(opened)) => break Poll::Ready(Ok(opened)), Some(Open::Duplicate(duplicate)) => duplicate, None => break Poll::Ready(Err(Error::Cancel)), @@ -1779,25 +1524,6 @@ impl GroupRecv { if let Poll::Ready(err) = duplicate.track.poll_aborted(waiter) { break Poll::Ready(Err(err)); } - // A YIELDED reset pauses the copy still arriving, which this stream continues. - if *yields { - let mut subs = self.subscriber.subscribes.lock(); - let Some(entry) = subs.get_mut(&hdr.subscribe) else { - break Poll::Ready(Err(Error::Cancel)); - }; - let paused = duplicate.paused.get_or_insert_with(|| entry.watch_paused()); - let _ = paused.poll(waiter, |_| Poll::<()>::Pending); - if entry.is_paused(hdr.key) { - let resumed = entry.resume(hdr, duplicate.reading).expect("checked paused above"); - match resumed { - Ok(resumed) => { - *open = Some(resumed); - continue; - } - Err(err) => break Poll::Ready(Err(err)), - } - } - } match duplicate.arriving.arriving().poll_closed(waiter) { Poll::Pending => { *open = Some(Open::Duplicate(duplicate)); @@ -1824,21 +1550,10 @@ impl GroupRecv { })?; let Opened { group, - partial: cut, - verdict, track, framing, reading, } = opened; - let yield_stream: Option = match yields { - true => self - .subscriber - .subscribes - .lock() - .get_mut(&hdr.subscribe) - .map(|entry| entry.read_yielding(hdr.key, verdict)), - false => None, - }; let (priority, order) = track .subscription() .map_or((0, Default::default()), |sub| (sub.priority, sub.order)); @@ -1850,8 +1565,7 @@ impl GroupRecv { // Guarded: dropping this machine mid-group is a cancellation, not a clean end. let group: crate::recv::Group = group; let track: track::Producer = track; - let mut ingest: FrameIngest = - FrameIngest::new(framing, ietf, hdr.frame_start).yielding(yields, cut.map(|cut| *cut)); + let mut ingest: FrameIngest = FrameIngest::new(framing, ietf, hdr.frame_start); let reading: Reading = reading; // Filling the group, bailing if the track or group dies first. The track or group @@ -1866,13 +1580,6 @@ impl GroupRecv { } }); - let yielding = yield_stream.and_then(|stream| { - self.subscriber - .subscribes - .lock() - .get_mut(&hdr.subscribe) - .and_then(|entry| entry.end_yielding(hdr.key, stream)) - }); let res = match res { Ok(()) => { // Only a lite-07 stale drop asks which groups arrived whole. @@ -1885,33 +1592,6 @@ impl GroupRecv { let _ = group.abort(err); Ok(()) } - // The publisher made room for a higher-priority stream: the group pauses, keeping - // what arrived and the subscription's end waiting, until a later stream continues it - // or a drop names it. - Err(Error::Stream(crate::StreamError::Yielded)) if yields => { - // A drop named the group first: the publisher gave it up, so nothing continues it. - // One that overtook the stream's header carried a stale verdict. - let err = match yielding { - Some(yielding) => yielding.verdict.error(), - None => Some(Error::Cancel), - }; - if let Some(err) = err { - let _ = group.abort(err); - drop((ingest, reading)); - return Poll::Ready(Ok(())); - } - let paused = PausedGroup { - partial: ingest.take_partial(), - group, - reading, - }; - tracing::debug!(group = hdr.key, "group paused"); - // A subscription gone meanwhile ends it as cancelled. - if let Some(entry) = self.subscriber.subscribes.lock().get_mut(&hdr.subscribe) { - entry.pause(hdr.key, paused); - } - return Poll::Ready(Ok(())); - } Err(err) => { tracing::debug!(%err, group = %group.key, "group error"); let _ = group.abort(err.clone()); @@ -1940,14 +1620,6 @@ pub(crate) struct FrameIngest { /// `Some` once the Group State of the `ietf` extension arrived: every frame follows an /// object record. records: Option, - /// The index of the next frame on the stream. - index: u64, - /// Lite-07 Group Stream without object records: a YIELDED reset pauses the group, - /// keeping a frame it cut short for the continuation. - yields: bool, - /// The frame a YIELDED reset cut short, holding the group's writer: kept on a pause, and - /// completed by the continuation. - partial: Option, } /// A group's object records on ingest, under the `ietf` extension. @@ -1995,11 +1667,6 @@ enum IngestPhase { }, /// Waiting to admit the frame until the group's fastest reader leaves room for it. Admit { header: frame::Header }, - /// A continuation re-sending a frame the group holds: discarding its payload. - Skip { left: u64 }, - /// A continuation re-sending the frame a YIELDED reset cut short: discarding the bytes - /// it holds, then completing it. - Resume { skip: u64 }, /// Streaming the frame payload. Payload { frame: frame::Producer }, } @@ -2016,56 +1683,6 @@ impl FrameIngest { }, frame_start, records: None, - index: frame_start, - yields: false, - partial: None, - } - } - - /// Whether a YIELDED reset pauses the group rather than ends it (a lite-07 Group Stream - /// without object records), continuing a paused group that holds the frame `partial` cut - /// short, if any. - pub(crate) fn yielding(mut self, yields: bool, partial: Option) -> Self { - self.yields = yields; - self.partial = partial; - self - } - - /// The frame a YIELDED reset cut short, which the pause keeps. - pub(crate) fn take_partial(&mut self) -> Option { - self.partial.take() - } - - /// The stream ended on a frame boundary: the group ends, unless a continuation ended - /// before completing the frame its group held cut short. - fn end(&self) -> Result<(), Error> { - match self.partial { - Some(_) => Err(Error::ProtocolViolation), - None => Ok(()), - } - } - - /// Where the frame `header` opens goes: a new frame of the group, or on a continuation, - /// one the group holds already, skipped, or the one a YIELDED reset cut short, completed. - fn dispatch(&mut self, group: &crate::recv::Group, header: frame::Header) -> Result { - let index = self.index; - // The peer's Frame Start can number a frame past any count. - self.index = index.checked_add(1).ok_or(Error::ProtocolViolation)?; - let held = group.frame_count() as u64; - if index >= held { - return Ok(IngestPhase::Admit { header }); - } - match &self.partial { - Some(frame) if index + 1 == held => { - // The same frame, so the same size: a publisher reusing the group's identity is - // refused rather than written past. - if frame.size != header.info.size { - return Err(Error::ProtocolViolation); - } - let skip = frame.size - frame.remaining() as u64; - Ok(IngestPhase::Resume { skip }) - } - _ => Ok(IngestPhase::Skip { left: header.info.size }), } } @@ -2101,10 +1718,7 @@ impl FrameIngest { let lite::ietf::Decoded { properties, gap } = lite::ietf::decode_properties(&block, self.framing, true)?; group.mark_gap(id, gap)?; - // A plain group's frames are numbered by index, so a continuation re-sending - // frames its group holds finds them below its count. - let next = id == group.frame_count() as u64 || (!records.relayed && id == self.index); - let plain = next && properties.is_empty(); + let plain = id == group.frame_count() as u64 && properties.is_empty(); if !plain && !records.relayed { return Err(Error::ProtocolViolation); } @@ -2169,7 +1783,7 @@ impl FrameIngest { } IngestPhase::Object => { let Some(code) = ready!(reader.poll_varint_maybe(waiter))? else { - return Poll::Ready(self.end()); + return Poll::Ready(Ok(())); }; let records = self.records.as_mut().expect("the Group State came first"); let (id, end) = lite::ietf::decode_object(records.prior, code)?; @@ -2216,11 +1830,11 @@ impl FrameIngest { if ready!(reader.poll_has_more(waiter))? { return Poll::Ready(Err(Error::ProtocolViolation)); } - return Poll::Ready(self.end()); + return Poll::Ready(Ok(())); } IngestPhase::Length => { let Some(length) = ready!(self.poll_header_varint(reader, true, waiter))? else { - return Poll::Ready(self.end()); + return Poll::Ready(Ok(())); }; // 0 is reserved for GROUP_END, which no version defines yet. let Some(size) = length.checked_sub(1) else { @@ -2236,7 +1850,7 @@ impl FrameIngest { }; // Before lite-07 the timestamp delta doubles as the per-frame sentinel. let Some(zz) = ready!(self.poll_header_varint(reader, size.is_none(), waiter))? else { - return Poll::Ready(self.end()); + return Poll::Ready(Ok(())); }; let next: u64 = (self.prev_ts as i128 + crate::coding::varint::unzigzag(zz) as i128) .try_into() @@ -2256,17 +1870,16 @@ impl FrameIngest { true => { let boundary = size.is_none() && self.framing.timescale.is_none(); let Some(inflated) = ready!(self.poll_header_varint(reader, boundary, waiter))? else { - return Poll::Ready(self.end()); + return Poll::Ready(Ok(())); }; Some(inflated) } false => None, }; self.phase = match size { - Some(size) => { - let header = self.header(group, timestamp, inflated, size)?; - self.dispatch(group, header)? - } + Some(size) => IngestPhase::Admit { + header: self.header(group, timestamp, inflated, size)?, + }, None => IngestPhase::Size { timestamp, inflated }, }; } @@ -2274,10 +1887,11 @@ impl FrameIngest { let (timestamp, inflated) = (*timestamp, *inflated); let boundary = self.framing.timescale.is_none() && !group.compressed(); let Some(size) = ready!(self.poll_header_varint(reader, boundary, waiter))? else { - return Poll::Ready(self.end()); + return Poll::Ready(Ok(())); + }; + self.phase = IngestPhase::Admit { + header: self.header(group, timestamp, inflated, size)?, }; - let header = self.header(group, timestamp, inflated, size)?; - self.phase = self.dispatch(group, header)?; } IngestPhase::Admit { header } => { ready!(reader.poll_admit(waiter, group, header))?; @@ -2292,19 +1906,6 @@ impl FrameIngest { let frame = group.create_object(header)?; self.phase = IngestPhase::Payload { frame }; } - IngestPhase::Skip { left } => { - ready!(poll_skip(reader, left, waiter))?; - self.phase = self.next_frame(); - ready!(budget.poll_yield(waiter)); - } - IngestPhase::Resume { skip } => { - ready!(poll_skip(reader, skip, waiter))?; - let frame = self - .partial - .take() - .expect("a continuation resumes the frame its group holds"); - self.phase = IngestPhase::Payload { frame }; - } IngestPhase::Payload { frame } => { let failed = ready!(reader.poll_read_frame(waiter, frame)).err(); @@ -2314,12 +1915,6 @@ impl FrameIngest { }; match failed { None => group.restore(frame.finish()?), - // A YIELDED reset pauses the group: the frame it cut short waits for the - // continuation. - Some(err @ Error::Stream(crate::StreamError::Yielded)) if self.yields => { - self.partial = Some(frame); - return Poll::Ready(Err(err)); - } Some(err) => { // Fail the group with the reason, not the Drop fallback's // generic `Dropped`. @@ -2336,23 +1931,6 @@ impl FrameIngest { } } -/// Discard the next `left` bytes of `reader`, counting them down so a wait resumes where it -/// stopped. A stream ending first cut a frame's payload short. -fn poll_skip( - reader: &mut Reader, - left: &mut u64, - waiter: &kio::Waiter, -) -> Poll> { - while *left > 0 { - let max = usize::try_from(*left).unwrap_or(usize::MAX); - match ready!(reader.poll_read_chunk(waiter, max))? { - Some(chunk) => *left -= chunk.len() as u64, - None => return Poll::Ready(Err(Error::WrongSize)), - } - } - Poll::Ready(Ok(())) -} - /// Receives QUIC datagrams and routes each to its subscription's track producer /// (lite-05 §6.4). /// @@ -5678,79 +5256,51 @@ mod tests { } } - /// Lite-07: a stream reset with YIELDED pauses its group, keeping the frame it cut short, - /// and a later stream continues it from the paused stream's Frame Start, whether it lands - /// after that reset or overtakes it. The group's reader gets every frame once, then its end. + /// A stream reset to make room for a higher-priority one ends its group with the reset's + /// code on every version: the frames that arrived stay, and no later stream continues it. #[kio::test] - async fn a_yielded_group_continues_without_loss() { + async fn a_group_reset_to_make_room_ends() { use crate::lite::test_transport::{Close, ScriptedSession}; use crate::transport::poll::Session as _; - let version = Version::Lite07; - let framing = lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: true, - }; - // The `ietf` extension's plain records: a Group State of 0 after the header, then a - // record before each frame, as a moq-lite track on a relay link carries them. - let header = |ietf: bool| { + for version in [Version::Lite06, Version::Lite07] { + let length_first = version == Version::Lite07; + let framing = lite::Framing { + timescale: Some(Timescale::MILLI), + ietf: None, + compressed: false, + length_first, + }; let mut script = Vec::new(); + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); lite::Group { subscribe: 7, key: 3, frame_start: 0, } - .encode(&mut crate::coding::Encoder::new(&mut script, version.into()), version) + .encode(&mut encoder, version) .unwrap(); - if ietf { - script.push(0); - } - script - }; - // A lite-07 FRAME: the payload size plus one, the timestamp delta, then the first - // `sent` bytes of the payload. - let frame = |script: &mut Vec, ietf: bool, delta: i64, payload: &[u8], sent: usize| { - if ietf { - // The next Object ID, with no properties. - script.extend_from_slice(&[0, 0]); - } - let mut encoder = crate::coding::Encoder::new(&mut *script, version.into()); - encoder.varint(payload.len() as u64 + 1).unwrap(); - encoder.varint(crate::coding::varint::zigzag(delta)).unwrap(); - script.extend_from_slice(&payload[..sent]); - }; - let frames: [(i64, &[u8]); 3] = [(300, b"zero"), (10, b"onetwo!"), (10, b"three")]; - - for (overtakes, ietf) in [(false, false), (true, false), (false, true), (true, true)] { // Frame 0 whole, then frame 1 cut after three bytes. - let mut cut = header(ietf); - frame(&mut cut, ietf, frames[0].0, frames[0].1, frames[0].1.len()); - frame(&mut cut, ietf, frames[1].0, frames[1].1, 3); - let mut whole = header(ietf); - for (delta, payload) in frames { - frame(&mut whole, ietf, delta, payload, payload.len()); - } - let yielded = ScriptedSession::new(cut); - let continued = ScriptedSession::eof(whole); - let clock = crate::time::Deadlines::new(crate::time::Instant::now()); - let peer_setup = crate::lite::PeerSetup::new(false, crate::transport::Address::Setup).offer_ietf(ietf); - if ietf { - peer_setup - .set(lite::Setup { - ietf: true, - ..Default::default() - }) - .unwrap(); + for (delta, payload, sent) in [(300, &b"zero"[..], 4), (10, &b"onetwo!"[..], 3)] { + let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); + if length_first { + encoder.varint(payload.len() as u64 + 1).unwrap(); + encoder.varint(crate::coding::varint::zigzag(delta)).unwrap(); + } else { + encoder.varint(crate::coding::varint::zigzag(delta)).unwrap(); + encoder.varint(payload.len() as u64).unwrap(); + } + script.extend_from_slice(&payload[..sent]); } + let session = ScriptedSession::new(script); + let clock = crate::time::Deadlines::new(crate::time::Instant::now()); let subscriber = Subscriber::new(SubscriberConfig { runtime: clock.clone(), - session: crate::driver::Transport::new(yielded.clone()), + session: crate::driver::Transport::new(session.clone()), origin: origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(), recv_bandwidth: None, version, - peer_setup, + peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), peer_hop: None, cost: None, going_away: Default::default(), @@ -5771,403 +5321,30 @@ mod tests { .lock() .insert(7, TrackEntry::accepted(track.clone(), framing, tail.clone())); - let (_, recv) = yielded.open_bi(Default::default()).await.unwrap(); - let mut first = GroupRecv::new( - subscriber.clone(), - Reader::new(crate::transport::boxed::RecvStream::new(recv), version), - ); - let (_, recv) = continued.open_bi(Default::default()).await.unwrap(); - let mut second = GroupRecv::new( + let (_, recv) = session.open_bi(Default::default()).await.unwrap(); + let mut stream = GroupRecv::new( subscriber.clone(), Reader::new(crate::transport::boxed::RecvStream::new(recv), version), ); - let waiter = kio::Waiter::noop(); - assert!( - first.poll_serve(&waiter).is_pending(), - "the cut frame waits for its rest" - ); - if overtakes { - assert!(second.poll_serve(&waiter).is_pending()); - assert_eq!(second.state.name(), "Hold", "the continuation waits for the reset"); - } - yielded.close(Close::ResetStream(crate::StreamError::Yielded.to_code())); - let res = kio::wait(|waiter| first.poll_serve(waiter)).await; - assert!(res.is_ok(), "a YIELDED reset pauses rather than fails: {res:?}"); - let res = kio::wait(|waiter| second.poll_serve(waiter)).await; - assert!(res.is_ok(), "overtakes: {overtakes}, ietf: {ietf}: {res:?}"); - + assert!(stream.poll_serve(&kio::Waiter::noop()).is_pending(), "{version:?}"); let mut group = reader.assert_group(); assert_eq!(group.start, 3); - for (_, payload) in frames { - let read = group.read_frame().await.unwrap().unwrap(); - assert_eq!(&read.payload[..], payload, "overtakes: {overtakes}, ietf: {ietf}"); - } + let read = group.read_frame().await.unwrap().unwrap(); + assert_eq!(&read.payload[..], b"zero", "{version:?}"); + + session.close(Close::ResetStream(crate::StreamError::TooFarBehind.to_code())); + let res = kio::wait(|waiter| stream.poll_serve(waiter)).await; + assert!(res.is_err(), "{version:?}: {res:?}"); + let read = group.read_frame().await; assert!( - group.read_frame().await.unwrap().is_none(), - "overtakes: {overtakes}, ietf: {ietf}" + matches!(read, Err(Error::Stream(crate::StreamError::TooFarBehind))), + "{version:?}: {read:?}" + ); + assert!( + !tail.read().is_reading(), + "{version:?}: the reset stream lets go of the end" ); - assert_eq!(tail.read().streams(), 2, "each stream counts toward the end"); - } - } - - /// Lite-07: a stream ending removes only its own record of the group it read, never that of - /// a later stream for the same group, which a YIELDED reset must still pause. - #[test] - fn a_replaced_stream_leaves_its_replacements_yielding_record() { - let track = track::Producer::new( - std::sync::Arc::new(crate::broadcast::Info::default()), - "video", - crate::track::Info::timed(), - ); - let framing = lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: true, - }; - let mut entry = TrackEntry::accepted(track, framing, kio::Producer::new(Tail::default())); - let replaced = entry.read_yielding(3, YieldVerdict::Owed); - let replacement = entry.read_yielding(3, YieldVerdict::Owed); - assert!( - entry.end_yielding(3, replaced).is_none(), - "the replaced stream's record is gone" - ); - let yielding = entry - .end_yielding(3, replacement) - .expect("the replacement's record stays"); - assert!(matches!(yielding.verdict, YieldVerdict::Owed)); - } - - /// Lite-07: a paused group the cache gave up on lets go of the subscription's end once a - /// continuation arrives, which opens the group afresh. - #[kio::test] - async fn a_paused_group_the_cache_gave_up_lets_go_of_the_end() { - use crate::lite::test_transport::{Close, ScriptedSession}; - use crate::transport::poll::Session as _; - - let version = Version::Lite07; - let header = || { - let mut script = Vec::new(); - lite::Group { - subscribe: 7, - key: 3, - frame_start: 0, - } - .encode(&mut crate::coding::Encoder::new(&mut script, version.into()), version) - .unwrap(); - let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); - encoder.varint(2).unwrap(); - encoder.varint(crate::coding::varint::zigzag(300)).unwrap(); - script.push(b'x'); - script - }; - let yielded = ScriptedSession::new(header()); - let continued = ScriptedSession::eof(header()); - let clock = crate::time::Deadlines::new(crate::time::Instant::now()); - let subscriber = Subscriber::new(SubscriberConfig { - runtime: clock.clone(), - session: crate::driver::Transport::new(yielded.clone()), - origin: origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(), - recv_bandwidth: None, - version, - peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), - peer_hop: None, - cost: None, - going_away: Default::default(), - auth: crate::auth::Link::new(false), - }); - let track = track::Producer::new( - std::sync::Arc::new(crate::broadcast::Info::default()), - "video", - crate::track::Info::timed(), - ); - let subscription = crate::track::Subscription::default() - .with_delay(crate::track::Delay::default().with_max(std::time::Duration::from_secs(10))) - .with_range(0..); - let _reader = track.subscribe(subscription); - let framing = lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: true, - }; - let tail = kio::Producer::new(Tail::default()); - subscriber - .subscribes - .lock() - .insert(7, TrackEntry::accepted(track.clone(), framing, tail.clone())); - - let (_, recv) = yielded.open_bi(Default::default()).await.unwrap(); - let mut first = GroupRecv::new( - subscriber.clone(), - Reader::new(crate::transport::boxed::RecvStream::new(recv), version), - ); - assert!(first.poll_serve(&kio::Waiter::noop()).is_pending()); - yielded.close(Close::ResetStream(crate::StreamError::Yielded.to_code())); - let res = kio::wait(|waiter| first.poll_serve(waiter)).await; - assert!(res.is_ok(), "{res:?}"); - assert!(tail.read().is_reading(), "the paused stream holds the end"); - - // The cache gives the paused group up, as eviction would. - { - let subs = subscriber.subscribes.lock(); - let paused = subs.get(&7).unwrap().paused.as_ref().unwrap().read(); - let _ = paused.groups.get(&3).unwrap().group.handle().abort(Error::Old); - } - let (_, recv) = continued.open_bi(Default::default()).await.unwrap(); - let mut second = GroupRecv::new( - subscriber.clone(), - Reader::new(crate::transport::boxed::RecvStream::new(recv), version), - ); - let res = kio::wait(|waiter| second.poll_serve(waiter)).await; - assert!(res.is_ok(), "{res:?}"); - assert!(!tail.read().is_reading(), "no stream holds the end"); - let subs = subscriber.subscribes.lock(); - let paused = subs.get(&7).unwrap().paused.as_ref().unwrap().read(); - assert!(paused.groups.is_empty(), "the given-up group is let go"); - } - - /// Lite-07: a drop can overtake the YIELDED reset of the stream it names. The group ends - /// with the drop's error rather than waiting for a continuation the publisher never sends. - #[kio::test] - async fn a_yielded_group_a_drop_named_ends() { - use crate::lite::test_transport::{Close, ScriptedSession}; - use crate::transport::poll::Session as _; - - let version = Version::Lite07; - let mut script = Vec::new(); - let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); - lite::Group { - subscribe: 7, - key: 3, - frame_start: 0, - } - .encode(&mut encoder, version) - .unwrap(); - encoder.varint(2).unwrap(); - encoder.varint(crate::coding::varint::zigzag(300)).unwrap(); - script.push(b'x'); - let yielded = ScriptedSession::new(script); - let clock = crate::time::Deadlines::new(crate::time::Instant::now()); - let subscriber = Subscriber::new(SubscriberConfig { - runtime: clock.clone(), - session: crate::driver::Transport::new(yielded.clone()), - origin: origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(), - recv_bandwidth: None, - version, - peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), - peer_hop: None, - cost: None, - going_away: Default::default(), - auth: crate::auth::Link::new(false), - }); - let track = track::Producer::new( - std::sync::Arc::new(crate::broadcast::Info::default()), - "video", - crate::track::Info::timed(), - ); - let subscription = crate::track::Subscription::default() - .with_delay(crate::track::Delay::default().with_max(std::time::Duration::from_secs(10))) - .with_range(0..); - let mut reader = track.subscribe(subscription); - let framing = lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: true, - }; - subscriber.subscribes.lock().insert( - 7, - TrackEntry::accepted(track.clone(), framing, kio::Producer::new(Tail::default())), - ); - - let (_, recv) = yielded.open_bi(Default::default()).await.unwrap(); - let mut first = GroupRecv::new( - subscriber.clone(), - Reader::new(crate::transport::boxed::RecvStream::new(recv), version), - ); - assert!(first.poll_serve(&kio::Waiter::noop()).is_pending()); - let mut group = reader.assert_group(); - // The publisher gave the group up after pausing it, and its drop lands first. - subscriber - .subscribes - .lock() - .get_mut(&7) - .unwrap() - .dropped(3..4, &Error::Stream(crate::StreamError::Old), true); - yielded.close(Close::ResetStream(crate::StreamError::Yielded.to_code())); - let res = kio::wait(|waiter| first.poll_serve(waiter)).await; - assert!(res.is_ok(), "{res:?}"); - - let read = group.read_frame().await; - assert!(matches!(read, Err(Error::Stream(crate::StreamError::Old))), "{read:?}"); - assert!( - subscriber.subscribes.lock().get(&7).unwrap().paused.is_none(), - "nothing paused" - ); - } - - /// Lite-07: a stale drop that overtook the YIELDED reset of the stream it names holds only - /// under the update it names: once the subscriber sends another, the reset pauses the - /// group, which the publisher then continues or drops again. - #[kio::test] - async fn a_yielded_group_pauses_once_an_update_supersedes_its_stale_drop() { - use crate::lite::test_transport::{Close, ScriptedSession}; - use crate::transport::poll::Session as _; - - let version = Version::Lite07; - let mut script = Vec::new(); - let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); - lite::Group { - subscribe: 7, - key: 3, - frame_start: 0, - } - .encode(&mut encoder, version) - .unwrap(); - encoder.varint(2).unwrap(); - encoder.varint(crate::coding::varint::zigzag(300)).unwrap(); - script.push(b'x'); - let yielded = ScriptedSession::new(script); - let clock = crate::time::Deadlines::new(crate::time::Instant::now()); - let subscriber = Subscriber::new(SubscriberConfig { - runtime: clock.clone(), - session: crate::driver::Transport::new(yielded.clone()), - origin: origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(), - recv_bandwidth: None, - version, - peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), - peer_hop: None, - cost: None, - going_away: Default::default(), - auth: crate::auth::Link::new(false), - }); - let track = track::Producer::new( - std::sync::Arc::new(crate::broadcast::Info::default()), - "video", - crate::track::Info::timed(), - ); - let subscription = crate::track::Subscription::default() - .with_delay(crate::track::Delay::default().with_max(std::time::Duration::from_secs(10))) - .with_range(0..); - let mut reader = track.subscribe(subscription); - let framing = lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: true, - }; - let tail = kio::Producer::new(Tail::default()); - subscriber - .subscribes - .lock() - .insert(7, TrackEntry::accepted(track.clone(), framing, tail.clone())); - - let (_, recv) = yielded.open_bi(Default::default()).await.unwrap(); - let mut first = GroupRecv::new( - subscriber.clone(), - Reader::new(crate::transport::boxed::RecvStream::new(recv), version), - ); - assert!(first.poll_serve(&kio::Waiter::noop()).is_pending()); - let mut group = reader.assert_group(); - // The publisher gave the group up after pausing it, and its drop lands first. - subscriber - .subscribes - .lock() - .get_mut(&7) - .unwrap() - .dropped(3..4, &Error::Stream(crate::StreamError::Old), true); - // An update supersedes it, as `buffer_update` does. - tail.write().ok().expect("the tail is open").updated(); - yielded.close(Close::ResetStream(crate::StreamError::Yielded.to_code())); - let res = kio::wait(|waiter| first.poll_serve(waiter)).await; - assert!(res.is_ok(), "{res:?}"); - - let read = group.read_frame().await; - assert!(matches!(read, Ok(Some(_))), "the frame that arrived stays: {read:?}"); - let subs = subscriber.subscribes.lock(); - let paused = subs.get(&7).unwrap().paused.as_ref().unwrap().read(); - assert!(paused.groups.contains_key(&3), "the group pauses"); - } - - /// Lite-07: a stale drop that overtook the header of the stream it names holds only under - /// the update it names: once the subscriber sends another, a YIELDED reset of that stream - /// pauses the group rather than ending it as stale. - #[kio::test] - async fn a_yielded_group_pauses_once_an_update_supersedes_a_stale_drop_before_its_header() { - use crate::lite::test_transport::{Close, ScriptedSession}; - use crate::transport::poll::Session as _; - - let version = Version::Lite07; - let mut script = Vec::new(); - let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); - lite::Group { - subscribe: 7, - key: 3, - frame_start: 0, } - .encode(&mut encoder, version) - .unwrap(); - encoder.varint(2).unwrap(); - encoder.varint(crate::coding::varint::zigzag(300)).unwrap(); - script.push(b'x'); - let yielded = ScriptedSession::new(script); - let clock = crate::time::Deadlines::new(crate::time::Instant::now()); - let subscriber = Subscriber::new(SubscriberConfig { - runtime: clock.clone(), - session: crate::driver::Transport::new(yielded.clone()), - origin: origin::Config::new(std::num::NonZeroU64::new(1).unwrap()).produce(), - recv_bandwidth: None, - version, - peer_setup: crate::lite::PeerSetup::new(false, crate::transport::Address::Setup), - peer_hop: None, - cost: None, - going_away: Default::default(), - auth: crate::auth::Link::new(false), - }); - let track = track::Producer::new( - std::sync::Arc::new(crate::broadcast::Info::default()), - "video", - crate::track::Info::timed(), - ); - let subscription = crate::track::Subscription::default() - .with_delay(crate::track::Delay::default().with_max(std::time::Duration::from_secs(10))) - .with_range(0..); - let mut reader = track.subscribe(subscription); - let framing = lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: true, - }; - let tail = kio::Producer::new(Tail::default()); - subscriber - .subscribes - .lock() - .insert(7, TrackEntry::accepted(track.clone(), framing, tail.clone())); - - // The drop lands before the stream's header, as `apply_response` records it. - track.give_up(3..4, track::Hole::stale(std::time::Duration::from_secs(10))); - tail.write().ok().expect("the tail is open").account(3..4, clock.now()); - - let (_, recv) = yielded.open_bi(Default::default()).await.unwrap(); - let mut first = GroupRecv::new( - subscriber.clone(), - Reader::new(crate::transport::boxed::RecvStream::new(recv), version), - ); - assert!(first.poll_serve(&kio::Waiter::noop()).is_pending()); - let mut group = reader.assert_group(); - // An update supersedes it, as `buffer_update` does. - tail.write().ok().expect("the tail is open").updated(); - yielded.close(Close::ResetStream(crate::StreamError::Yielded.to_code())); - let res = kio::wait(|waiter| first.poll_serve(waiter)).await; - assert!(res.is_ok(), "{res:?}"); - - let read = group.read_frame().await; - assert!(matches!(read, Ok(Some(_))), "the frame that arrived stays: {read:?}"); - let subs = subscriber.subscribes.lock(); - let paused = subs.get(&7).unwrap().paused.as_ref().unwrap().read(); - assert!(paused.groups.contains_key(&3), "the group pauses"); } /// A SUBSCRIBE_FINISH below a group already received contradicts that group. lite-05 @@ -11134,8 +10311,7 @@ mod tests { let _a = slots.acquire().expect("the failed stream kept a slot"); let _b = slots.acquire().expect("the failed stream kept a slot"); } - /// A frame numbered past `u64::MAX` by a huge Frame Start is a protocol violation, not - /// a counter overflow. + /// A frame numbered past `u64::MAX` by a huge Frame Start is refused, not a counter overflow. #[kio::test] async fn group_frame_start_past_any_count_is_refused() { use crate::transport::poll::Session as _; @@ -11192,8 +10368,8 @@ mod tests { subscriber.clone(), Reader::new(crate::transport::boxed::RecvStream::new(recv), version), ); - let err = kio::wait(|waiter| group.poll_serve(waiter)).await.unwrap_err(); - assert!(matches!(err, Error::ProtocolViolation), "{err:?}"); + let res = kio::wait(|waiter| group.poll_serve(waiter)).await; + assert!(res.is_err(), "{res:?}"); } /// Distinct live GROUP starts are copies of the same group, including while @@ -13918,10 +13094,6 @@ impl ServeLoop { if let Ok(mut tail) = active.tail.write() { tail.account(groups.clone(), serve.subscriber.runtime.now()); } - // A paused group the drop names ends there: no continuation follows. - if let Some(entry) = serve.subscriber.subscribes.lock().get_mut(&active.id) { - entry.dropped(groups.clone(), &Error::Stream(error.clone()), stale); - } match stale { // Judged under the budget this subscriber sent with that update. A // key already skipped stays skipped, and one whose group arrived diff --git a/rs/moq-net/src/lite/version.rs b/rs/moq-net/src/lite/version.rs index ed399c5e5ff..64ea8948f17 100644 --- a/rs/moq-net/src/lite/version.rs +++ b/rs/moq-net/src/lite/version.rs @@ -296,17 +296,6 @@ impl Version { ) } - /// Whether a Group Stream reset with YIELDED pauses its group, which a later stream - /// continues. Added in lite-07; an earlier publisher out of stream credit gives the reset - /// group up. - #[cfg(feature = "async")] - pub(crate) fn has_yield(self) -> bool { - !matches!( - self, - Self::Lite01 | Self::Lite02 | Self::Lite03 | Self::Lite04 | Self::Lite05 | Self::Lite06 - ) - } - /// Whether SUBSCRIBE_DROP's Error Code is a stream error code, ending a dropped group's /// reader as a reset keeping its header would. Added in lite-07; earlier versions carry /// an application-specific code there. diff --git a/rs/moq-net/src/model/group.rs b/rs/moq-net/src/model/group.rs index e36ae7a2362..0bd9cb76a0d 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -3880,19 +3880,6 @@ impl Consumer { self.cursor.poll(waiter, |state| state.poll_outcome()) } - /// Resolves once the group is aborted, including when the cache releases it after it - /// finished, with the cause. Finishing alone does not end it. - #[cfg(feature = "async")] - pub(crate) fn poll_aborted(&self, waiter: &kio::Waiter) -> Poll { - let abort = |state: &GroupState| state.outcome.error().cloned().map_or(Poll::Pending, Poll::Ready); - // Not `Cursor::poll`: it reads a closed channel as `Dropped`, but every writer dropping - // after a finish aborts nothing, and with no writer left nothing ever will. - match ready!(self.cursor.state.poll(waiter, |state| abort(state))) { - Ok(err) => Poll::Ready(err), - Err(state) => abort(&state), - } - } - /// The index of the first frame the group holds (see [`Producer::start_at`]). pub(crate) fn first_frame(&self) -> u64 { self.cursor.state.read().offset as u64 @@ -6794,21 +6781,6 @@ mod test { assert!(matches!(consumer.poll_read_frame(&waiter), Poll::Ready(Ok(None)))); } - /// Every writer dropping after a finish aborts nothing, so a reader keeps its frames. - #[cfg(feature = "async")] - #[test] - fn writers_dropped_after_a_finish_abort_nothing() { - let mut producer = Info { start: 0 }.produce(); - producer.write_frame(Timestamp::ZERO, Bytes::from_static(b"a")).unwrap(); - let mut consumer = producer.consume(); - drop(producer.close().unwrap()); - let waiter = kio::Waiter::noop(); - - assert!(consumer.poll_aborted(&waiter).is_pending()); - assert!(matches!(consumer.poll_read_frame(&waiter), Poll::Ready(Ok(Some(_))))); - assert!(matches!(consumer.poll_read_frame(&waiter), Poll::Ready(Ok(None)))); - } - /// An abort before the finish is the outcome, on every observer. #[test] fn abort_before_finish_is_the_outcome() { diff --git a/rs/moq-net/src/model/track.rs b/rs/moq-net/src/model/track.rs index eeda631ad97..8bf51a1f8c4 100644 --- a/rs/moq-net/src/model/track.rs +++ b/rs/moq-net/src/model/track.rs @@ -4630,17 +4630,6 @@ impl Producer { state.supersede_stale() } - /// Whether a stale drop under the current terms gave up `key`, which a group arriving - /// for it replaces and an update supersedes. - #[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))] - pub(crate) fn holds_stale(&self, key: u64) -> bool { - self.state - .read() - .drops - .get(&key) - .is_some_and(|hole| hole.stale_budget().is_some()) - } - /// Record `groups` given up as `hole` describes, returning the runs recorded: a drop /// leaves a skipped key skipped, and a group a copy still serves whole, since a group /// stream wins over its drop: a stale drop leaves it still arriving or not, and a final one diff --git a/rs/moq-net/src/tail.rs b/rs/moq-net/src/tail.rs index 51ff2736a26..29d0b856cc5 100644 --- a/rs/moq-net/src/tail.rs +++ b/rs/moq-net/src/tail.rs @@ -38,8 +38,6 @@ pub(crate) struct Tail { // A stale drop there for a group the ledger already accounted may be one its stream // overtook, so the next update owes none of it again. folded: Option>, - // How many updates were sent, so a stale drop can tell whether a later one superseded it. - updates: u64, // What `finished` bills to the track's account, [`FINISHED_OVERHEAD`] per run, once the // subscription has a copy to bill. bill: Option, @@ -66,7 +64,6 @@ impl Tail { ended: false, finished: RangeSet::new(), folded: None, - updates: 0, bill: None, } } @@ -190,17 +187,11 @@ impl Tail { /// Forget which streams finished, for an update being sent: a stale drop under an earlier /// one is no verdict, so none needs them. pub fn updated(&mut self) { - self.updates += 1; self.finished = RangeSet::new(); self.folded = None; self.rebill(); } - /// How many updates were sent, each superseding the stale drops before it. - pub fn updates(&self) -> u64 { - self.updates - } - /// Whether a data stream whose header arrived is still being read. pub fn is_reading(&self) -> bool { self.active > 0 @@ -210,11 +201,6 @@ impl Tail { pub fn streams(&self) -> u64 { self.streams } - - /// Whether a stream or a drop accounted for `group` already. - pub fn accounts(&self, group: u64) -> bool { - self.ledger.covers(group..group.saturating_add(1)) - } } /// A data stream whose header arrived, holding its subscription's end open until dropped. diff --git a/rs/moq-net/src/transport/boxed.rs b/rs/moq-net/src/transport/boxed.rs index 24b14b9e24b..52428b4f6ce 100644 --- a/rs/moq-net/src/transport/boxed.rs +++ b/rs/moq-net/src/transport/boxed.rs @@ -282,14 +282,6 @@ impl SendStream { Self(Sender::Own(Box::new(stream))) } - /// Whether the session reset this group stream to make room for a higher-ranked open. - pub(crate) fn preempted(&self) -> bool { - match &self.0 { - Sender::Own(_) => false, - Sender::Listed(listing) => listing.listed.lock().preempted, - } - } - /// Run `f` on the transport's stream, then leave the session's list if `unlist`. fn with(&mut self, unlist: bool, f: impl FnOnce(&mut dyn ErasedSend) -> T) -> T { match &mut self.0 { diff --git a/rs/moq-net/tests/integration/stream_credit.rs b/rs/moq-net/tests/integration/stream_credit.rs index 3cc3118418c..79f604005fb 100644 --- a/rs/moq-net/tests/integration/stream_credit.rs +++ b/rs/moq-net/tests/integration/stream_credit.rs @@ -146,14 +146,18 @@ async fn a_newer_group_resets_an_unfinished_older_one() { } } -/// On lite-07 a group whose stream a newer one reset to make room only pauses: once credit -/// returns, a new stream continues it, and its reader receives every frame without an error. -/// So does one on a relay link, where the `ietf` extension changes nothing for a moq-lite -/// track, which carries no object records. +/// A group whose stream a newer one reset to make room is given up on its subscription, on +/// each lite version: its reader ends with TOO_FAR_BEHIND after the frames that arrived, and a +/// FETCH returns it whole once it finishes. So does one on a lite-07 relay link, where the +/// `ietf` extension changes nothing for a moq-lite track. #[kio::test] -async fn a_reset_group_resumes_without_loss() { - for ietf in [false, true] { - let version: Version = "moq-lite-07-wip".parse().unwrap(); +async fn a_reset_group_is_fetched_whole() { + for (version, ietf) in [ + ("moq-lite-06", false), + ("moq-lite-07-wip", false), + ("moq-lite-07-wip", true), + ] { + let version: Version = version.parse().unwrap(); let publisher = origin(1); let viewer = origin(2); let broadcast = publisher @@ -184,18 +188,19 @@ async fn a_reset_group_resumes_without_loss() { let prefs = track::Subscription::default() .with_order(track::Order::Descending) .with_delay(track::Delay::default().with_max(Duration::from_secs(30))); - let mut subscriber = remote.track("0").unwrap().subscribe(prefs).await.unwrap(); + let remote = remote.track("0").unwrap(); + let mut subscriber = remote.subscribe(prefs).await.unwrap(); // Unfinished, so its stream holds the only credit until the newer group resets it. let mut older = track.append_next().unwrap(); older.write_frame(older.at_start(), b"a".as_slice()).unwrap(); - let mut paused = kio::clock::timeout(Duration::from_secs(5), subscriber.recv_group()) + let mut reset = kio::clock::timeout(Duration::from_secs(5), subscriber.recv_group()) .await - .unwrap_or_else(|_| panic!("ietf: {ietf}: the older group arrives")) + .unwrap_or_else(|_| panic!("{version} ietf: {ietf}: the older group arrives")) .unwrap() .unwrap(); - assert_eq!(paused.start, 0); - let first = kio::clock::timeout(Duration::from_secs(5), paused.read_frame()) + assert_eq!(reset.start, 0); + let first = kio::clock::timeout(Duration::from_secs(5), reset.read_frame()) .await .expect("its first frame arrives") .unwrap(); @@ -211,165 +216,34 @@ async fn a_reset_group_resumes_without_loss() { .unwrap(); assert_eq!(group.start, 1); - // Written after the reset: a continuation carries them, behind the frame already held. + let end = kio::clock::timeout(Duration::from_secs(5), reset.read_frame()) + .await + .expect("the reset group ends"); + let err = end + .err() + .unwrap_or_else(|| panic!("{version} ietf: {ietf}: the reset group ends cleanly")); + assert_eq!( + moq_net::StreamError::from(&err), + moq_net::StreamError::TooFarBehind, + "{version} ietf: {ietf}" + ); + + // Written after the reset: no stream carries them, but a FETCH does. older.write_frame(older.at_start(), b"b".as_slice()).unwrap(); - older.write_frame(older.at_start(), b"c".as_slice()).unwrap(); older.close().unwrap(); - let mut rest = Vec::new(); - while let Some(frame) = kio::clock::timeout(Duration::from_secs(5), paused.read_frame()) + let mut fetched = kio::clock::timeout(Duration::from_secs(5), remote.fetch_group(0, None)) .await - .expect("the paused group continues") - .expect("the paused group is not given up") - { - rest.push(frame.payload); - } - assert_eq!(rest, [b"b".as_slice(), b"c".as_slice()]); - } -} - -/// On lite-07 a group paused while it waited for its next frame, which then finishes with no -/// more frames, still only pauses: the continuation ends it cleanly rather than a drop giving -/// it up. -#[kio::test] -async fn a_reset_group_finishing_without_more_frames_ends_cleanly() { - let version: Version = "moq-lite-07-wip".parse().unwrap(); - let publisher = origin(1); - let viewer = origin(2); - let broadcast = publisher - .route("live", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - let info = track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_max_age(None); - let track = broadcast.broadcast().create_track("0", info).unwrap(); - broadcast.announce().unwrap(); - - let mut options = MockConnectOptions::new(version); - options.transport.max_concurrent_uni_streams = Some(1); - options.server_publish = Some(publisher.consume()); - options.client_subscribe = Some(viewer.clone()); - let _pair = connect_mock(options).await; - - let remote = viewer - .consume() - .follow("live", None) - .unwrap() - .available() - .await - .unwrap() - .unwrap(); - let prefs = track::Subscription::default() - .with_order(track::Order::Descending) - .with_delay(track::Delay::default().with_max(Duration::from_secs(30))); - let mut subscriber = remote.track("0").unwrap().subscribe(prefs).await.unwrap(); - - let mut older = track.append_next().unwrap(); - older.write_frame(older.at_start(), b"a".as_slice()).unwrap(); - let mut paused = kio::clock::timeout(Duration::from_secs(5), subscriber.recv_group()) - .await - .expect("the older group arrives") - .unwrap() - .unwrap(); - let first = kio::clock::timeout(Duration::from_secs(5), paused.read_frame()) - .await - .expect("its first frame arrives") - .unwrap(); - assert_eq!(first.map(|frame| frame.payload), Some(bytes::Bytes::from_static(b"a"))); - - let mut newer = track.append_next().unwrap(); - newer.write_frame(newer.at_start(), b"new".as_slice()).unwrap(); - newer.close().unwrap(); - let group = kio::clock::timeout(Duration::from_secs(5), subscriber.recv_group()) - .await - .expect("the newer group takes the older one's credit") - .unwrap() - .unwrap(); - assert_eq!(group.start, 1); - - // No frame follows the reset: the group just ends. - older.close().unwrap(); - let end = kio::clock::timeout(Duration::from_secs(5), paused.read_frame()) - .await - .expect("the paused group continues"); - assert!(matches!(end, Ok(None)), "the paused group ends cleanly: {end:?}"); -} - -/// On lite-07 a paused group's continuation waiting for credit ranks below the stream it -/// lost to, so it never holds back a newer group that outranks that stream: the newer group -/// still resets it, and both paused groups then continue without loss. -#[kio::test] -async fn a_waiting_continuation_lets_a_newer_group_through() { - let version: Version = "moq-lite-07-wip".parse().unwrap(); - let publisher = origin(1); - let viewer = origin(2); - let broadcast = publisher - .route("live", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - let info = track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_max_age(None); - let track = broadcast.broadcast().create_track("0", info).unwrap(); - broadcast.announce().unwrap(); - - let mut options = MockConnectOptions::new(version); - options.transport.max_concurrent_uni_streams = Some(1); - options.server_publish = Some(publisher.consume()); - options.client_subscribe = Some(viewer.clone()); - let _pair = connect_mock(options).await; - - let remote = viewer - .consume() - .follow("live", None) - .unwrap() - .available() - .await - .unwrap() - .unwrap(); - let prefs = track::Subscription::default() - .with_order(track::Order::Descending) - .with_delay(track::Delay::default().with_max(Duration::from_secs(30))); - let mut subscriber = remote.track("0").unwrap().subscribe(prefs).await.unwrap(); - macro_rules! next { - () => { - kio::clock::timeout(Duration::from_secs(5), subscriber.recv_group()) - .await - .expect("a group arrives") - .unwrap() - .unwrap() - }; - } - - // Groups 0 and 1 stay unfinished: 1 resets 0, whose continuation then waits for credit. - let mut groups = Vec::new(); - let mut readers = Vec::new(); - for key in 0..2 { - let mut group = track.append_next().unwrap(); - group.write_frame(group.at_start(), b"a".as_slice()).unwrap(); - let reader = next!(); - assert_eq!(reader.start, key); - groups.push(group); - readers.push(reader); - } - - // The newest outranks group 1's stream, so it resets that one too. - let mut newest = track.append_next().unwrap(); - newest.write_frame(newest.at_start(), b"new".as_slice()).unwrap(); - newest.close().unwrap(); - assert_eq!(next!().start, 2, "the newest group waits behind a continuation"); - - // The paused groups continue highest-ranked first: newest first in descending order. - for (mut group, mut reader) in groups.into_iter().zip(readers).rev() { - group.write_frame(group.at_start(), b"b".as_slice()).unwrap(); - group.close().unwrap(); + .expect("the fetch is answered") + .unwrap_or_else(|err| panic!("{version} ietf: {ietf}: {err}")); let mut frames = Vec::new(); - while let Some(frame) = kio::clock::timeout(Duration::from_secs(5), reader.read_frame()) + while let Some(frame) = kio::clock::timeout(Duration::from_secs(5), fetched.read_frame()) .await - .expect("the paused group continues") - .expect("the paused group is not given up") + .expect("the fetched group arrives") + .unwrap() { frames.push(frame.payload); } - assert_eq!(frames, [b"a".as_slice(), b"b".as_slice()], "group {}", reader.start); + assert_eq!(frames, [b"a".as_slice(), b"b".as_slice()], "{version} ietf: {ietf}"); } } @@ -716,71 +590,6 @@ async fn an_ascending_backlog_waits_behind_a_waiting_group() { } } -/// On lite-07 a paused group waiting behind another paused group's continuation is still -/// given up once its publisher aborts it, rather than held until credit returns: the paused -/// groups are bounded by what the cache still holds, however long congestion lasts. -#[kio::test] -async fn a_paused_group_given_up_while_it_waits_is_dropped() { - let version: Version = "moq-lite-07-wip".parse().unwrap(); - let publisher = origin(1); - let viewer = origin(2); - let broadcast = publisher - .route("live", moq_net::origin::RouteOptions::hidden()) - .unwrap(); - let info = track::Info::default() - .with_timescale(moq_net::Timescale::MILLI) - .with_max_age(None); - let track = broadcast.broadcast().create_track("0", info).unwrap(); - broadcast.announce().unwrap(); - - let mut options = MockConnectOptions::new(version); - options.transport.max_concurrent_uni_streams = Some(1); - options.server_publish = Some(publisher.consume()); - options.client_subscribe = Some(viewer.clone()); - let _pair = connect_mock(options).await; - - let remote = viewer - .consume() - .follow("live", None) - .unwrap() - .available() - .await - .unwrap() - .unwrap(); - let prefs = track::Subscription::default() - .with_order(track::Order::Descending) - .with_delay(track::Delay::default().with_max(Duration::from_secs(30))); - let mut subscriber = remote.track("0").unwrap().subscribe(prefs).await.unwrap(); - - // Each unfinished group resets the one before: the newest holds the only credit, group - // 1's continuation waits for it, and group 0 waits among the paused behind that. - let mut groups = Vec::new(); - let mut readers = Vec::new(); - for start in 0..3 { - let mut group = track.append_next().unwrap(); - group.write_frame(group.at_start(), b"a".as_slice()).unwrap(); - let mut reader = kio::clock::timeout(Duration::from_secs(5), subscriber.recv_group()) - .await - .expect("a group arrives") - .unwrap() - .unwrap(); - assert_eq!(reader.start, start); - let first = kio::clock::timeout(Duration::from_secs(5), reader.read_frame()) - .await - .expect("its first frame arrives") - .unwrap(); - assert_eq!(first.map(|frame| frame.payload), Some(bytes::Bytes::from_static(b"a"))); - groups.push(group); - readers.push(reader); - } - - groups.remove(0).abort(moq_net::Error::Cancel).unwrap(); - let end = kio::clock::timeout(Duration::from_secs(5), readers[0].read_frame()) - .await - .expect("the paused group is dropped while the others hold the credit"); - assert!(end.is_err(), "the aborted paused group fails: {end:?}"); -} - /// A finished stream still waiting for its acknowledgement holds credit that no group can /// take over, so it doesn't count when ranking a waiting group: a late group ranked above it /// but below the stream still serving waits, and a newer group resets that stream rather than diff --git a/rs/moq-net/verify/subscribe/Continuation.cfg b/rs/moq-net/verify/subscribe/Continuation.cfg deleted file mode 100644 index 7897ec10f2f..00000000000 --- a/rs/moq-net/verify/subscribe/Continuation.cfg +++ /dev/null @@ -1,20 +0,0 @@ -\* A group of one frame, paused up to twice, without pressure: where each continuation -\* starts against the frames the subscriber holds, and every rule but StreamWins, whose -\* finding StreamWins.cfg tracks. -CONSTANTS - Groups = {0} - MaxUpdates = 2 - MaxStreams = 4 - MaxPauses = 2 - Frames = 1 - Pressure = FALSE - -SPECIFICATION Spec - -INVARIANTS - NoViolation - ServedOnce - QuietIsSettled - PausedIsOwed - OneReader - ContinuesWithinHeld diff --git a/rs/moq-net/verify/subscribe/StreamWins.cfg b/rs/moq-net/verify/subscribe/StreamWins.cfg index 48c76e3b7b6..3f73e53a54d 100644 --- a/rs/moq-net/verify/subscribe/StreamWins.cfg +++ b/rs/moq-net/verify/subscribe/StreamWins.cfg @@ -7,8 +7,6 @@ CONSTANTS Groups = {0} MaxUpdates = 4 MaxStreams = 4 - MaxPauses = 0 - Frames = 0 Pressure = FALSE SPECIFICATION Spec diff --git a/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg b/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg index 4dfd8a3c5dc..5849692dd0b 100644 --- a/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg +++ b/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg @@ -1,15 +1,12 @@ -\* One group, four SUBSCRIBE_UPDATEs, four Group Streams, and one YIELDED reset. +\* One group, four SUBSCRIBE_UPDATEs, and four Group Streams. \* Groups share only the order of the Subscribe Stream, so one group holds every race \* the model has, and a second multiplies the states without adding one. \* StreamWins lives in StreamWins.cfg, without pressure, while its finding is open. -\* Pressure lets the pool fold what each side keeps at any point. Frames are left out: -\* Continuation.cfg checks where a continuation starts. +\* Pressure lets the pool fold what each side keeps at any point. CONSTANTS Groups = {0} MaxUpdates = 4 MaxStreams = 4 - MaxPauses = 1 - Frames = 0 Pressure = TRUE SPECIFICATION Spec @@ -18,5 +15,4 @@ INVARIANTS NoViolation ServedOnce QuietIsSettled - PausedIsOwed OneReader diff --git a/rs/moq-net/verify/subscribe/SubscribeAccounting.tla b/rs/moq-net/verify/subscribe/SubscribeAccounting.tla index 29791f7929b..f41848397f2 100644 --- a/rs/moq-net/verify/subscribe/SubscribeAccounting.tla +++ b/rs/moq-net/verify/subscribe/SubscribeAccounting.tla @@ -13,25 +13,17 @@ (* Stream; each Group Stream travels on its own, unordered against them, *) (* and a reset can lose its header. *) (* *) -(* A publisher out of stream credit may reset a Group Stream with YIELDED, *) -(* which pauses the group instead of accounting for it: the subscriber *) -(* keeps the frames it read, and the publisher later continues the group *) -(* on a new Group Stream from the reset stream's own Frame Start, or names *) -(* it in a drop. A continuation may overtake the YIELDED reset and waits, *) -(* unread, for it. *) -(* *) (* Each step is one poll of the code that handles one event. The track *) (* cursor's verdicts (hand a group out, give it up stale or final, skip *) (* it) and whether the cache still holds a fresh copy on an update are *) (* free choices, standing in for the cache, the clock, and the routes. *) -(* So are when the publisher yields a stream, and whether it continues a *) -(* paused group or gives it up, standing in for stream credit and expiry. *) (* Under `Pressure`, so is when the pool's debt folds what each side keeps *) (* of the group: the publisher's record and the subscriber's tail. *) (* *) (* Scope: the subscription is already answered (SUBSCRIBE_OK) and covers *) (* every group; ranges, starts, fills, and the tail's grace are left out. *) -(* GROUP_END takeover is left out: only the YIELDED reset pauses a group. *) +(* Frames are left out: no stream continues another, so what a stream *) +(* carried never changes how its group is accounted. *) (***************************************************************************) EXTENDS Integers, Sequences, FiniteSets @@ -39,27 +31,19 @@ CONSTANTS Groups, \* group sequences in range MaxUpdates, \* SUBSCRIBE_UPDATEs the subscriber sends MaxStreams, \* Group Streams the publisher opens - MaxPauses, \* YIELDED resets the publisher sends per group - Frames, \* frames in each group: 0 leaves frames out Pressure \* the cache pool may hand the readers debt, so each side folds \* A Group Stream: the publisher's side (`GroupState`), and the subscriber's. \* pub: "open" (serving), "fin" (FIN sent, awaiting its acknowledgement), -\* "acked", or "reset" with `err` ("old", "evicted", or "yielded") +\* "acked", or "reset" with `err` ("old" or "evicted") \* sub: "none" (no header yet, or lost with a reset), "header" (filling the copy), \* "held" (waiting on the stream filling the copy), "fin", "reset", or "dup" -\* of: the stream this one continues, or 0 for a fresh one -\* start: its Frame Start; wrote: frames the publisher wrote on it; read: frames the -\* subscriber read from it -NewStream(g, of, start) == - [g |-> g, pub |-> "open", err |-> "", wasFin |-> FALSE, sub |-> "none", - of |-> of, start |-> start, wrote |-> 0, read |-> 0] +NewStream(g) == + [g |-> g, pub |-> "open", err |-> "", wasFin |-> FALSE, sub |-> "none"] DropMsg(g, err, u) == [type |-> "drop", g |-> g, err |-> err, u |-> u] SkipMsg(g) == [type |-> "skip", g |-> g, err |-> "", u |-> 0] -Max(a, b) == IF a > b THEN a ELSE b - VARIABLES \* The publisher's track cursor: its verdict on each group. cur, \* "pending", "handed", "stale", "final", or "skip" @@ -74,8 +58,6 @@ VARIABLES reoffered, \* `reoffered`: expired groups an update serves again outq, \* `GroupOutcomes::dropped`: failed streams not yet named folded, \* the record folded the group into its floor (`Record::folded`) - paused, \* the YIELDED stream a paused group waits to continue, or 0 - pauses, \* YIELDED resets sent for the group \* The wire. streams, \* every Group Stream opened, in order resp, \* the Subscribe Stream, publisher to subscriber @@ -83,22 +65,16 @@ VARIABLES \* The subscriber's `SubStream`, `Tail`, and copy. sup, \* `SubStream::update`: the latest number sent acct, \* the tail's ledger: groups accounted for - reading, \* the part of `acct` only the stream filling the copy accounts for finished, \* `Tail::finished`: groups whose stream finished since the last update forgot, \* `Tail::folded`: the span a fold forgot since the last update sstale, \* `SubStream::stale`: runs accounted by a stale drop under `sup` - sdrop, \* groups a stale drop under `sup` named, recorded or not rec, \* the copy's record: "none", "stale", "final", or "skip" - cached, \* the copy's group: "none", "open", "paused", "whole", "aborted", "evicted" - have, \* the index after the last frame the copy holds of the group + cached, \* the copy's group: "none", "open", "whole", "aborted", "evicted" got, \* history: the subscriber read the group to its FIN - malformed, \* a continuation started past the frames the copy holds violation \* the subscriber closed the session with a PROTOCOL_VIOLATION -pubVars == <> -subVars == <> +pubVars == <> +subVars == <> vars == <> Init == @@ -107,16 +83,12 @@ Init == /\ pup = 0 /\ served = {} /\ dropped = {} /\ dstale = {} /\ expired = {} /\ redrop = {} /\ reoffered = {} /\ outq = <<>> /\ folded = {} - /\ paused = [g \in Groups |-> 0] - /\ pauses = [g \in Groups |-> 0] /\ streams = <<>> /\ resp = <<>> /\ ctrl = <<>> /\ sup = 0 - /\ acct = {} /\ reading = {} /\ finished = {} /\ forgot = {} /\ sstale = {} /\ sdrop = {} + /\ acct = {} /\ finished = {} /\ forgot = {} /\ sstale = {} /\ rec = [g \in Groups |-> "none"] /\ cached = [g \in Groups |-> "none"] - /\ have = [g \in Groups |-> 0] /\ got = {} - /\ malformed = FALSE /\ violation = FALSE ----------------------------------------------------------------------------- @@ -133,7 +105,7 @@ ReplaceStale(g) == \* `serve`: open a Group Stream for g, which `mark_served` records. Serve(g) == /\ Len(streams) < MaxStreams - /\ streams' = Append(streams, NewStream(g, 0, 0)) + /\ streams' = Append(streams, NewStream(g)) /\ served' = served \cup {g} \* `poll_track` takes `Recv::Group`: the cursor hands g out. Once more, it may hand out a @@ -155,7 +127,7 @@ CursorHand(g) == THEN UNCHANGED <> ELSE /\ Serve(g) /\ UNCHANGED <> - /\ UNCHANGED <> + /\ UNCHANGED <> \* `poll_forward_drop`: the cursor gives g up stale ("old"), final ("evicted"), or \* declares it skipped, after `poll_step` named failed streams and dropped again. A later @@ -185,19 +157,11 @@ CursorGiveUp(g, kind) == /\ resp' = IF ~named THEN resp ELSE IF kind = "skip" THEN Append(resp, SkipMsg(g)) ELSE Append(resp, DropMsg(g, kind, pup)) - /\ UNCHANGED <> - -\* A serving stream writes its next frame. -StreamWriteAt(i) == - /\ streams[i].pub = "open" - /\ streams[i].start + streams[i].wrote < Frames - /\ streams' = [streams EXCEPT ![i].wrote = @ + 1] - /\ UNCHANGED <> + /\ UNCHANGED <> \* A serving stream writes its last frame and FINs (`GroupState::Closed`). StreamFinAt(i) == /\ streams[i].pub = "open" - /\ streams[i].start + streams[i].wrote = Frames /\ streams' = [streams EXCEPT ![i].pub = "fin"] /\ UNCHANGED <> @@ -210,38 +174,7 @@ StreamFailAt(i, err) == ![i].wasFin = (streams[i].pub = "fin")] /\ outq' = Append(outq, <>) /\ UNCHANGED <> - -\* Out of stream credit, the publisher resets a serving stream with YIELDED. The group is -\* paused, not given up: nothing names it, and the publisher owes a continuation or a drop. -StreamYieldAt(i) == - /\ streams[i].pub = "open" - /\ pauses[streams[i].g] < MaxPauses - /\ streams' = [streams EXCEPT ![i].pub = "reset", ![i].err = "yielded"] - /\ paused' = [paused EXCEPT ![streams[i].g] = i] - /\ pauses' = [pauses EXCEPT ![streams[i].g] = @ + 1] - /\ UNCHANGED <> - -\* The publisher continues a paused group on a new Group Stream, from the YIELDED -\* stream's own Frame Start: a reset can discard frames the transport acknowledged. -PubContinue(g) == - /\ paused[g] # 0 - /\ Len(streams) < MaxStreams - /\ LET k == paused[g] - IN streams' = Append(streams, NewStream(g, k, streams[k].start)) - /\ paused' = [paused EXCEPT ![g] = 0] - /\ UNCHANGED <> - -\* The publisher gives a paused group up instead, as it would the stream it yielded: -\* it expires stale under the delivery budget, or is evicted. -PausedGiveUp(g, err) == - /\ paused[g] # 0 - /\ paused' = [paused EXCEPT ![g] = 0] - /\ outq' = Append(outq, <>) - /\ UNCHANGED <> + folded, resp, ctrl, subVars>> \* The peer acknowledged everything up to the FIN. StreamAckAt(i) == @@ -264,8 +197,7 @@ PubOutcomes == /\ dropped' = dropped \cup {g} /\ dstale' = dstale \cup {g} ELSE UNCHANGED <> - /\ UNCHANGED <> + /\ UNCHANGED <> \* `poll_step` drops again, under the new number, what an update could not re-offer. PubRedrop == @@ -278,8 +210,7 @@ PubRedrop == /\ dropped' = dropped \cup {g} /\ dstale' = dstale \cup {g} /\ served' = served \cup {g} - /\ UNCHANGED <> + /\ UNCHANGED <> \* `poll_track` serves a re-offered group, unless a verdict since accounted for it. One \* the record folded while it waited is named in a final drop instead. @@ -293,8 +224,8 @@ PubReoffer == /\ UNCHANGED <> [] g \in served -> UNCHANGED <> [] OTHER -> Serve(g) /\ UNCHANGED resp - /\ UNCHANGED <> + /\ UNCHANGED <> \* The pool hands the reader debt and its record folds the group into its floor: a group \* handed out or named final, once nothing about it is still to tell. The fold stops at a @@ -307,12 +238,11 @@ PubFold(g) == /\ \A i \in 1..Len(outq) : outq[i][1] # g /\ folded' = folded \cup {g} /\ UNCHANGED <> + streams, resp, ctrl, subVars>> \* `TrackRun::update` applies a SUBSCRIBE_UPDATE: the cursor's stale verdicts reopen \* (`reoffer_stale`, then `owe`), and each expired stream is judged again: served \* again where the cache still holds a fresh copy (`reoffer_groups`), else dropped again. -\* A paused group is still owed its continuation or drop, so the update leaves it be. PubUpdate == /\ ctrl # <<>> /\ LET u == Head(ctrl) @@ -329,7 +259,7 @@ PubUpdate == /\ served' = served \ owed /\ dropped' = dropped \ owed /\ dstale' = dstale \ owed - /\ UNCHANGED <> + /\ UNCHANGED <> ----------------------------------------------------------------------------- (* Subscriber *) @@ -341,21 +271,13 @@ Held(g) == {i \in 1..Len(streams) : streams[i].g = g /\ streams[i].sub = "held"} \* A stream of g is still being read into the copy, even one a final drop aborted. Filling(g) == \E j \in 1..Len(streams) : streams[j].g = g /\ streams[j].sub = "header" -\* The ledger already accounts for g, not only through a stream still reading it, and the -\* next update does not take that back (`buffer_update` owes again only a stale run the -\* copy still records): a stream that finished it, a reset, a final drop, a skip, or a stale -\* drop a fold marked overtaken. A stream that starts filling the copy then adds nothing a -\* YIELDED reset of it could take back. -Settled(g) == g \in acct /\ g \notin reading /\ ~(g \in sstale /\ rec[g] = "stale") - \* The first of them, which takes the copy over once the filling stream ends. NextHeld(g) == {j \in Held(g) : \A k \in Held(g) : j <= k} \* A Group Stream's header arrives (`TrackEntry::open`, `Reading::open`): the tail \* accounts for the group. It fills the copy and replaces a stale record of it, unless \* another stream of it is still read (`Open::Duplicate`, held until that one settles) -\* or the copy holds the group whole (`Error::Duplicate`). Into a paused copy it -\* continues the group: the copy ignores the frames it holds and appends the rest. +\* or the copy holds the group whole (`Error::Duplicate`). SubHeaderAt(i) == /\ streams[i].sub = "none" /\ streams[i].pub \in {"open", "fin", "reset"} @@ -363,42 +285,21 @@ SubHeaderAt(i) == IN /\ acct' = acct \cup {g} /\ CASE Filling(g) \/ cached[g] = "open" -> /\ streams' = [streams EXCEPT ![i].sub = "held"] - /\ UNCHANGED <> + /\ UNCHANGED <> [] cached[g] = "whole" -> /\ streams' = [streams EXCEPT ![i].sub = "dup"] - /\ UNCHANGED <> - [] cached[g] = "paused" -> - /\ streams' = [streams EXCEPT ![i].sub = "header"] - /\ cached' = [cached EXCEPT ![g] = "open"] - /\ rec' = [rec EXCEPT ![g] = IF @ = "stale" THEN "none" ELSE @] - /\ malformed' = (malformed \/ streams[i].start > have[g]) - /\ reading' = IF Settled(g) THEN reading ELSE reading \cup {g} - /\ UNCHANGED have + /\ UNCHANGED <> [] OTHER -> /\ streams' = [streams EXCEPT ![i].sub = "header"] /\ cached' = [cached EXCEPT ![g] = "open"] /\ rec' = [rec EXCEPT ![g] = IF @ = "stale" THEN "none" ELSE @] - /\ have' = [have EXCEPT ![g] = streams[i].start] - /\ reading' = IF Settled(g) THEN reading ELSE reading \cup {g} - /\ UNCHANGED malformed - /\ UNCHANGED <> - -\* The stream filling the copy delivers its next frame, which may have arrived before a -\* reset that discards the rest. The copy ignores a frame it already holds. -SubFrameAt(i) == - /\ streams[i].sub = "header" - /\ streams[i].read < streams[i].wrote - /\ streams' = [streams EXCEPT ![i].read = @ + 1] - /\ have' = [have EXCEPT ![streams[i].g] = Max(@, streams[i].start + streams[i].read + 1)] - /\ UNCHANGED <> + /\ UNCHANGED <> \* The stream filling the copy is read to its FIN (`Reading::finish`); once every byte \* arrived, a reset sent after the FIN changes nothing. A held stream of the group then -\* only duplicates it, a continuation included. +\* only duplicates it. SubFinAt(i) == /\ streams[i].sub = "header" - /\ streams[i].read = streams[i].wrote /\ \/ streams[i].pub \in {"fin", "acked"} \/ streams[i].pub = "reset" /\ streams[i].wasFin /\ LET g == streams[i].g @@ -409,46 +310,22 @@ SubFinAt(i) == /\ finished' = finished \cup {g} /\ cached' = [cached EXCEPT ![g] = "whole"] /\ got' = got \cup {g} - /\ reading' = reading \ {g} - /\ UNCHANGED <> + /\ UNCHANGED <> -\* The reset of the stream filling the copy arrives. Any code but YIELDED ends the group's -\* readers with its error, and the first held stream of the group opens it again as a fresh -\* copy (`TrackEntry::reopen`). YIELDED keeps the frames read: the first held stream -\* continues the group, or with none the copy is paused, and the tail takes back what the -\* paused stream's reading accounted, unless a drop or skip under the update in effect -\* accounts for the group. A stale drop the filling stream had won over then records the -\* group stale, as it would arriving now. A YIELDED stream whose header arrived late, after -\* another stream settled the group, accounted nothing and takes nothing back. +\* The reset of the stream filling the copy arrives: it ends the group's readers with its +\* error, and the first held stream of the group opens it again as a fresh copy +\* (`TrackEntry::reopen`). SubResetAt(i) == /\ streams[i].sub = "header" /\ streams[i].pub = "reset" /\ LET g == streams[i].g - yielded == streams[i].err = "yielded" next == NextHeld(g) - n == CHOOSE j \in next : TRUE - pause == yielded /\ next = {} /\ cached[g] = "open" - verdict == g \in sdrop \/ rec[g] \in {"final", "skip"} - mine == g \in reading - restale == pause /\ mine /\ g \in sdrop /\ rec[g] = "none" IN /\ streams' = [j \in 1..Len(streams) |-> IF j = i THEN [streams[j] EXCEPT !.sub = "reset"] ELSE IF j \in next THEN [streams[j] EXCEPT !.sub = "header"] ELSE streams[j]] - /\ cached' = [cached EXCEPT ![g] = CASE next # {} -> "open" - [] pause -> "paused" - [] yielded -> @ - [] OTHER -> "aborted"] - /\ have' = IF next # {} /\ ~yielded - THEN [have EXCEPT ![g] = streams[n].start] - ELSE have - /\ malformed' = (malformed \/ (next # {} /\ yielded /\ streams[n].start > have[g])) - /\ acct' = IF pause /\ mine /\ ~verdict THEN acct \ {g} ELSE acct - /\ reading' = IF yielded /\ next # {} THEN reading ELSE reading \ {g} - /\ rec' = IF restale THEN [rec EXCEPT ![g] = "stale"] ELSE rec - /\ sstale' = IF restale THEN sstale \cup {g} ELSE sstale - /\ UNCHANGED <> + /\ cached' = [cached EXCEPT ![g] = IF next # {} THEN "open" ELSE "aborted"] + /\ UNCHANGED <> \* A held stream's own reset arrives. SubResetHeldAt(i) == @@ -461,8 +338,8 @@ SubResetHeldAt(i) == Evict(g) == /\ cached[g] = "whole" /\ cached' = [cached EXCEPT ![g] = "evicted"] - /\ UNCHANGED <> + /\ UNCHANGED <> \* The pool hands the tail debt and `Tail::rebill` folds every finished run but the newest \* into the span it forgot: one group stands for any of them, since a later group may have @@ -473,13 +350,12 @@ TailFold(g) == /\ g \notin forgot /\ finished' = finished \ {g} /\ forgot' = forgot \cup {g} - /\ UNCHANGED <> + /\ UNCHANGED <> \* `apply_response` for SUBSCRIBE_DROP. A stale drop under a replaced update is no \* verdict. Otherwise the tail accounts for the group; a stale drop records it in the \* copy unless its stream finished under this update or the copy holds it -\* (`give_up` keeps an open group, though not a paused one), and a final drop records it +\* (`give_up` keeps an open group), and a final drop records it \* and ends its readers unless the copy holds it whole (`give_up` keeps a group a finished \* copy serves, the live one or a fetched head). A fetched head still filling, which this \* model leaves out, holds the drop in its slot until it finishes ("whole" here) or dies, @@ -492,9 +368,9 @@ OnDrop(m) == stale == m.err = "old" IN IF m.u > sup THEN /\ violation' = TRUE - /\ UNCHANGED <> + /\ UNCHANGED <> ELSE IF stale /\ m.u < sup - THEN UNCHANGED <> + THEN UNCHANGED <> ELSE IF stale THEN LET recorded == g \notin finished /\ cached[g] \notin {"open", "whole"} @@ -503,13 +379,12 @@ OnDrop(m) == IN /\ acct' = acct \cup {g} /\ rec' = IF recorded THEN [rec EXCEPT ![g] = "stale"] ELSE rec /\ sstale' = IF owed THEN sstale \cup {g} ELSE sstale - /\ sdrop' = sdrop \cup {g} /\ UNCHANGED <> ELSE /\ acct' = acct \cup {g} /\ sstale' = sstale \ {g} /\ rec' = IF rec[g] = "skip" \/ cached[g] = "whole" THEN rec ELSE [rec EXCEPT ![g] = "final"] - /\ cached' = [cached EXCEPT ![g] = IF @ \in {"open", "paused"} THEN "aborted" ELSE @] - /\ UNCHANGED <> + /\ cached' = [cached EXCEPT ![g] = IF @ = "open" THEN "aborted" ELSE @] + /\ UNCHANGED violation \* `apply_response` for SUBSCRIBE_SKIP: final, overriding any drop, but a group that \* arrived keeps it (`skip_received`). @@ -518,14 +393,14 @@ OnSkip(m) == IN /\ acct' = acct \cup {g} /\ sstale' = sstale \ {g} /\ rec' = IF cached[g] \in {"open", "whole"} THEN rec ELSE [rec EXCEPT ![g] = "skip"] - /\ UNCHANGED <> + /\ UNCHANGED <> SubRecv == /\ resp # <<>> /\ resp' = Tail(resp) /\ LET m == Head(resp) IN IF m.type = "drop" THEN OnDrop(m) ELSE OnSkip(m) - /\ UNCHANGED <> + /\ UNCHANGED <> \* `buffer_update`: the copy forgets every stale record (`supersede_stale`), the tail \* owes again each stale run this subscription accounted that the copy still held, @@ -539,10 +414,9 @@ SubUpdate == /\ rec' = [g \in Groups |-> IF rec[g] = "stale" THEN "none" ELSE rec[g]] /\ acct' = acct \ (sstale \cap held) /\ sstale' = {} - /\ sdrop' = {} /\ finished' = {} /\ forgot' = {} - /\ UNCHANGED <> + /\ UNCHANGED <> ----------------------------------------------------------------------------- @@ -556,7 +430,7 @@ StreamDone(s) == Quiet == /\ resp = <<>> /\ ctrl = <<>> /\ outq = <<>> /\ redrop = {} /\ reoffered = {} - /\ \A g \in Groups : cur[g] # "pending" /\ paused[g] = 0 + /\ \A g \in Groups : cur[g] # "pending" /\ \A i \in 1..Len(streams) : StreamDone(streams[i]) \* The end: quiet, or out of Group Streams to open. @@ -565,13 +439,10 @@ Done == /\ UNCHANGED vars \* Each action over the streams, named so a trace says which one ran. -StreamWrite == \E i \in 1..Len(streams) : StreamWriteAt(i) StreamFin == \E i \in 1..Len(streams) : StreamFinAt(i) StreamFail == \E i \in 1..Len(streams), err \in {"old", "evicted"} : StreamFailAt(i, err) -StreamYield == \E i \in 1..Len(streams) : StreamYieldAt(i) StreamAck == \E i \in 1..Len(streams) : StreamAckAt(i) SubHeader == \E i \in 1..Len(streams) : SubHeaderAt(i) -SubFrame == \E i \in 1..Len(streams) : SubFrameAt(i) SubFin == \E i \in 1..Len(streams) : SubFinAt(i) SubReset == \E i \in 1..Len(streams) : SubResetAt(i) SubResetHeld == \E i \in 1..Len(streams) : SubResetHeldAt(i) @@ -579,20 +450,15 @@ SubResetHeld == \E i \in 1..Len(streams) : SubResetHeldAt(i) Next == \/ \E g \in Groups : CursorHand(g) \/ \E g \in Groups, kind \in {"old", "evicted", "skip"} : CursorGiveUp(g, kind) - \/ StreamWrite \/ StreamFin \/ StreamFail - \/ StreamYield \/ StreamAck - \/ \E g \in Groups : PubContinue(g) - \/ \E g \in Groups, err \in {"old", "evicted"} : PausedGiveUp(g, err) \/ PubOutcomes \/ PubRedrop \/ PubReoffer \/ PubUpdate \/ \E g \in Groups : PubFold(g) \/ SubHeader - \/ SubFrame \/ SubFin \/ SubReset \/ SubResetHeld @@ -614,34 +480,10 @@ QuietIsSettled == Quiet => acct = Groups \* A group read to its FIN is never recorded stale: the stream wins over the drop. StreamWins == \A g \in got : rec[g] # "stale" -\* The publisher serves a group again only to re-offer one whose stream it reset, or to -\* continue the latest stream of the group, which it reset with YIELDED. +\* The publisher serves a group again only to re-offer one whose stream it reset. ServedOnce == \A i, j \in 1..Len(streams) : - /\ (i < j /\ streams[i].g = streams[j].g) => streams[i].pub = "reset" - /\ streams[j].of = i => - /\ i < j - /\ streams[i].g = streams[j].g - /\ streams[i].err = "yielded" - /\ \A k \in i + 1..j - 1 : streams[k].g # streams[j].g - -\* A YIELDED reset alone never accounts for a group: while the publisher owes a paused -\* group its continuation or drop, the subscriber's paused copy stays owed. Only what else -\* accounts for a group excuses it: a drop or skip under the update in effect, a stream -\* read to its FIN, or one whose reset of another code arrived with its header. -PausedIsOwed == - \A g \in Groups : - (/\ cached[g] = "paused" - /\ paused[g] # 0 - /\ g \notin sdrop - /\ rec[g] \notin {"final", "skip"} - /\ g \notin got - /\ \A i \in 1..Len(streams) : - (streams[i].g = g /\ streams[i].sub = "reset") => streams[i].err = "yielded") - => g \notin acct - -\* A continuation never starts past the frames the paused copy holds. -ContinuesWithinHeld == ~malformed + (i < j /\ streams[i].g = streams[j].g) => streams[i].pub = "reset" \* The subscriber reads at most one Group Stream of a group at a time. OneReader == diff --git a/test/stream-credit/README.md b/test/stream-credit/README.md index 4dbe2fd8d5f..a1088c63667 100644 --- a/test/stream-credit/README.md +++ b/test/stream-credit/README.md @@ -3,8 +3,8 @@ A browser tab reads 300 live tracks through a Rust relay over WebTransport, three times the ~100 streams Chromium and Firefox let a server open to one session. The relay hands the streams it can open to the highest-priority -groups and, on lite-07, makes room with a YIELDED reset that pauses a -lower-priority group for a later stream to continue. +groups and makes room by resetting a lower-priority group's stream with +TOO\_FAR\_BEHIND, which gives that group up; the tab fetches it whole instead. ## Running @@ -35,10 +35,12 @@ The driver plays every other part: open: a frame at its start, one halfway through, and its end as the next group starts, 40 groups at 200 ms. The high tier starts its groups half an interval after the others, while their streams hold every credit the browser - gave, so the relay must pause a lower group (YIELDED) to make room. + gave, so the relay must reset a lower group's stream to make room. - **The reader** is a page built from `src/` with Vite, in Playwright's Chromium or Firefox. It dials the relay over WebTransport only and subscribes - to every track in three tiers of subscriber priority: + to every track in three tiers of subscriber priority. A group that stops + early for any reason but going stale (`OLD`) is fetched, as a reader that + still wants it would: | Tier | Priority | Tracks | Delay budget | | --- | --- | --- | --- | @@ -50,22 +52,21 @@ A cell passes when: - the high tier read every group whole, every frame in order; - every subscription ended cleanly within 10 s of the publisher finishing - (the relay drains the last groups in under a second on loopback); -- on lite-07, every track read its final group whole, since its readers wait - for every group the relay owes them. A lite-06 reader has no drops to wait - on, so it ends at the end it was told, and a starved track may lose the - final group still queued for credit; + (the relay drains the last groups in under a second on loopback). A + lite-07 reader waits for every group the relay owes it, so ending shows + each group it missed was named in a drop; - the tiers read frames in priority order, and the low tier lost some, so the cap bound; -- on lite-07, the relay log shows paused groups continued, the YIELDED path. +- some group stopped early, so the relay reset a stream to make room, and + every group the tab fetched came back whole. -With every tier at one priority, the high tier misses groups past its budget -and the relay continues no paused group, so the run fails. +With every tier at one priority, the high tier misses groups past its budget, +so the run fails. Both browsers mishandle a reset that lands just after its stream's data, -before the page read it, which a YIELDED reset of a freshly opened stream -does: Chromium loses the reset, leaving the stream open forever, and Firefox -reports it as a bare `TypeError` without its code. The `@moq/net` reader -copes, since a lite-07 publisher follows every reset with a continuation or a -drop, so a failure here that names a stream left open or an uncoded -`TypeError` is worth checking against those first. +before the page read it, which a reset of a freshly opened stream does: +Chromium loses the reset, leaving the stream open forever, and Firefox reports +it as a bare `TypeError` without its code. The `@moq/net` reader copes, since +the relay also names every group it resets in a drop, so a failure here that +names a stream left open or an uncoded `TypeError` is worth checking against +those first. diff --git a/test/stream-credit/driver.ts b/test/stream-credit/driver.ts index 3e867dd19ee..6fecbc0e641 100644 --- a/test/stream-credit/driver.ts +++ b/test/stream-credit/driver.ts @@ -8,8 +8,8 @@ * Each track keeps one group open at a time, the way a live video track keeps its GOP open, so * the relay wants 300 streams to the tab at once. Tiers of subscriber priority decide who gets * them. Exits 0 when the top tier received every group whole, every subscription ended cleanly - * soon after the publisher finished (on lite-07 with its final group whole), and the lower tiers - * lost frames in priority order; 1 otherwise. + * soon after the publisher finished, the lower tiers lost frames in priority order, and every + * group the relay reset to make room came back whole when the tab fetched it; 1 otherwise. * * @module */ @@ -19,7 +19,7 @@ import { parseArgs, stripVTControlCharacters } from "node:util"; import * as Moq from "@moq/net"; import { build } from "vite"; import { launch } from "../interop/clients/js/harness.ts"; -import type { Group, Plan, Report, Track } from "./src/plan.ts"; +import type { Plan, Report, Track } from "./src/plan.ts"; const { values } = parseArgs({ options: { @@ -65,7 +65,7 @@ const TIERS = [ type Tier = (typeof TIERS)[number]; // Groups per track, one every interval, each with a frame at its start and one halfway through, -// so a group is open across the whole interval and a paused one has a frame left to continue. +// so a group is open across the whole interval and one reset to make room has a frame left. const GROUPS = 40; const INTERVAL_MS = 200; // How long every subscription has to end once the publisher finishes. On loopback the relay @@ -131,7 +131,7 @@ const server = Bun.serve({ }); const browser = await launch([], engine); -// Every page log line, kept with the run: @moq/net logs each pause at debug level. +// Every page log line, kept with the run. const pageLog = Bun.file(`${out}/console.log`).writer(); const failures: string[] = []; let report: Report | undefined; @@ -155,7 +155,7 @@ try { reading.catch(() => {}); // Every track keeps one group open: the previous one finishes as the next one starts. The top // tier starts its groups half an interval after the rest, while their streams hold every - // credit, so the relay must pause a lower group to make room rather than hand out credit in + // credit, so the relay must reset a lower group to make room rather than hand out credit in // priority order as all the groups start at once. const open = new Map(); const step = (offset: boolean, half: number) => { @@ -227,7 +227,8 @@ if (report) { function judge(report: Report) { if (report.version !== version) failures.push(`the tab negotiated ${report.version}, not ${version}`); if (report.closed) failures.push(`the tab's session closed with ${report.closed}`); - const accounted = !/^moq-lite-0[0-6]$/.test(version); + // Groups that stopped early for any reason but going stale, each fetched again. + let fetched = 0; // Frames read per tier, the measure of who the streams went to: a live-edge reader's group // goes stale as its successor starts, so most of its groups end early by design. @@ -240,11 +241,9 @@ function judge(report: Report) { if (received.ended) fail(`ended with ${received.ended}`); const seen = new Set(); - let last: Group | undefined; for (const group of received.groups) { if (seen.has(group.sequence)) fail(`group ${group.sequence} handed out twice`); seen.add(group.sequence); - if (group.sequence === GROUPS - 1) last = group; // A frame read is never wrong, only missing: each one read is the next one written. const expected = [0, 1].map((frame) => payload(track, group.sequence, frame)); if (group.frames.some((frame, i) => frame !== expected[i])) @@ -253,18 +252,16 @@ function judge(report: Report) { // The top tier asks for every group, so none may stop early. if (tier.every && (group.error || group.frames.length !== expected.length)) fail(`group ${group.sequence} stopped after ${group.frames.length} frames: ${group.error}`); + // A group the relay reset to make room is still whole upstream, so a fetch returns it. + if (group.fetched === undefined) continue; + fetched++; + if (JSON.stringify(group.fetched) !== JSON.stringify(expected)) + fail(`group ${group.sequence} stopped (${group.error}) and fetched ${JSON.stringify(group.fetched)}`); } if (tier.every) { const missing = Array.from({ length: GROUPS }, (_, i) => i).filter((i) => !seen.has(i)); if (missing.length) fail(`missed groups ${missing.join(",")}`); } - // Once the publisher stops, the backlog drains: the newest group reaches every reader whole. - // Only lite-07 accounts for every group, so only there does a reader wait for one still - // queued for credit at the track's end. A lite-06 reader cannot tell a group still coming - // from one the relay shed, so it ends at the end it was told, and a starved track loses - // the final group the relay had not opened yet. - if ((accounted || tier.every) && (!last || last.error || last.frames.length !== 2)) - fail(`final group ${last ? `stopped after ${last.frames.length} frames: ${last.error}` : "never arrived"}`); } const share = TIERS.map((tier) => { @@ -281,22 +278,17 @@ function judge(report: Report) { const [high, mid, low] = share; if (!(low < 1)) failures.push("the low tier read every frame: the browser's stream cap never bound"); if (!(high >= mid && mid > low)) failures.push(`frames read did not follow priority: ${share.join(", ")}`); + // The relay makes room for a higher-priority group by resetting a lower one's stream, which + // gives that group up: the tab sees it stop early, not go stale, and fetches it. + log(`${fetched} groups stopped early and were fetched`); + if (fetched === 0) failures.push("no group stopped early: the relay never reset a stream to make room"); - // The relay's own account of its scheduling, independent of what the tab read: the tab's - // session is the last WebTransport one negotiated on this version. + // The relay's own account, independent of what the tab reported: the tab's session is a + // WebTransport one negotiated on this version. const lines = stripVTControlCharacters(readFileSync(relayLog, "utf8")).split("\n"); - const negotiated = new RegExp(`conn\\{id=(\\d+) .*negotiated version=${version} transport=webtransport`); - const id = lines.flatMap((line) => negotiated.exec(line)?.[1] ?? []).at(-1); - if (id === undefined) { + const negotiated = new RegExp(`conn\\{id=\\d+ .*negotiated version=${version} transport=webtransport`); + if (!lines.some((line) => negotiated.test(line))) failures.push(`the relay log names no WebTransport session on ${version}`); - return; - } - const ours = lines.filter((line) => line.includes(`conn{id=${id} `)); - const count = (what: string) => ours.filter((line) => line.includes(what)).length; - const continued = count("continuing paused group"); - log(`relay: ${continued} paused groups continued`); - // Lite-07's YIELDED reset pauses a group to make room, and a later stream continues it. - if (accounted && continued === 0) failures.push("the relay never continued a paused group"); } if (failures.length) { diff --git a/test/stream-credit/relay.toml b/test/stream-credit/relay.toml index da21adbd18a..39d1818a7de 100644 --- a/test/stream-credit/relay.toml +++ b/test/stream-credit/relay.toml @@ -8,7 +8,6 @@ level = "info" [listen] bind = "127.0.0.1:4480" tls.generate = ["localhost", "127.0.0.1"] -# The YIELDED reset that pauses a group is lite-07's; lite-06 resets cancel it. version = ["moq-lite-07-wip", "moq-lite-06"] [web.http] diff --git a/test/stream-credit/run.sh b/test/stream-credit/run.sh index fdd946bfc80..b4033d4edb0 100755 --- a/test/stream-credit/run.sh +++ b/test/stream-credit/run.sh @@ -79,7 +79,7 @@ if harness_probe "$URL/certificate.sha256"; then echo "error: something is already listening on 127.0.0.1:$PORT (stale relay?)" >&2 exit 1 fi -# The publisher's scheduling decisions, which show the cap bound and groups yielded. +# The publisher's scheduling decisions, kept with the run to show the cap bound. RUST_LOG="info,moq_net::lite::publisher=debug" harness_spawn relay "$HARNESS_RUN/relay.log" \ "$RELAY" "$DIR/relay.toml" --listen "127.0.0.1:$PORT" --web-http-listen "127.0.0.1:$PORT" RELAY_PID="$HARNESS_PID" diff --git a/test/stream-credit/src/main.ts b/test/stream-credit/src/main.ts index 6a9f45c9b35..6ca58a035b6 100644 --- a/test/stream-credit/src/main.ts +++ b/test/stream-credit/src/main.ts @@ -39,8 +39,9 @@ window.subscribe = async (plan) => { while (!broadcast) broadcast = await request.active.changed(); const active = broadcast; - const subscribers = plan.tracks.map((track) => - active.track(track.name).subscribe({ + const consumers = plan.tracks.map((track) => active.track(track.name)); + const subscribers = plan.tracks.map((track, i) => + consumers[i].subscribe({ priority: track.priority, delay: { max: Moq.Clock.Duration.millis(track.budget) }, }), @@ -50,7 +51,9 @@ window.subscribe = async (plan) => { const started = performance.now(); reading = (async () => { - const tracks = await Promise.all(plan.tracks.map((track, i) => read(track, subscribers[i], started))); + const tracks = await Promise.all( + plan.tracks.map((track, i) => read(track, consumers[i], subscribers[i], started)), + ); const closed = connection.closed; await connection.close(); return { version: connection.version, tracks, closed: String((await closed) ?? "") || null }; @@ -65,8 +68,15 @@ window.report = () => { window.pending = () => progress.filter((received) => received.at === undefined); // Read every group the subscription hands out until it ends. Frames are read concurrently with -// the next group, since a group paused for credit must not hold back the groups after it. -async function read(track: Track, subscriber: Moq.Track.Subscriber, started: number): Promise { +// the next group, since a group waiting on its frames must not hold back the groups after it. A +// group that ends early for any reason but going stale is fetched, as a reader that still wants +// it would: the relay reset its stream to make room, and a fetch rides a stream the page opens. +async function read( + track: Track, + consumer: Moq.Track.Consumer, + subscriber: Moq.Track.Subscriber, + started: number, +): Promise { const received: Received = { name: track.name, groups: [], ended: null }; progress.push(received); const frames: Promise[] = []; @@ -87,6 +97,8 @@ async function read(track: Track, subscriber: Moq.Track.Subscriber, started: num entry.at = performance.now() - started; } catch (err) { entry.error = String(err); + if (err instanceof Moq.Error.Stream && err.code === Moq.StreamCode.Old) return; + entry.fetched = await fetch(consumer, group.sequence); } })(), ); @@ -98,3 +110,18 @@ async function read(track: Track, subscriber: Moq.Track.Subscriber, started: num received.at = performance.now() - started; return received; } + +// Every frame of group `sequence`, fetched whole, or why the fetch failed. +async function fetch(consumer: Moq.Track.Consumer, sequence: bigint): Promise { + const frames: string[] = []; + try { + const group = await consumer.fetchGroup(sequence); + for (;;) { + const frame = await group.readString(); + if (frame === undefined) return frames; + frames.push(frame); + } + } catch (err) { + return `${frames.length} frames, then ${err}`; + } +} diff --git a/test/stream-credit/src/plan.ts b/test/stream-credit/src/plan.ts index d604befd995..9ce7e48f058 100644 --- a/test/stream-credit/src/plan.ts +++ b/test/stream-credit/src/plan.ts @@ -27,6 +27,8 @@ export interface Group { at?: number; /** Why reading its frames stopped early. */ error?: string; + /** Each frame's payload once fetched after it stopped early for any reason but going stale, or why the fetch failed. */ + fetched?: string[] | string; } /** What one subscription read. */ From 9819f87bf0400aa88fa9dc4f4ea0a068c9cb5ecf Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 06:30:30 -0800 Subject: [PATCH 3/5] verify-subscribe: sync the model to the merge Co-Authored-By: Claude Opus 5.5 --- .claude/skills/verify-subscribe/SKILL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.claude/skills/verify-subscribe/SKILL.md b/.claude/skills/verify-subscribe/SKILL.md index ecc8fe20e27..51b171d1cc9 100644 --- a/.claude/skills/verify-subscribe/SKILL.md +++ b/.claude/skills/verify-subscribe/SKILL.md @@ -31,7 +31,7 @@ Follow Reading a failure in `rs/moq-net/verify/README.md`. Replay the path on th ## Coverage -Sync commit: `SYNC_COMMIT`. Run `git log SYNC_SHORT.. -- rs/moq-net/src/lite/publisher.rs rs/moq-net/src/lite/subscriber.rs rs/moq-net/src/tail.rs rs/moq-net/src/model/track.rs rs/moq-net/src/model/record.rs` to see what changed since. Follow Coverage in `rs/moq-net/verify/README.md` after a change to a covered function. +Sync commit: `4a02338970b6364de721b64750876d7dd5a20bb1`. Run `git log 4a02338970.. -- rs/moq-net/src/lite/publisher.rs rs/moq-net/src/lite/subscriber.rs rs/moq-net/src/tail.rs rs/moq-net/src/model/track.rs rs/moq-net/src/model/record.rs` to see what changed since. Follow Coverage in `rs/moq-net/verify/README.md` after a change to a covered function. | Code | Model | | --- | --- | From ffe5ad8df9c94eb29a5ad849b6f469c37eeaefe1 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 06:36:47 -0800 Subject: [PATCH 4/5] verify-subscribe: anchor the sync commit to the PR base PRs squash, so a branch merge commit is not in main's history. Co-Authored-By: Claude Opus 5.5 --- .claude/skills/verify-subscribe/SKILL.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.claude/skills/verify-subscribe/SKILL.md b/.claude/skills/verify-subscribe/SKILL.md index 51b171d1cc9..f4d91b001ab 100644 --- a/.claude/skills/verify-subscribe/SKILL.md +++ b/.claude/skills/verify-subscribe/SKILL.md @@ -31,7 +31,7 @@ Follow Reading a failure in `rs/moq-net/verify/README.md`. Replay the path on th ## Coverage -Sync commit: `4a02338970b6364de721b64750876d7dd5a20bb1`. Run `git log 4a02338970.. -- rs/moq-net/src/lite/publisher.rs rs/moq-net/src/lite/subscriber.rs rs/moq-net/src/tail.rs rs/moq-net/src/model/track.rs rs/moq-net/src/model/record.rs` to see what changed since. Follow Coverage in `rs/moq-net/verify/README.md` after a change to a covered function. +Sync commit: `3016af623807e939c2e99571a3ec02beb2493579`. Run `git log 3016af6238.. -- rs/moq-net/src/lite/publisher.rs rs/moq-net/src/lite/subscriber.rs rs/moq-net/src/tail.rs rs/moq-net/src/model/track.rs rs/moq-net/src/model/record.rs` to see what changed since. Follow Coverage in `rs/moq-net/verify/README.md` after a change to a covered function. | Code | Model | | --- | --- | From d5b7ac6fbec320afc93dd4d0b52da866ef8936fe Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 17 Nov 2026 06:52:48 -0800 Subject: [PATCH 5/5] fix(js/net): register a group read before its duplicate wait A lite-07 Group Stream that waits on a copy of its group still arriving, as a recovery's does on the copy its subscription shares, registered its read only after the wait. A final drop that landed during the wait could not excuse it, so a stream whose reset the browser lost held the subscription's end past the grace. Register it first and keep the excuse check after the wait. Co-Authored-By: Claude Opus 5.5 --- js/net/src/lite/subscriber.test.ts | 49 ++++++++++++++++++++++++++++++ js/net/src/lite/subscriber.ts | 17 ++++++----- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/js/net/src/lite/subscriber.test.ts b/js/net/src/lite/subscriber.test.ts index 89862098ed9..732be7edb80 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -1589,6 +1589,55 @@ test.each([ } }); +// Lite-07: a Group Stream can wait unread on a copy of its group still arriving, as a recovery's does +// on the copy its subscription shares. A final drop that lands during that wait accounts for the +// group, so a stream whose reset the browser then loses holds the end at most the grace. +test("a drop during a duplicate wait lets the end give up on a stream that never ends", async () => { + const clock = new Manual(); + const installed = clock.install(); + const version = Version.DRAFT_07; + const { quic, streams, opened } = fakeSession(); + const subscriber = new Subscriber(quic, version, toHop(1n)); + const track = subscriber.consume(Path.from("room")).track("video").subscribe(); + try { + await opened(0); + await answerTrackInfo(streams[0], version, 1000); + const request = await opened(1); + await request.reading; + await respond(request, version, [{ start: new SubscribeStart([0n]) }]); + + // The copy still arriving, then a stream of the same group that waits on it. + const first = handStream(version); + first.write(await liteFrame(0, new Uint8Array([1]))); + void subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), first.reader); + const group = await track.recvGroup(); + expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); + const lost = handStream(version); + lost.write(await liteFrame(0, new Uint8Array([1]))); + void subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), lost.reader); + await drainReactions(); + + await respond(request, version, [ + { drop: new SubscribeDrop({ start: 0n, end: 1n, error: StreamCode.NotFound }) }, + { finish: new SubscribeFinish(1n) }, + ]); + await drainReactions(); + + let fin = false; + void request.finished.then(() => { + fin = true; + }); + await clock.advance(GRACE.div(2)); + expect(fin).toBe(false); + await clock.advance(GRACE); + expect(fin).toBe(true); + } finally { + track.close(); + subscriber.close(); + installed.close(); + } +}); + // Lite-07: a header that arrives after the grace gave up on its gap is covered by the tail, but // no drop accounts for it, so its stream is still delivering and holds the end until it is read. test("a stream whose gap the grace gave up on still holds the end", async () => { diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 37df2887b1c..c5afcf6aad9 100644 --- a/js/net/src/lite/subscriber.ts +++ b/js/net/src/lite/subscriber.ts @@ -2972,6 +2972,12 @@ export class Subscriber { track.abort(violation); throw violation; } + // Registered before any duplicate wait below, so a drop that lands during it finds the read. + if (hasSkip(this.version)) { + entry.reading ??= new Map(); + entry.reading.set(group.sequence, counted); + reading = true; + } // A duplicate of a copy still arriving, such as one another subscription sharing the copy // delivers, waits unread, which flow control bounds. Created once that copy ends, it is // refused as a duplicate if the copy finished and takes the group if it failed, @@ -3000,14 +3006,9 @@ export class Subscriber { if (passed.start < passed.end) this.#skipped(entry, passed.start, passed.end, `GROUP ${group.sequence}`); // Released once this copy took the group, so the failed copy's readers find it. arriving?.release(); - if (hasSkip(this.version)) { - entry.reading ??= new Map(); - entry.reading.set(group.sequence, counted); - reading = true; - // A drop that overtook the header already accounts for the group; a copy served again - // is owed whatever dropped the earlier one. - if (!owed && dropped) counted.excuse(); - } + // A drop that overtook the header already accounts for the group; a copy served again is + // owed whatever dropped the earlier one. + if (reading && !owed && dropped) counted.excuse(); const format = { scale: timescale, compress: entry.compress, lengthFirst: hasLengthFirst(this.version) }; const frames = readFrames(stream, producer, format);