diff --git a/.claude/skills/verify-subscribe/SKILL.md b/.claude/skills/verify-subscribe/SKILL.md index be5f3971b78..f4d91b001ab 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`, `src/tail.rs`) with TLA+. Use when a change touches SUBSCRIBE_DROP, SUBSCRIBE_SKIP, stale (OLD) drops, which groups a publisher serves or names once, Group Stream resets and expiry (a lite-07 stream closed when its group expires), a GROUP's Previous Start and the skips it passes over or the publisher holds back for it, YIELDED resets and continuations of a paused group, duplicate Group Streams, or the subscriber's tail, or when a drop 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`, `src/tail.rs`) with TLA+. Use when a change touches SUBSCRIBE_DROP, SUBSCRIBE_SKIP, stale (OLD) drops, which groups a publisher serves or names once, Group Stream resets, preemption, and expiry (a lite-07 stream closed when its group expires), a GROUP's Previous Start and the skips it passes over or the publisher holds back for it, or the subscriber's tail, or when a drop bug needs every ordering of the Subscribe Stream against the Group Streams checked. --- # Verify subscription accounting -`rs/moq-net/verify/subscribe/SubscribeAccounting.tla` models the groups 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, and that every drop is final on its subscription, a stale (OLD) one included: a SUBSCRIBE_UPDATE reopens nothing, so the model leaves updates out. A skip still goes out over a drop of a group never served. Drops and skips travel in order on the Subscribe Stream, each Group Stream travels on its own, and a reset can lose a stream's header. A Group Stream whose group expires stale is closed, not reset: a FIN without GROUP_END, so the subscriber keeps what arrived as a closed group and no drop names it. A stream that never opened is still given up with a drop, and one whose FIN went out is never reset for expiry. Each GROUP's Previous Start passes over the skipped run directly below it as a SUBSCRIBE_SKIP would, so the publisher holds such a skip back for the GROUP of a group it still owes, and sends it after all, ahead of the drop or joined to the skip, if it gives that group up instead. A drop of a group whose stream opened goes out after a skip of what its GROUP passed over, since the GROUP may never have arrived. 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, and is the only second Group Stream of a group. Each model 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) are free choices, and so are when the publisher yields a stream, whether it continues a paused group or gives it up, and whether the cache holds the group above a skip, which decides whether the skip is held back. With `Pressure`, so is when the cache pool's debt folds the publisher's record of a group. TLC checks every ordering under the bounds in the config. +`rs/moq-net/verify/subscribe/SubscribeAccounting.tla` models the groups 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, and that every drop is final on its subscription, a stale (OLD) one included: a SUBSCRIBE_UPDATE reopens nothing, so the model leaves updates out. A skip still goes out over a drop of a group never served. Drops and skips travel in order on the Subscribe Stream, each Group Stream travels on its own, and a reset can lose a stream's header. A Group Stream whose group expires stale is closed, not reset: a FIN without GROUP_END, so the subscriber keeps what arrived as a closed group and no drop names it. A stream that never opened is still given up with a drop, and one whose FIN went out is never reset for expiry. A stream reset while serving, evicted or preempted with TOO_FAR_BEHIND for a group that outranks it, is named in a final drop. Each GROUP's Previous Start passes over the skipped run directly below it as a SUBSCRIBE_SKIP would, so the publisher holds such a skip back for the GROUP of a group it still owes, and sends it after all, ahead of the drop or joined to the skip, if it gives that group up instead. A drop of a group whose stream opened goes out after a skip of what its GROUP passed over, since the GROUP may never have arrived. Each model 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) are free choices, and so are when a stream fails and whether the cache holds the group above a skip, which decides whether the skip is held back. With `Pressure`, so is when the cache pool's debt folds the publisher's record of a group. TLC checks every ordering under the bounds in the config, and reports a deadlock for any state short of quiet where nothing can happen. ## Rules @@ -13,21 +13,17 @@ Each rule is an INVARIANT; the harm says why it matters. | Rule | Holds when | Harm if broken | | --- | --- | --- | -| `ServedOnce` | The publisher opens a second Group Stream for a group only to continue the group's latest stream, which it reset with YIELDED. | The group's bytes go out twice, or a continuation resumes a group nobody paused. | +| `ServedOnce` | The publisher opens at most one Group Stream for a group. | The group's bytes go out twice, and the subscriber reads two streams of one group. | | `NamedOnce` | The publisher names a group in at most one drop and at most one skip, and a skip only for a group it never served. The one exception repeats a skip a GROUP passed over, once, ahead of that group's drop. | A second name contradicts a final one: the subscriber accounts the group twice or ends a reader on a group it delivered. | | `QuietIsSettled` | Once nothing is in flight and the publisher has nothing left to do, the subscriber's tail accounts for every group, a skip held back for a GROUP included. | 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, 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. | ## 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 ``` -The first checks every rule but `ContinuesWithinHeld` under `Pressure`, over two groups, 1 and 2 so a skip of 1 can be held back for 2's GROUP, six Group Streams, and two YIELDED resets per group, with frames left out: about 15.9M distinct states, four minutes. The second gives one group two frames and three YIELDED resets, without `Pressure`, so it also checks where each continuation starts and where an expired stream closes: about 493K distinct states, five seconds. Both pass. Nightly runs every config here and fails if a config with an `\* Open finding:` line passes. +It checks every rule under `Pressure` over three groups, 1 to 3, so a skip of 1 can be held back for 2's GROUP and a run of 1 and 2 for 3's: about 1.97M distinct states, ten seconds. It passes. Nightly runs every config here and fails if a config with an `\* Open finding:` line passes. ## Reading a failure @@ -35,34 +31,28 @@ Follow Reading a failure in `rs/moq-net/verify/README.md`. Replay the path on th ## Coverage -Sync commit: `79379ee3ae7a154c007322afe2c5a4178816793f`. Run `git log 79379ee3ae.. -- 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 | | --- | --- | | `rs/moq-net/src/lite/publisher.rs` `TrackRun::poll_track` (`Recv::Group`), `serve`, `mark_served`; `rs/moq-net/src/model/record.rs` `Record::told`; `rs/moq-net/src/model/track.rs` `TrackState::previous_start` | `CursorHand`, `Serve`, `SkippedBelow` | -| `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`, `report`, `hole`; `rs/moq-net/src/model/record.rs` `Record::report`, `tell`, `untold`, `Report::blocks` | `CursorGiveUp`, `dropped` | | `rs/moq-net/src/lite/publisher.rs` `TrackRun::elides`, `owes_group`, `send_elided` | `held`, the `hold` choice of `CursorGiveUp` | | `rs/moq-net/src/lite/publisher.rs` `TrackRun::poll_step` (outcomes) | `PubOutcomes`, `nahead` | -| `rs/moq-net/src/lite/publisher.rs` `GroupServe::poll_serve` (`Serve`, `Closed`), `settle_failed`, `poll_stopped`, `closes_expired`, `Ending` | `StreamFin`, `StreamClose`, `StreamFail`, `StreamAck` | +| `rs/moq-net/src/lite/publisher.rs` `GroupServe::poll_serve` (`Serve`, `Closed`), `settle_failed`, `poll_stopped`, `closes_expired`, `Ending`; `rs/moq-net/src/transport/boxed.rs` `ListedState::preempted` | `StreamFin`, `StreamFail`, `StreamAck` | | `rs/moq-net/src/model/record.rs` `Record::fold` | `PubFold`, `folded` | -| `rs/moq-net/src/lite/subscriber.rs` `TrackEntry::open` (with the passed-over skip), `reopen`, `place`; `GroupRecv::poll_serve` (the duplicate wait) | `SubHeader`, `SubFin`, `SubReset`, `SubResetHeld` | +| `rs/moq-net/src/lite/subscriber.rs` `TrackEntry::open` (with the passed-over skip), `reopen`, `place`; `GroupRecv::poll_serve` | `SubHeader`, `SubFin`, `SubReset` | | `rs/moq-net/src/lite/subscriber.rs` `ServeLoop::apply_response` (`Drop`, `Skip`) | `OnDrop`, `OnSkip`, `SubRecv` | -| `rs/moq-net/src/tail.rs` `Reading::open`, `Tail::account`, `accounts` | `acct`, `reading` | +| `rs/moq-net/src/tail.rs` `Reading::open`, `Tail::account` | `acct` | | `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 that reached a stream still being read ends its group once the YIELDED reset arrives (`TrackEntry::dropped`). The model leaves the copy paused and accounted; 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`). - The publisher serves no sequence its record already told, served or named (`Record::told`), and a skip goes out over a drop alone (`Report::blocks`), so a cursor that hands a group out again after giving it up, as after a route switch, sends nothing (`ServedOnce`, `NamedOnce`). - A skip held back for a group's GROUP goes out once that group is given up instead: ahead of its drop (`drop_range`, `forward_range`), and joined to its skip, since no GROUP passes over it then (`forward_range`). A skip of the group above that kept the run held would leave it unsent for good (`QuietIsSettled`). -- A drop of a group whose stream opened, or that waited paused, goes out after a skip of what its GROUP passed over (`GroupOutcomes::dropped`), since a reset can lose the GROUP (`QuietIsSettled`). +- A drop of a group whose stream opened goes out after a skip of what its GROUP passed over (`GroupOutcomes::dropped`), since a reset can lose the GROUP (`QuietIsSettled`). ## Not covered -The subscription starts answered and covers the groups, so SUBSCRIBE_OK, SUBSCRIBE_STARTED, ranges and Range IDs, fills (`RangeFills`) and the separate request (`Recovery`) a reader wider than a stale drop asks with, floors, SUBSCRIBE_UPDATE, and SUBSCRIBE_FINISH with `drop_unserved` are left out. The tail's grace and the ledger's folding are left out: `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, the budget a relay records a stale drop under, 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. So is how much of a group an expired stream closes on, beyond `Continuation.cfg`'s two frames, and GROUP_END's End, which only says where a whole group ends. A GROUP's Previous Start is modeled as the skipped run directly below it; one naming a known group below, or nothing, passes over nothing, and a run released by an update (`release_elided`) is left out with updates. Runs are as long as the two groups allow, so a held run never grows past one group. A pass covers two groups, six Group Streams, and two YIELDED resets per group, or one group of two frames with three YIELDED resets in `Continuation.cfg`. +The subscription starts answered and covers the groups, so SUBSCRIBE_OK, SUBSCRIBE_STARTED, ranges and Range IDs, fills (`RangeFills`) and the separate request (`Recovery`) a reader wider than a stale drop asks with, floors, SUBSCRIBE_UPDATE, and SUBSCRIBE_FINISH with `drop_unserved` are left out. The tail's grace and the ledger's folding are left out: `QuietIsSettled` asks for the publisher's answer, never the grace. So are priority and order (`Steer`), which stream a preempt picks, the Subscribe Stream's FIN and cancellation, sessions closing, versions before lite-07, relays forwarding drops downstream, the budget a relay records a stale drop under, 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, and so are frames: how much of a group an expired stream closes on, and GROUP_END's End, which only says where a whole group ends. With one Group Stream per group, the subscriber's wait for a duplicate stream (`Open::Duplicate`) is never reached. A GROUP's Previous Start is modeled as the skipped run directly below it; one naming a known group below, or nothing, passes over nothing, and a run released by an update (`release_elided`) is left out with updates. A pass covers three groups. diff --git a/doc/concept/moq-lite.md b/doc/concept/moq-lite.md index 8221336c0da..336387708f6 100644 --- a/doc/concept/moq-lite.md +++ b/doc/concept/moq-lite.md @@ -295,6 +295,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 a2e6ba3afd1..d4fa2b0046a 100644 --- a/drafts/draft-lcurley-moq-lite.md +++ b/drafts/draft-lcurley-moq-lite.md @@ -203,8 +203,8 @@ A frame is used to represent a chunk of data with an upfront size. The contents are opaque to the moq-lite layer. Frames within a Group are numbered from 0 in the order they were produced. -This index is not transmitted per frame; it is implied by position within the Group, anchored by the [GROUP](#group-message) message's `Frame Start`. -A Group Stream normally starts at frame 0. +This index is not transmitted per frame; it is implied by position within the Group. +A Group Stream starts at frame 0, and a Fetch Stream at its FETCH's `Frame Start`. Each frame carries a presentation timestamp expressed in the Track's `Timescale` (see [TRACK_INFO](#track-info)), used by the moq-lite layer for [expiration](#expiration) decisions. A Track with a `Timescale` of 0 is scale-less: its timestamps and Group starts are bare numbers in no known unit, and each frame's timestamp is its Group's start. @@ -224,7 +224,7 @@ A subscriber that has received frames 0 through `N-1` of the Group starting at ` 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 the later frames of a Group a subscription covers names it in a [SUBSCRIBE_DROP](#subscribe-drop) rather than deliver the part it holds. One that holds its earlier frames, such as a relay whose upstream closed the Group or a Group expiring as it is sent, delivers them and closes the Group Stream without a GROUP_END. A Group is the unit of decodability: its earlier Frames may carry a keyframe or compression state the later Frames need. @@ -362,8 +362,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. @@ -675,7 +673,7 @@ Drops and Groups name starts, so the subscriber judges them against its latest R The publisher never closes its side first: a subscription whose every Range has an `End` other than None and whose every Group has been delivered, skipped, or dropped stays open for an update to widen it. A subscriber fills Groups it misses by asking for them as Ranges, adding them to an open subscription with SUBSCRIBE_UPDATE or opening a bounded SUBSCRIBE when none is open, rather than with a FETCH of each Group; FETCH remains for one Group, or a slice of one. -A publisher never sends a Group twice on one subscription, except to [continue](#yield) one, so a Group the subscription already delivered or named in a SUBSCRIBE_DROP is asked for with a SUBSCRIBE of its own. +A publisher never sends a Group twice on one subscription, so a Group the subscription already delivered or named in a SUBSCRIBE_DROP is asked for with a SUBSCRIBE of its own. That includes a Group dropped with OLD that the subscriber still wants, such as a relay reader whose `Subscriber Max Age` is wider than the one the drop was judged under: no SUBSCRIBE_UPDATE reopens a drop. The subscriber closes its side (FIN) once it wants nothing more from the subscription, and the publisher then closes its side (FIN), cancelling any Group Stream still open. @@ -929,28 +927,19 @@ A Group Stream MUST start with a GROUP message and MAY be followed by any number A Group MAY contain zero FRAME messages, potentially indicating a gap in the track. 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 ends a Group Stream the way its Group ended (see [Group](#group)): with a GROUP_END and a FIN for a finished Group, with a FIN alone for a closed one, and with a reset for an aborted one. A publisher MUST NOT send a GROUP_END for a Group that has not finished at this hop, and a subscriber treats bytes after a GROUP_END as a malformed Track. -A publisher MUST NOT send two Group Streams for the same Group Start on one subscription, except to [continue](#yield) one. +A publisher MUST NOT send two Group Streams for the same Group Start on one subscription. 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. 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; since the publisher reset that stream, the subscriber MAY instead end it at once and read the continuation, ignoring the frames it holds, as a browser can lose a reset that reaches a stream before its application does. -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. @@ -2122,8 +2111,7 @@ For every start in the subscription's range, the publisher MUST send a Group Str 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 publisher that names a Group in a drop after opening its stream also names, in a SUBSCRIBE_SKIP sent before the drop, the skipped starts that stream's `Previous Start` passed over, since the GROUP may never arrive. 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 start as soon as it gives it up, without waiting for SUBSCRIBE_FINISH. ## SUBSCRIBE_SKIP {#subscribe-skip} @@ -2356,7 +2344,6 @@ GROUP Message { Subscribe ID (i) Group Start (i) Previous Start (i) - Frame Start (i) } ~~~ @@ -2374,12 +2361,6 @@ No Group has a start between the two, so the subscriber accounts for the starts A publisher knows it when the starts below this Group were declared skipped down to a Group, or a Group starts just below it. A value at or above `Group Start` plus one is a malformed Track. -**Frame Start**: -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. - ## FRAME The FRAME message is a payload within a group. @@ -2549,12 +2530,10 @@ A stream that FINs without a GROUP_END carries a closed Group, which may have he - Replaced group datagrams with datagram Tracks, a namespace apart from Tracks of Groups. A subscriber subscribes to one on the Datagrams Stream (0x9) with DATAGRAMS and DATAGRAMS_UPDATE, which carry only a priority, and the publisher answers with DATAGRAMS_INFO. A datagram carries the Track's own Sequence and a Timestamp, with no Group, Inflated Size, or Range. Datagrams no longer count toward SUBSCRIBE_DROP, and a subscriber no longer deduplicates them against Group Streams. - Added a leading Type to datagrams, 0x0 for a Track Datagram; a receiver discards one of an unknown Type. - FRAME and datagrams always carry a timestamp, a bare number on a scale-less Track. -- Replaced GROUP's `Group Sequence` with `Group Start`, and added `Previous Start`, the start of the Group before it, so a subscriber sees the gap before a Group without a SUBSCRIBE_SKIP. +- Replaced GROUP's `Group Sequence` with `Group Start`, added `Previous Start`, the start of the Group before it, so a subscriber sees the gap before a Group without a SUBSCRIBE_SKIP, and removed `Frame Start`: a subscription reads whole Groups, so every Group Stream starts at frame 0. - Added GROUP_END, a `Payload Length` of 0 followed by the Group's exclusive end: a Group or Fetch Stream ends with it when its Group finished, a FIN alone ends a closed Group whose frames may not all exist, and a reset aborts. A Group's own end bounds its reach. - Expiration also measures a group's arrival age, the time since its successor arrived at this hop. - A publisher closes the open Group Stream of a group that expires, without a GROUP_END, rather than resetting it, and a relay closes a Group it holds only the start of. -- 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. - 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 631df4c2bc1..b1c2986f661 100644 --- a/js/net/api/index.api.md +++ b/js/net/api/index.api.md @@ -649,7 +649,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/integers.test.ts b/js/net/src/integers.test.ts index 4a90e434c9a..ab324700bda 100644 --- a/js/net/src/integers.test.ts +++ b/js/net/src/integers.test.ts @@ -145,9 +145,11 @@ for (const version of [Lite.DRAFT_06, Lite.DRAFT_07]) { await Writer.collect(version, (w) => LiteMessage.encode(w, (w) => w.write(body))), version, ); - // Lite-07 carries the Previous Start ahead of the Frame Start. - const group = version === Lite.DRAFT_07 ? fields(version, 0, 0, 0, UNSAFE) : fields(version, 0, 0, UNSAFE); - await expect(LiteGroup.decode(await framed(group), version)).rejects.toThrow(RangeError); + // Only lite-06 carries a Frame Start. + if (version === Lite.DRAFT_06) + await expect(LiteGroup.decode(await framed(fields(version, 0, 0, UNSAFE)), version)).rejects.toThrow( + RangeError, + ); if (version === Lite.DRAFT_07) await expect(SubscribeStart.decode(await framed(fields(version, 1, UNSAFE)), version)).rejects.toThrow( RangeError, diff --git a/js/net/src/integration.test.ts b/js/net/src/integration.test.ts index 47241382438..8befa03d401 100644 --- a/js/net/src/integration.test.ts +++ b/js/net/src/integration.test.ts @@ -2995,10 +2995,10 @@ function qmuxPair(protocol: string, maxStreams: bigint): { client: Qmux; server: 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. @@ -3024,29 +3024,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 2c2406bef35..9b6c99cc64f 100644 --- a/js/net/src/internal.ts +++ b/js/net/src/internal.ts @@ -527,8 +527,6 @@ export interface ResumeSource { fork(options?: Subscription): Subscriber; /** 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.continueGroup}, answered by the serving copy. */ - continueGroup(sequence: bigint, from: number): GroupConsumer | undefined; /** Let go of every copy. */ close(abort?: Error): void; } @@ -803,12 +801,6 @@ export const hooks: { * subscriber's own record. Mirrors the Rust crate-private `track::Subscriber::record_mut`. */ wire: (subscriber: Subscriber) => WireRecord; - /** - * A fresh read from frame `from` of group `sequence`, if a subscriber's track still caches it from - * there, its range still admits it, and its current budget does not judge it stale, expiring - * against that budget: a lite-07 continuation of a group whose stream yielded. - */ - continueGroup: (subscriber: Subscriber, sequence: bigint, from: number) => GroupConsumer | undefined; /** 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. */ @@ -1136,9 +1128,6 @@ export const hooks: { supersedeStale: () => { throw new Error("track.ts not loaded"); }, - continueGroup: () => { - throw new Error("track.ts not loaded"); - }, deliveredRuns: () => { throw new Error("track.ts not loaded"); }, diff --git a/js/net/src/lite/connection.test.ts b/js/net/src/lite/connection.test.ts index b8b5bd77c50..4412d0194e7 100644 --- a/js/net/src/lite/connection.test.ts +++ b/js/net/src/lite/connection.test.ts @@ -957,16 +957,17 @@ for (const [alpn, version] of [ }); } -// A transport tail keeps its absolute frame numbers and the session open. A group past -// the declared end still ends the session, so a hostile peer cannot repeat that violation. +// A lite-06 transport tail keeps its absolute frame numbers and the session open (lite-07 has no +// Frame Start). A group past the declared end still ends the session, so a hostile peer cannot +// repeat that violation. for (const violation of ["a partial group", "a group past the declared end"] as const) { test( violation === "a partial group" ? "a partial group keeps its indices and the session open" : "a group past the declared end closes the session with PROTOCOL_VIOLATION", async () => { - const version = Version.DRAFT_07; - const pair = createMockTransportPair(ALPN_07_WIP); + const version = violation === "a partial group" ? Version.DRAFT_06 : Version.DRAFT_07; + const pair = createMockTransportPair(violation === "a partial group" ? ALPN_06 : ALPN_07_WIP); const connection = new Connection({ url: new URL("https://relay.example/"), quic: pair.client, version }); try { const track = wireOf(connection).consume(Path.from("room")).track("video").subscribe(); @@ -1005,8 +1006,8 @@ for (const violation of ["a partial group", "a group past the declared end"] as await group.encode(writer, version); if (violation === "a partial group") { - await writer.u53(2); // a one-byte payload's size plus one await writer.u62(2000n); // timestamp 1000, zigzag delta from 0 + await writer.u53(1); // a one-byte payload await writer.write(new Uint8Array([7])); writer.close(); const received = await track.recvGroup(); diff --git a/js/net/src/lite/copy_fill.test.ts b/js/net/src/lite/copy_fill.test.ts index 56b65e3d5c0..72d306d5489 100644 --- a/js/net/src/lite/copy_fill.test.ts +++ b/js/net/src/lite/copy_fill.test.ts @@ -377,10 +377,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); @@ -420,10 +419,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()); @@ -432,15 +431,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 a04608071e2..e5fc902ec08 100644 --- a/js/net/src/lite/group.test.ts +++ b/js/net/src/lite/group.test.ts @@ -4,7 +4,6 @@ import { Producer } from "../group.ts"; import { hooks } from "../internal.ts"; import { concat, encodeVarint, Reader, Writer } from "../stream.ts"; import { Timestamp } from "../time.ts"; -import { SEGMENT_BYTES } from "../util/limits.ts"; import * as Varint from "../varint.ts"; import { frameDecoder, Group, readFrames, type WireFrame } from "./group.ts"; import { frameHeader } from "./publisher.ts"; @@ -220,79 +219,26 @@ test("a scale-less lite-07 FRAME carries its group's start", async () => { }); // GROUP carries the previous group's start plus one on lite-07, 0 when unknown, and passes over the -// starts strictly between; earlier versions carry nothing. +// starts strictly between; earlier versions carry nothing. Only lite-06 carries a Frame Start. test("a lite-07 GROUP carries its Previous Start plus one", async () => { const encode = (previous: bigint | undefined, version: Version) => Writer.collect(version, (w) => new Group({ subscribe: 1n, sequence: 7n, previous }).encode(w, version)); - expect(Array.from(await encode(4n, Version.DRAFT_07))).toEqual([4, 1, 7, 5, 0]); - expect(Array.from(await encode(undefined, Version.DRAFT_07))).toEqual([4, 1, 7, 0, 0]); + expect(Array.from(await encode(4n, Version.DRAFT_07))).toEqual([3, 1, 7, 5]); + expect(Array.from(await encode(undefined, Version.DRAFT_07))).toEqual([3, 1, 7, 0]); expect(Array.from(await encode(4n, Version.DRAFT_06))).toEqual([3, 1, 7, 0]); + const partial = (version: Version) => + Writer.collect(version, (w) => new Group({ subscribe: 1n, sequence: 7n, frameStart: 4 }).encode(w, version)); + expect(Array.from(await partial(Version.DRAFT_06))).toEqual([3, 1, 7, 4]); + await expect(partial(Version.DRAFT_07)).rejects.toThrow(); const decode = (bytes: number[], version: Version) => Group.decode(streamOf([new Uint8Array(bytes)], version), version); - const group = await decode([4, 1, 7, 5, 0], Version.DRAFT_07); - expect([group.previous, group.passedOver()]).toEqual([4n, { start: 5n, end: 7n }]); - expect((await decode([4, 1, 7, 0, 0], Version.DRAFT_07)).passedOver()).toEqual({ start: 7n, end: 7n }); + const group = await decode([3, 1, 7, 5], Version.DRAFT_07); + expect([group.previous, group.passedOver(), group.frameStart]).toEqual([4n, { start: 5n, end: 7n }, 0]); + expect((await decode([3, 1, 7, 0], Version.DRAFT_07)).passedOver()).toEqual({ start: 7n, end: 7n }); expect((await decode([3, 1, 7, 0], Version.DRAFT_06)).previous).toBeUndefined(); }); -// 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; @@ -307,23 +253,15 @@ test("a lite-07 stream ending at the Payload Length ends the group", async () => ).rejects.toThrow("unexpected end of stream"); }); -// A peer's huge Frame Start numbers a frame past what a number counts exactly, which is refused -// as a protocol violation rather than counted wrong. Mirrors the Rust -// `group_frame_start_past_any_count_is_refused`. +// A huge frame start, such as a FETCH's, numbers a frame past what a number counts exactly, which is +// refused as a protocol violation rather than counted wrong. test("a lite-07 frame numbered past the safe integer range is refused", async () => { const version = Version.DRAFT_07; const frame = concat([frameHeader(version, 0n, undefined, 1, true), new Uint8Array([7])]); 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 42596c6b1a2..2279e84791a 100644 --- a/js/net/src/lite/group.ts +++ b/js/net/src/lite/group.ts @@ -1,15 +1,14 @@ import { race } from "@moq/signals"; import { FrameTooLarge, StreamCode, StreamError, TimestampMismatch } 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"; import { MAX_FRAME_BYTES } from "../util/limits.ts"; import * as Message from "./message.ts"; -import { hasFrameBounds, hasGroupEnd, type Version } from "./version.ts"; +import { hasGroupEnd, hasGroupFrameStart, type Version } from "./version.ts"; export class Group { subscribe: bigint; @@ -26,9 +25,8 @@ export class Group { * The index of the first frame on this stream within the group. * * 0 (the common case) means the stream carries the group from its beginning. A - * higher value means the leading frames are not here, either because the - * subscription started partway into the group or because the publisher only holds - * the tail. Lite-06+; older versions always start at 0. + * higher value means the leading frames are not here, because the subscription started + * partway into the group. Lite-06 only; other versions always start at 0. */ frameStart: number; @@ -62,7 +60,7 @@ export class Group { await w.u62(this.sequence); // Plus one, so 0 says unknown. if (hasGroupEnd(version)) await w.u62(this.previous === undefined ? 0n : this.previous + 1n); - if (hasFrameBounds(version)) { + if (hasGroupFrameStart(version)) { await w.u53(this.frameStart); } else if (this.frameStart !== 0) { // The peer would number the frames from 0 and silently misalign the group. @@ -75,7 +73,7 @@ export class Group { const sequence = await r.u62(); const plusOne = hasGroupEnd(version) ? await r.u62() : 0n; const previous = plusOne === 0n ? undefined : plusOne - 1n; - const frameStart = hasFrameBounds(version) ? await r.u53() : 0; + const frameStart = hasGroupFrameStart(version) ? await r.u53() : 0; return new Group({ subscribe, sequence, previous, frameStart }); } @@ -182,37 +180,16 @@ export function frameDecoder({ }; } -/** - * 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. * Resolves with where a lite-07 GROUP_END said the group ends, or undefined when the stream ended * without one: the caller finishes the group at that end, and a bare FIN only closes it. Anything * after GROUP_END, or an End the group refuses (at or before its start), is malformed. - * - * 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 { const decode = frameDecoder({ ...format, start: producer.sequence }); @@ -221,11 +198,8 @@ 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 undefined; if ("end" in frame) { - if (ingest?.partial) throw malformed("GROUP_END before completing the frame a continuation resumed"); if (frame.end <= producer.sequence) throw malformed(`GROUP_END at ${frame.end}, not past the group's start`); // The FIN may never come, so the group's own close, such as an abort or eviction, still @@ -235,61 +209,12 @@ export async function readFrames( if (done !== true) return undefined; return frame.end; } - 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 77ffacfc7ba..393e3637b9b 100644 --- a/js/net/src/lite/publisher.test.ts +++ b/js/net/src/lite/publisher.test.ts @@ -942,15 +942,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); @@ -962,36 +962,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 78a047a7beb..36ad0595d35 100644 --- a/js/net/src/lite/publisher.ts +++ b/js/net/src/lite/publisher.ts @@ -94,7 +94,6 @@ import { hasRoutes, hasSkip, hasTrackStream, - hasYield, keepsSubscribeOpen, resolvesStart, Version, @@ -1514,16 +1513,6 @@ export class Publisher { // skipped starts its GROUP passed over, which go out first since that GROUP may never have // arrived. const pendingDrops: { start: bigint; end: bigint; error: Error; passed: { start: bigint; end: bigint } }[] = []; - // 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 => { - // From the reset stream's Frame Start, which a cache holding only the group's tail can serve. - const resumed = hooks.continueGroup(track, serving.group.sequence, serving.start); - 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 @@ -1847,49 +1836,24 @@ export class Publisher { end: range.end, previous, }; - // 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, - passed: passedOver(serving.previous, group.sequence), - }); - 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, + passed: passedOver(options.previous, group.sequence), + }); + drops.update((revision) => revision + 1); } })(); groups.add(task); @@ -2352,8 +2316,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(); @@ -2494,9 +2456,7 @@ export class Publisher { e = failed; } 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 9a9a8afc550..732be7edb80 100644 --- a/js/net/src/lite/subscriber.test.ts +++ b/js/net/src/lite/subscriber.test.ts @@ -6,7 +6,6 @@ import type { RouteOptions } from "../announced.ts"; import type { Grant } from "../auth.ts"; import type { AuthSession } from "../auth_session.ts"; import { Viewers } from "../bandwidth.ts"; -import { DEFAULT_EXPIRY } from "../cache.ts"; import type { Probe as ProbeStats } from "../connection/stats.ts"; import { error, @@ -1444,173 +1443,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 is final on its subscription, so an update sent since does not undo it: a -// YIELDED reset of the stream it named, whether the drop overtook the reset or the stream's header, -// ends the group with OLD rather than pausing it for a continuation that never comes. +// 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([ - ["the reset", false], - ["the header", true], -])("a stale drop that overtook %s ends a yielded group after an update", async (_, beforeHeader) => { - const version = Version.DRAFT_07; + { 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 @@ -1624,30 +1463,28 @@ test.each([ await request.reading; await respond(request, version, [{ start: new SubscribeStart([0n]) }]); - const drop = () => - respond(request, version, [{ drop: new SubscribeDrop({ start: 3n, end: 4n, error: StreamCode.Old }) }]); - if (beforeHeader) await drop(); - 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])); - if (!beforeHeader) await drop(); - await drainReactions(); - track.update({ priority: 1, delay: { max: Duration.millis(60_000) } }); + 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(); - first.reset(StreamCode.Yielded); - await yielded; - - const read = await group?.readFrame().then( - () => undefined, - (err: unknown) => err, - ); - expect(read).toBeInstanceOf(StreamError); - expect((read as StreamError).code).toBe(StreamCode.Old); + expect(await group?.closed).toMatchObject({ code: StreamCode.TooFarBehind }); } finally { track.close(); subscriber.close(); @@ -1655,9 +1492,9 @@ test.each([ }); // 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)); @@ -1679,28 +1516,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(); @@ -1773,9 +1589,10 @@ test.each([ } }); -// 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 () => { +// 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; @@ -1788,30 +1605,31 @@ test("a stream whose gap the grace gave up on still holds the end", async () => const request = await opened(1); await request.reading; await respond(request, version, [{ start: new SubscribeStart([0n]) }]); - // Group 2 opens a gap at 1, which the grace gives up on by the time group 3 arrives. - for (const sequence of [0n, 2n, 3n]) { - const stream = handStream(version); - stream.finish(); - await subscriber.runGroup(new Group({ subscribe: 0n, sequence }), stream.reader); - await clock.advance(GRACE.mul(2)); - } - const late = handStream(version); - late.write(await liteFrame(0, new Uint8Array([1]))); - const running = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 1n }), late.reader); + // 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, [{ finish: new SubscribeFinish(4n) }]); + + 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.mul(3)); + await clock.advance(GRACE.div(2)); expect(fin).toBe(false); - late.finish(); - await running; - await drainReactions(); + await clock.advance(GRACE); expect(fin).toBe(true); } finally { track.close(); @@ -1820,78 +1638,48 @@ 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 () => { +// 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 () => { 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; + 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 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]) { + // Group 2 opens a gap at 1, which the grace gives up on by the time group 3 arrives. + for (const sequence of [0n, 2n, 3n]) { const stream = handStream(version); stream.finish(); await subscriber.runGroup(new Group({ subscribe: 0n, sequence }), stream.reader); + await clock.advance(GRACE.mul(2)); } - 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); + const late = handStream(version); + late.write(await liteFrame(0, new Uint8Array([1]))); + const running = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 1n }), late.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 respond(request, version, [{ finish: new SubscribeFinish(4n) }]); 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(); + + let fin = false; + void request.finished.then(() => { + fin = true; + }); + await clock.advance(GRACE.mul(3)); + expect(fin).toBe(false); + late.finish(); + await running; await drainReactions(); - expect(over).toBe(true); + expect(fin).toBe(true); } finally { - history?.close(); - live.close(); + track.close(); subscriber.close(); installed.close(); } @@ -1936,231 +1724,40 @@ test("a stream a stale drop excused stays excused after an update", async () => } }); -// Lite-07: a duplicate stream waiting out the copy it may take over holds the end, even under a -// stale drop that overtook the first copy's header: it is owed once that copy fails or pauses. -test("a duplicate stream waiting out its copy holds the end", async () => { - const clock = new Manual(); - const installed = clock.install(); +// Lite-07: a malformed FRAME on a Group Stream, such as bytes after GROUP_END, 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(); - 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]) }]); - // A stale drop overtakes the group's stream. - await respond(request, version, [{ drop: new SubscribeDrop({ start: 0n, end: 1n, error: StreamCode.Old }) }]); - await drainReactions(); - - const first = handStream(version); - first.write(await liteFrame(0, new Uint8Array([1]))); - void subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), first.reader).catch(() => {}); - const group = await track.recvGroup(); - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([1])); - - // SUBSCRIBE_FINISH starts the end's grace. The duplicate arrives under the stale drop. - await respond(request, version, [{ finish: new SubscribeFinish(1n) }]); - await drainReactions(); - const second = handStream(version); - second.write(await liteFrame(0, new Uint8Array([1]))); - const running = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), second.reader); - await drainReactions(); - - let fin = false; - void request.finished.then(() => { - fin = true; - }); - await clock.advance(GRACE.mul(3)); - expect(fin).toBe(false); - second.write(await liteFrame(10, new Uint8Array([2]))); - second.finish(); - await running; - await request.finished; - } finally { - track.close(); - subscriber.close(); - installed.close(); - } -}); - -// Lite-07: a malformed FRAME on a Group Stream ends only that stream and its group, never the -// session: bytes after GROUP_END, 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(["after GROUP_END", "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 === "after GROUP_END") { - // A zero Payload Length, the End, then a byte where only the FIN may follow. - first.write( - await Writer.collect(version, async (w) => { - await w.u53(0); - await w.u62(1000n); - await w.u53(0); - }), - ); - first.finish(); - } 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 !== "after GROUP_END") { - 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; - 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 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); - 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 }); - } finally { - track.close(); - subscriber.close(); - } -}); -// A lite-07 continuation can overtake the YIELDED 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. -test("a continuation ends the copy still arriving and continues 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(); - try { - await opened(0); - await (await opened(1)).writing; - await answerTrackInfo(streams[0], version, 1000); - - // The original copy, still arriving: one frame in, no end yet. - const chunks: Uint8Array[] = []; - const writer = new Writer( - new WritableStream({ write: (chunk) => void chunks.push(new Uint8Array(chunk)) }), - version, - ); - // A lite-07 FRAME: the size plus one, a zero timestamp delta, then the payload. - await writer.u53(2); - await writer.u62(0n); - await writer.write(new Uint8Array([7])); - let first!: ReadableStreamDefaultController; - const arriving = new ReadableStream({ - start: (controller) => { - first = controller; - }, - }); - const original = subscriber.runGroup( - new Group({ subscribe: 0n, sequence: 0n }), - new Reader(arriving, undefined, version), + const zero = new TextEncoder().encode("zero"); + const first = handStream(version); + first.write(await liteFrame(300, zero)); + // A zero Payload Length, the End, then a byte where only the FIN may follow. + first.write( + await Writer.collect(version, async (w) => { + await w.u53(0); + await w.u62(1000n); + await w.u53(0); + }), ); - for (const chunk of chunks) first.enqueue(chunk); + first.finish(); + const running = subscriber.runGroup(new Group({ subscribe: 0n, sequence: 3n }), first.reader); const group = await track.recvGroup(); - expect((await group?.readFrame())?.payload).toEqual(new Uint8Array([7])); + expect((await group?.readFrame())?.payload).toEqual(zero); - const resent: Uint8Array[] = []; - const resend = new Writer( - new WritableStream({ write: (chunk) => void resent.push(new Uint8Array(chunk)) }), - version, - ); - for (const [timestamp, payload] of [ - [0n, 7], - [2n, 8], - ] as const) { - // The size plus one first, as above. - await resend.u53(2); - await resend.u62(timestamp); - await resend.write(new Uint8Array([payload])); - } - const duplicate = new Reader( - new ReadableStream({ - start: (controller) => { - for (const chunk of resent) controller.enqueue(chunk); - controller.close(); - }, - }), - undefined, - version, - ); - const next = group?.readFrame(); - await subscriber.runGroup(new Group({ subscribe: 0n, sequence: 0n }), duplicate); - await original; - expect((await next)?.payload).toEqual(new Uint8Array([8])); - expect(await group?.readFrame()).toBeUndefined(); - expect(await group?.closed).toBeNull(); - // One that had not reached it reads the continuation whole. - const later = subscriber.consume(Path.from("room")).track("video").subscribe(); - try { - const again = await later.recvGroup(); - expect(again?.sequence).toBe(0n); - expect((await again?.readFrame())?.payload).toEqual(new Uint8Array([7])); - expect((await again?.readFrame())?.payload).toEqual(new Uint8Array([8])); - } finally { - later.close(); - } + // 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(); diff --git a/js/net/src/lite/subscriber.ts b/js/net/src/lite/subscriber.ts index 43f8791d417..c5afcf6aad9 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 { 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, type WireRoute } from "../hop.ts"; import * as Instance from "../instance.ts"; @@ -75,7 +74,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 { 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 { Routed } from "./routed.ts"; import { ProbeLevel, type Setup } from "./setup.ts"; @@ -115,7 +114,6 @@ import { hasSynced, hasTrackStream, hasTracks, - hasYield, keepsSubscribeOpen, resolvesStart, updateSupported, @@ -245,80 +243,11 @@ 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 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`. - 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. - supersede: () => void; -} - -// An already accounted header stays accounted, and `dropped` says whether a drop overtook it, -// rather than a gap the grace gave up on. -type YieldVerdict = { kind: "owed" } | { kind: "accounted"; dropped: boolean } | { kind: "dropped"; error: Error }; - -// Whether a drop accounts for the group, 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. -function excused(verdict: YieldVerdict): boolean { - switch (verdict.kind) { - case "owed": - return false; - case "accounted": - return verdict.dropped; - case "dropped": - return true; - } -} - -// The error a YIELDED reset ends the group with, since no continuation follows, or undefined when -// the group pauses. -function yieldError(verdict: YieldVerdict): Error | undefined { - switch (verdict.kind) { - case "owed": - return undefined; - case "accounted": - return new StreamError(StreamCode.Old); - 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; + // Lite-07: the hold each stream reading a group has on the subscription's end, by sequence, one + // per stream, so stream credit bounds them. 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 naming + // the group excuses it, and the end waits for the stream at most the grace. + reading?: Map; } // A subscribe stream's phases. Every SUBSCRIBE_UPDATE and our FIN go out on it. @@ -1638,7 +1567,6 @@ export class Subscriber { stream.abort(e); } finally { answering?.removeEventListener("abort", replaced); - this.#endPaused(entry); entry.tail.close(); entry.unanswered?.held.close(); entry.datagramDemand?.(); @@ -2327,7 +2255,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 { @@ -2362,7 +2290,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. @@ -2421,8 +2348,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)); @@ -2694,10 +2620,9 @@ export class Subscriber { const { start, end } = resp.drop; const error = resp.drop.cause(this.version); // Lite-07 tells a stream a drop overtook from one whose gap the grace gave up on. - if (hasYield(this.version)) entry.tail.drop(start, end); + if (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); + this.#dropped(entry, start, end); // On lite-07 an OLD drop is stale, recorded under the narrowest budget it may have been // judged under: a reader wider than that does not settle on it, and asks for the group // with a request of its own. @@ -3026,19 +2951,15 @@ export class Subscriber { ); return; } - // Lite-07: a YIELDED reset pauses the group, for a later stream to continue. - const yields = hasYield(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 drop that overtook this stream, rather than a gap the grace gave up on. const dropped = !owed && tail.dropped(group.sequence); let producer: netGroup.Producer | undefined; let arriving: Arriving | undefined; - let ingest: Ingest | undefined; - let yielding: Yielding | undefined; + // Lite-07: this stream's entry in `SubscribeEntry.reading`. + let reading = false; 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; try { // The publisher contradicted its own end, which no later group can repair. lite-05 @@ -3051,39 +2972,27 @@ 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, // 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(); 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, @@ -3097,38 +3006,13 @@ 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(); - let superseded: Promise | undefined; - if (yields) { - ingest ??= { index: group.frameStart }; - let supersede = () => {}; - superseded = new Promise((resolve) => { - supersede = resolve; - }); - yielding = { - verdict: owed ? { kind: "owed" } : { kind: "accounted", dropped }, - counted, - supersede, - }; - entry.yielding ??= new Map(); - entry.yielding.set(group.sequence, yielding); - // A drop may already account for the group. - if (excused(yielding.verdict)) 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 reading = readFrames(stream, producer, format, ingest); - const reads = entry.filling ? untilAbandoned(producer, reading) : reading; - const end = - superseded !== undefined - ? await race([reads, superseded.then((): typeof SUPERSEDED => SUPERSEDED)]) - : await reads; - if (end === 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. - stream.stop(new StreamError(StreamCode.Cancel)); - await reads.catch(() => {}); - throw new StreamError(StreamCode.Yielded, { message: "continued on a later stream" }); - } + const frames = readFrames(stream, producer, format); + const end = await (entry.filling ? untilAbandoned(producer, frames) : frames); // Whole at a GROUP_END's end, else without one: every frame sent arrived. if (end === undefined) producer.finish(); @@ -3142,117 +3026,23 @@ 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. - const given = yieldError(yielding.verdict); - 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) === counted) 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`: 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. Mirrors the Rust `TrackEntry::dropped`. - #dropPaused(entry: SubscribeEntry, start: bigint, end: bigint, error: Error): void { - for (const [sequence, yielding] of entry.yielding ?? []) { - if (sequence < start || sequence >= end) continue; - yielding.verdict = { kind: "dropped", error }; - // The drop 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. - yielding.counted.excuse(); - } - const paused = entry.paused; - if (!paused) return; - for (const [sequence, group] of paused.groups) { - if (sequence < start || sequence >= end) continue; - paused.groups.delete(sequence); - group.group.abort(error); - group.read(); + // A SUBSCRIBE_DROP named `[start, end)`: a stream still reading a group it names holds the + // subscription's end at most the grace, since a browser can lose that stream's reset. + #dropped(entry: SubscribeEntry, start: bigint, end: bigint): void { + for (const [sequence, counted] of entry.reading ?? []) { + if (sequence >= start && sequence < end) counted.excuse(); } } - // 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(); - } - paused.groups.clear(); - } - /** * Receives QUIC datagrams and routes each to its subscription's track producer (lite-05 §6.4). * @@ -3561,9 +3351,6 @@ async function groupUnwanted(group: netGroup.Producer): Promise { } } -// What a read of a Group Stream resolves to once a later stream for its group superseded it. -const SUPERSEDED: unique symbol = Symbol("superseded"); - // Like untilClosed, but also cancels once every reader has left. Demand is level-triggered, so a // caller that coalesces onto the group before the check re-arms it. async function untilAbandoned(group: netGroup.Producer, step: Promise): Promise { diff --git a/js/net/src/lite/tail.test.ts b/js/net/src/lite/tail.test.ts index 7155c73f9ae..ea34dce7004 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 ab386d9c4f9..683001cf0ed 100644 --- a/js/net/src/lite/version.ts +++ b/js/net/src/lite/version.ts @@ -234,7 +234,7 @@ export function hasRouteCost(version: Version): boolean { } } -/** Whether SUBSCRIBE, SUBSCRIBE_UPDATE, FETCH, and GROUP carry frame indices alongside +/** Whether SUBSCRIBE, SUBSCRIBE_UPDATE, and FETCH carry frame indices alongside * their group sequences, so a subscription or fetch can start and end partway through a * group. Added in lite-06. Older versions only address whole groups, so a route change * has to wait for the next group before it can resume. @@ -255,6 +255,12 @@ export function hasFrameBounds(version: Version): boolean { } } +/** Whether GROUP carries `Frame Start`, the index of the stream's first frame. Lite-06 only: + * earlier versions have no frame bounds, and a lite-07 subscription reads whole groups. */ +export function hasGroupFrameStart(version: Version): boolean { + return version === Version.DRAFT_06; +} + /** Whether SUBSCRIBE and SUBSCRIBE_UPDATE carry the subscriber's `Group Order`. Added in * lite-07; an earlier subscription is served newest first. */ export function hasOrder(version: Version): boolean { @@ -791,21 +797,3 @@ export function hasGroupEnd(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.test.ts b/js/net/src/resume.test.ts index 4f5290cbc13..cedd116bf91 100644 --- a/js/net/src/resume.test.ts +++ b/js/net/src/resume.test.ts @@ -3914,25 +3914,6 @@ test("a route whose IETF session closed before subscribing moves the track to th b.close(); }); -// A front's reader continues a yielded group its serving route's copy still caches and judges fresh, -// so a relay's publisher continues the group through it, and nothing it lacks. -test("a resumed reader continues a group its serving copy still caches", () => { - const front = new Front(); - const a = new BroadcastProducer(); - const ta = copy(a); - front.serve(a.consume()); - const sub = front.consumer.track("seq").subscribe({ delay: { max: Duration.millis(60_000) } }); - ta.write(0n); - expect(drain(sub)).toEqual([0n]); - const group = hooks.continueGroup(sub, 0n, 0); - expect(group?.sequence).toBe(0n); - group?.close(); - expect(hooks.continueGroup(sub, 1n, 0)).toBeUndefined(); - sub.close(); - front.consumer.close(); - a.close(); -}); - test("routed metadata receives a refusal without polling a group cursor", async () => { const clock = new Manual(); const installed = clock.install(); diff --git a/js/net/src/resume.ts b/js/net/src/resume.ts index 3f7c19f98b1..883b2dd73d6 100644 --- a/js/net/src/resume.ts +++ b/js/net/src/resume.ts @@ -744,16 +744,6 @@ class Reader implements ResumeSource { return holding; } - /** - * A fresh read from frame `from` of group `sequence`, if the serving copy still caches it and - * judges it fresh, handed out as any group of that copy is. - */ - continueGroup(sequence: bigint, from: number): GroupConsumer | undefined { - const serving = this.#serving(); - const group = serving && hooks.continueGroup(serving.sub, sequence, from); - return group && this.#handOut(serving, group); - } - cachedGroup(sequence: bigint, from = 0): GroupConsumer | undefined { for (let index = this.#copies.length - 1; index >= 0; index--) { const copy = this.#copies[index]; 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 0fc2927afed..5469269bf6b 100644 --- a/js/net/src/track.ts +++ b/js/net/src/track.ts @@ -3108,20 +3108,6 @@ export class Subscriber { }; } - // A fresh read from frame `from` of group `sequence`, if the cache still holds it from there and - // this cursor admits it and does not judge it stale, expiring against the budget: a lite-07 - // continuation of a group whose stream yielded. A resumed reader asks its serving route's copy. - #continueGroup(sequence: bigint, from: number): GroupConsumer | undefined { - if (this.#resume) return this.#resume.continueGroup(sequence, from); - const producer = this.#state.producer; - if (!producer) return undefined; - const cap = this.#cursor.peek().end; - const admitted = (cap === undefined || sequence < cap) && this.#window.contains(sequence); - if (!admitted || this.#staleAt(sequence, this.#drift())) return undefined; - const group = cachedMirror(producer, sequence, from); - return group && this.#guard(group); - } - private constructor(name: string, state: TrackState) { this.name = name; this.#state = state; @@ -3816,7 +3802,6 @@ export class Subscriber { }; hooks.forgetPastBudget = (subscriber, delivered) => subscriber.#forgetPastBudget(delivered); hooks.wire = (subscriber) => subscriber.#wireRecord(); - hooks.continueGroup = (subscriber, sequence, from) => subscriber.#continueGroup(sequence, from); 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 9f987c4bc6a..52c081d15ce 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 +- **lite-07 wire.** YIELDED continuations left for lite-08 + ([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)); the ietf extension costs nothing on a moq-lite track; the path escape is finished; Subscriber Min Age stays and gains its reader, reversing 2026-11-14; and diff --git a/quest/m2/lite07-finalize.md b/quest/m2/lite07-finalize.md index 45acd1f6acb..af64bbff394 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 (rejected: as it is, where every frame on a relay link carries a record). - ✅ The `\` path escape is finished rather than @@ -186,8 +185,8 @@ audited the lite-07 draft for what it could lose): - The smaller wire fixes rode [lite-07 by start](https://github.com/moq-dev/moq/pull/8908): Max Age as value plus one, no Padding stream or datagram type, a kind in the track listing, and drops as spans with no count. GROUP `Frame Start` - leaves with [YIELDED deferred](/quest/m2/yield-defer.md), since a - continuation still sets it. + left with YIELDED continuations + ([moq-dev/moq#8965](https://github.com/moq-dev/moq/pull/8965)). - Not reopened: AUTH_ERROR's retry codes (2026-11-14) and AUTH_OK's rate fields, which wait for lite-08 ([rate grant](/quest/m5/rate-grant.md)). @@ -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 - [Cache-aware multicast](/quest/m2/multicast/README.md) - lite-07 carries the redesigned route layer: routes to forwarding nodes, no holds or doubt, claims crossing trust boundaries by a chain, and no Route Key - [Routes and announces](/quest/m2/cluster-routes/README.md) - lite-07 carries the route layer: ROUTE per origin node and path-less ANNOUNCE, with the native hop list gone (only the temporary lite-06 compatibility chain remains) - [@moq/net by start](/quest/m2/time-addressing/js.md) - JS speaks the same lite-07 wire diff --git a/quest/m2/yield-defer.md b/quest/m2/yield-defer.md deleted file mode 100644 index df36175ebd3..00000000000 --- a/quest/m2/yield-defer.md +++ /dev/null @@ -1,103 +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 rules for - reopened OLD drops left with [time addressing](https://github.com/moq-dev/moq/pull/8908). - -What goes: - -- Draft: the Yield section, the YIELDED row (0x3C), GROUP's `Frame Start` - (a continuation is its last sender on a subscription; lite-06 keeps its - field), 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`: [time addressing](https://github.com/moq-dev/moq/pull/8908) re-derived the - model around final drops, expiry closes, and `Previous Start`; remove its - YIELDED half (`Continuation.cfg`, the paused rules, and the continuation - steps) and rerun the rest. - -Guidance for the PR: - -- 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 - -- [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..05a339ada47 --- /dev/null +++ b/quest/m3/fetch-preempted-group.md @@ -0,0 +1,55 @@ +# [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. Since [moq-dev/moq#8908](https://github.com/moq-dev/moq/pull/8908) +a lite-07 subscriber already asks again, on a request of its own, for an OLD +drop a wider reader still wants (`stale_budget` in +`rs/moq-net/src/lite/subscriber.rs`); a drop to make room can take the same +path. 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 acf020f92c1..808caa27b56 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 @@ -123,10 +123,12 @@ track's. `GroupRecv` rebinds the stream at each GROUP message. Public API: `StreamError::Yielded`, or whatever replaces it, returns to Rust and `@moq/net`. Wire: lite-08 paused groups, continuations, a takeover -marker, and multi-group streams. +marker, and multi-group streams. lite-07's GROUP has no `Frame Start` (only +lite-06 carries one), so a continuation that starts partway through its group +brings one back, or names its start some other way. ## 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 - [Finalize lite-07](/quest/m2/lite07-finalize.md) - freezes lite-07 without takeover or continuations diff --git a/quest/m4/browser-reset-reports.md b/quest/m4/browser-reset-reports.md index 09959e27c52..3acadce0e93 100644 --- a/quest/m4/browser-reset-reports.md +++ b/quest/m4/browser-reset-reports.md @@ -12,7 +12,8 @@ browser fix make those paths dead code. ## Plan Found by [moq-dev/moq#8673](https://github.com/moq-dev/moq/pull/8673) with -`just test stream-credit`, whose YIELDED resets of just-opened streams hit +`just test stream-credit`, whose preempting resets of just-opened streams +(YIELDED then, TOO_FAR_BEHIND since #8965) hit both bugs within a few milliseconds of the stream's data. - Chromium 156: the network service accepts the stream and replies to Blink's 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/fixtures/lite.json b/rs/moq-net/fixtures/lite.json index c4c3e867784..4e8a981d3c0 100644 --- a/rs/moq-net/fixtures/lite.json +++ b/rs/moq-net/fixtures/lite.json @@ -320,14 +320,21 @@ "name": "a group sequence at 2^64-1, the largest a u64 holds", "version": "moq-lite-07-wip", "message": "group", - "hex": "0c00ffffffffffffffffff0000", + "hex": "0b00ffffffffffffffffff00", "expect": "decoded" }, + { + "name": "a lite-07 group carrying a Frame Start, which only lite-06 has", + "version": "moq-lite-07-wip", + "message": "group", + "hex": "0c00ffffffffffffffffff0000", + "expect": "refused" + }, { "name": "a group naming the start just below it as the previous one, carried plus one", "version": "moq-lite-07-wip", "message": "group", - "hex": "1400ffffffffffffffffffffffffffffffffffff00", + "hex": "1300ffffffffffffffffffffffffffffffffffff", "expect": "decoded" }, { diff --git a/rs/moq-net/src/coding/writer.rs b/rs/moq-net/src/coding/writer.rs index c6dfc77fde1..fe75fea5c76 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 50a03462881..b26d2ff8b6f 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/group.rs b/rs/moq-net/src/lite/group.rs index c62a8771dc4..fb13df29b1b 100644 --- a/rs/moq-net/src/lite/group.rs +++ b/rs/moq-net/src/lite/group.rs @@ -17,15 +17,15 @@ pub struct Group { // The index of the first frame on this stream. 0 (the common case) means the stream // carries the group from its beginning; a higher value means the leading frames are - // missing, either because the subscription started partway in or because the - // publisher only holds the tail. Lite06+ only; older versions always start at 0. + // missing, because the subscription started partway in. Lite-06 only; other versions + // always start at 0. pub frame_start: u64, } impl Group { - /// The varints the newest layout carries: Subscribe ID, Group Start, Previous Start and - /// Frame Start. - const FIELDS: usize = 4; + /// The varints the longest layout carries: Subscribe ID, Group Start, and lite-07's + /// Previous Start or lite-06's Frame Start. + const FIELDS: usize = 3; /// The starts this header passes over as skipped; see [`passed_over`]. pub(crate) fn passed_over(&self) -> std::ops::Range { @@ -56,7 +56,7 @@ impl<'a> Message<'a> for Group { true => r.varint()?.checked_sub(1), false => None, }; - let frame_start = match version.has_frame_bounds() { + let frame_start = match version.has_group_frame_start() { true => r.varint()?, false => 0, }; @@ -80,7 +80,7 @@ impl<'a> Message<'a> for Group { w.varint(previous)?; } - if version.has_frame_bounds() { + if version.has_group_frame_start() { w.varint(self.frame_start)?; } else if self.frame_start != 0 { // The peer would number the frames from 0 and silently misalign the group. @@ -112,20 +112,23 @@ mod test { assert_eq!((got.key, got.frame_start), (7, 4)); } - /// An older peer would number the frames from 0 and silently misalign the group. + /// A peer of another version would number the frames from 0 and silently misalign the group. #[test] - fn frame_start_rejected_before_lite06() { + fn frame_start_rejected_off_lite06() { let msg = Group { subscribe: 1, key: 7, previous: None, frame_start: 4, }; - let mut buf = Vec::new(); - assert!( - msg.encode_msg(&mut Encoder::new(&mut buf, Version::Lite05.into()), Version::Lite05) - .is_err() - ); + for version in [Version::Lite05, Version::Lite07] { + let mut buf = Vec::new(); + assert!( + msg.encode_msg(&mut Encoder::new(&mut buf, version.into()), version) + .is_err(), + "{version:?}" + ); + } let whole = Group { frame_start: 0, ..msg }; let mut buf = Vec::new(); @@ -153,23 +156,23 @@ mod test { .unwrap(); buf }; - assert_eq!(encode(Some(4), Version::Lite07), [1, 7, 5, 0]); - assert_eq!(encode(None, Version::Lite07), [1, 7, 0, 0]); + assert_eq!(encode(Some(4), Version::Lite07), [1, 7, 5]); + assert_eq!(encode(None, Version::Lite07), [1, 7, 0]); assert_eq!(encode(Some(4), Version::Lite06), [1, 7, 0]); let decode = |wire: &[u8], version| { crate::coding::decode_buf(&mut &wire[..], version, |r, v| Group::decode_msg(r, v)).unwrap() }; - let got = decode(&[1, 7, 5, 0], Version::Lite07); + let got = decode(&[1, 7, 5], Version::Lite07); assert_eq!((got.previous, got.passed_over()), (Some(4), 5..7)); - let got = decode(&[1, 7, 7, 0], Version::Lite07); + let got = decode(&[1, 7, 7], Version::Lite07); assert_eq!((got.previous, got.passed_over()), (Some(6), 7..7)); - let got = decode(&[1, 7, 0, 0], Version::Lite07); + let got = decode(&[1, 7, 0], Version::Lite07); assert_eq!((got.previous, got.passed_over()), (None, 7..7)); assert_eq!(decode(&[1, 7, 0], Version::Lite06).previous, None); // One at or above the group's own start passes over nothing: the receiver refuses it. - let got = decode(&[1, 7, 8, 0], Version::Lite07); + let got = decode(&[1, 7, 8], Version::Lite07); assert_eq!((got.previous, got.passed_over()), (Some(7), 7..7)); } @@ -192,12 +195,12 @@ mod test { Form::Quic => (crate::coding::varint::MAX_QUIC, 8), Form::LeadingOnes => (u64::MAX, crate::coding::varint::MAX_LEN), }; - let fields = 2 + usize::from(version.has_frame_bounds()) + usize::from(version.has_group_end()); + let fields = 2 + usize::from(version.has_group_frame_start()) + usize::from(version.has_group_end()); let msg = Group { subscribe: max, key: max, previous: version.has_group_end().then_some(max - 1), - frame_start: if version.has_frame_bounds() { max } else { 0 }, + frame_start: if version.has_group_frame_start() { max } else { 0 }, }; let mut body = Vec::new(); @@ -218,13 +221,13 @@ mod test { ); } - assert_eq!(::MAX_SIZE, 36); + assert_eq!(::MAX_SIZE, 27); let over = ::MAX_SIZE + 1; let mut wire = vec![over as u8]; wire.resize(1 + over, 0); assert!(matches!( Group::decode_slice(&wire, Version::Lite07), - Err(DecodeError::MessageTooLarge { size: 37, max: 36 }) + Err(DecodeError::MessageTooLarge { size: 28, max: 27 }) )); } } diff --git a/rs/moq-net/src/lite/mod.rs b/rs/moq-net/src/lite/mod.rs index e4661a6f936..5cecef2195a 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 rekey; mod replay; diff --git a/rs/moq-net/src/lite/publisher.rs b/rs/moq-net/src/lite/publisher.rs index afb166fb7cb..c483653976a 100644 --- a/rs/moq-net/src/lite/publisher.rs +++ b/rs/moq-net/src/lite/publisher.rs @@ -4767,8 +4767,7 @@ struct Subscription { /// Outcomes shared by the subscription and every group it serves. outcomes: Arc, /// Whether the session negotiated the `ietf` extension and a moq-transport publisher made - /// the track, so its group streams carry object records, which a continuation cannot - /// resume. + /// the track, so its group streams carry object records. object_records: bool, } @@ -4784,8 +4783,7 @@ struct GroupContext { /// Outcomes shared by the subscription and every group it serves. outcomes: Arc, /// Whether the session negotiated the `ietf` extension and a moq-transport publisher made - /// the track, so its group streams carry object records, which a continuation cannot - /// resume. + /// the track, so its group streams carry object records. object_records: bool, } @@ -4810,42 +4808,6 @@ struct GroupOutcomes { /// Groups whose stream failed, with why, for the control stream to name, each with the /// skipped starts its GROUP's Previous Start passed over (see [`TrackRun::elided`]). 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, - /// The previous group's start its GROUP carried, which the continuation's carries too. - previous: Option, -} - -/// 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 { @@ -4961,16 +4923,6 @@ struct TrackRun { refused: Option, // Where delivery starts, which SUBSCRIBE_START reports. start: Start, - // Lite-07: the paused groups by start, each continued on its own stream ahead of 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 @@ -5050,9 +5002,6 @@ impl TrackRun { cap, steer_tx, start, - paused: Default::default(), - paused_tasks: kio::Tasks::new(), - continuing: false, end_sent: false, held_end: None, opening: None, @@ -5311,7 +5260,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), @@ -5332,15 +5280,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() { // Each was served, so its record says so already, and the drop is final: nothing @@ -5409,29 +5348,9 @@ impl TrackRun { return Poll::Ready(Ok(ControlFlow::Continue(()))); } - // A paused group takes the next stream credit, ahead of 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. + // One cursor drives the whole subscription: poll the cap-aware arrival-order group. 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, - previous, - } = 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, previous); - self.continuing = true; - return Poll::Ready(Ok(ControlFlow::Continue(()))); - } - - // One cursor drives the whole subscription: poll the cap-aware arrival-order group. 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 @@ -5501,7 +5420,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; } @@ -5934,25 +5853,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. 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, - previous: waiting.previous, - }); - } - 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 @@ -5969,52 +5878,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| { - let start = paused.group.start; - dropped.push(( - start, - StreamError::from(&err), - lite::passed_over(paused.previous, start), - )); - }); - 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`, its GROUP naming `previous` /// as the group before it, which waits for stream credit before the cursor hands out /// another. @@ -6172,59 +6035,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, - previous: None, - }); - 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. @@ -6316,12 +6126,6 @@ struct GroupServe { previous: Option, /// 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. @@ -6401,10 +6205,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(), @@ -6455,14 +6256,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) => { @@ -6778,18 +6574,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)); } @@ -6838,35 +6627,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, - previous: self.previous, - }; - self.ctx.outcomes.paused.lock().unwrap().push(paused); - return; - } self.ctx.outcomes.dropped.lock().unwrap().push(( self.key, StreamError::from(err), @@ -7277,7 +7043,7 @@ mod serve_group_test { let (bytes, fin) = first_stream(&log); assert!(fin, "no FIN"); // Only the GROUP: the write blocked before the first frame, and none goes out after. - assert_eq!(bytes, [0, 4, 0, 0, 0, 0]); + assert_eq!(bytes, [0, 3, 0, 0, 0]); assert!(outcomes.dropped.lock().unwrap().is_empty(), "dropped"); } @@ -12756,64 +12522,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, - previous: None, - }); - 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.finish().unwrap(); - run.pause(Paused { - group: empty.consume(), - frame_start: 0, - previous: None, - }); - 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 3b085c074fe..a78f4f23b72 100644 --- a/rs/moq-net/src/lite/subscriber.rs +++ b/rs/moq-net/src/lite/subscriber.rs @@ -182,19 +182,10 @@ 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 - // 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. + /// The group streams that arrived before the copy, while any wait. Parked groups hold + /// group writers, which reach the whole track model: the cell proves `Send` once here, so + /// the session driver's proof stays within the default recursion limit. 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. @@ -240,59 +231,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, -} - -/// What a YIELDED reset of the stream leaves of its group: still owed a continuation, or -/// accounted already, by a drop that overtook the header or one that named the group since, -/// which ends it. Every drop is final, a stale one included. -enum YieldVerdict { - Owed, - Accounted, - Dropped(Error), -} - -impl YieldVerdict { - fn error(self) -> Option { - match self { - Self::Owed => None, - Self::Accounted => Some(Error::Stream(crate::StreamError::Old)), - Self::Dropped(error) => Some(error), - } - } -} - /// What a group stream's header opened in its subscription's copy. enum Open { Group(Opened), @@ -302,24 +240,17 @@ 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, } -/// A group stream duplicating a copy still arriving, such as a lite-07 continuation that -/// overtook the YIELDED reset of the stream it continues, or another subscription's stream +/// A group stream duplicating a copy still arriving, such as another subscription's stream /// feeding the same copy. It waits unread, which its stream credit bounds, and is discarded /// if that copy finishes or takes the group if it fails. 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, @@ -327,13 +258,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 { @@ -372,9 +296,6 @@ impl TrackEntry { Self { copy, parked: None, - paused: None, - yielding: BTreeMap::new(), - yield_streams: 0, tail, start: None, first: None, @@ -463,10 +384,6 @@ 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); }; @@ -493,8 +410,6 @@ impl TrackEntry { ); } } - // 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 @@ -520,141 +435,21 @@ impl TrackEntry { } } } - let verdict = match owed { - true => YieldVerdict::Owed, - false => YieldVerdict::Accounted, - }; - self.place(hdr, version, received, reading, verdict) + self.place(hdr, version, received, reading) } - /// Whether the track carries moq-transport object records, which a continuation cannot resume. + /// Whether the track carries moq-transport object records. 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. - fn end_yielding(&mut self, key: u64, stream: u64) -> Option { - match self.yielding.get(&key) { - Some(yielding) if yielding.stream == stream => self.yielding.remove(&key), - _ => None, - } - } - - /// A SUBSCRIBE_DROP named `groups` with `err`: 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. - fn dropped(&mut self, groups: std::ops::Range, err: &Error) { - for (_, yielding) in self.yielding.range_mut(groups.clone()) { - yielding.verdict = YieldVerdict::Dropped(err.clone()); - } - 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() - } - /// Open the group of a held stream again, once the copy it waited on failed. fn reopen(&self, hdr: &lite::Group, version: Version, reading: Reading) -> Result { let Some(Accepted { producer, .. }) = &self.copy else { 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. @@ -664,7 +459,6 @@ impl TrackEntry { version: Version, received: Result, reading: Reading, - verdict: YieldVerdict, ) -> Result { let Some(Accepted { producer, framing }) = &self.copy else { return Err(Error::Cancel); @@ -674,7 +468,6 @@ impl TrackEntry { Ok(track::Received::Held(arriving)) => { return Ok(Open::Duplicate(Duplicate { arriving, - paused: None, track: producer.clone(), reading, })); @@ -697,8 +490,6 @@ impl TrackEntry { }; Ok(Open::Group(Opened { group: crate::recv::Group::new(group), - partial: None, - verdict, track: producer.clone(), framing: *framing, reading, @@ -1711,7 +1502,7 @@ 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. + // Under the `ietf` extension, a track a moq-transport publisher made carries object records. let records: bool = ietf && self .subscriber @@ -1719,7 +1510,6 @@ impl GroupRecv { .lock() .get(&hdr.subscribe) .is_some_and(|entry| entry.has_records()); - let yields: bool = self.subscriber.version.has_yield() && !records; if let (Err(err), Some((_, ticket))) = (&accepted, &parked) && let Some(entry) = self.subscriber.subscribes.lock().get_mut(&hdr.subscribe) { @@ -1741,7 +1531,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)), @@ -1752,25 +1542,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)); @@ -1797,21 +1568,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)); @@ -1823,8 +1583,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, records, hdr.frame_start).yielding(yields, cut.map(|cut| *cut)); + let mut ingest: FrameIngest = FrameIngest::new(framing, records, hdr.frame_start); let reading: Reading = reading; // Filling the group, bailing if the track or group dies first. The track or group @@ -1839,13 +1598,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 { // Whole at a GROUP_END's end, else closed: every frame sent arrived. Ok(end) => group.end(end), @@ -1853,33 +1605,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()); @@ -1908,14 +1633,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. @@ -1967,11 +1684,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 }, } @@ -1989,57 +1701,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, whole at `end` after a GROUP_END - /// or closed without one, unless a continuation ended before completing the frame its - /// group held cut short. - fn end(&self, end: Option) -> Result, Error> { - match self.partial { - Some(_) => Err(Error::ProtocolViolation), - None => Ok(end), - } - } - - /// 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 }), } } @@ -2073,10 +1734,7 @@ impl FrameIngest { Some(records) => { let (id, block) = records.pending.take().expect("a record precedes each frame"); let lite::ietf::Decoded { properties, gap } = lite::ietf::decode_properties(&block, self.framing)?; - // 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); } @@ -2139,7 +1797,7 @@ impl FrameIngest { } IngestPhase::Object => { let Some(code) = ready!(reader.poll_varint_maybe(waiter))? else { - return Poll::Ready(self.end(None)); + return Poll::Ready(Ok(None)); }; let records = self.records.as_mut().expect("the Group State came first"); let (id, end) = lite::ietf::decode_object(records.prior, code)?; @@ -2186,11 +1844,11 @@ impl FrameIngest { if ready!(reader.poll_has_more(waiter))? { return Poll::Ready(Err(Error::ProtocolViolation)); } - return Poll::Ready(self.end(None)); + return Poll::Ready(Ok(None)); } IngestPhase::Length => { let Some(length) = ready!(self.poll_header_varint(reader, true, waiter))? else { - return Poll::Ready(self.end(None)); + return Poll::Ready(Ok(None)); }; // 0 is GROUP_END, where a frame would start. Under object records a record // precedes every FRAME, and the group's end is the stream's. @@ -2212,7 +1870,7 @@ impl FrameIngest { if ready!(reader.poll_has_more(waiter))? { return Poll::Ready(Err(Error::MalformedTrack)); } - return Poll::Ready(self.end(Some(end))); + return Poll::Ready(Ok(Some(end))); } IngestPhase::Timing { size } => { let size = *size; @@ -2222,7 +1880,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(None)); + return Poll::Ready(Ok(None)); }; let next: u64 = (self.prev_ts as i128 + crate::coding::varint::unzigzag(zz) as i128) .try_into() @@ -2246,7 +1904,7 @@ impl FrameIngest { true => { let boundary = size.is_none() && !self.framing.stamped(); let Some(inflated) = ready!(self.poll_header_varint(reader, boundary, waiter))? else { - return Poll::Ready(self.end(None)); + return Poll::Ready(Ok(None)); }; Some(inflated) } @@ -2255,7 +1913,7 @@ impl FrameIngest { self.phase = match size { Some(size) => { let header = self.header(group, timestamp, inflated, size)?; - self.dispatch(group, header)? + IngestPhase::Admit { header } } None => IngestPhase::Size { timestamp, inflated }, }; @@ -2264,10 +1922,10 @@ impl FrameIngest { let (timestamp, inflated) = (*timestamp, *inflated); let boundary = !self.framing.stamped() && !group.compressed(); let Some(size) = ready!(self.poll_header_varint(reader, boundary, waiter))? else { - return Poll::Ready(self.end(None)); + return Poll::Ready(Ok(None)); }; let header = self.header(group, timestamp, inflated, size)?; - self.phase = self.dispatch(group, header)?; + self.phase = IngestPhase::Admit { header }; } IngestPhase::Admit { header } => { ready!(reader.poll_admit(waiter, group, header))?; @@ -2282,19 +1940,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(); @@ -2304,12 +1949,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`. @@ -2326,23 +1965,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). /// @@ -5609,73 +5231,52 @@ 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, - }; - // A moq-lite track carries no records, so a relay link that negotiated the `ietf` - // extension sends the same stream. - let header = || { + 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, previous: None, frame_start: 0, } - .encode(&mut crate::coding::Encoder::new(&mut script, version.into()), version) + .encode(&mut encoder, version) .unwrap(); - 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, delta: i64, payload: &[u8], sent: usize| { - 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(); - frame(&mut cut, frames[0].0, frames[0].1, frames[0].1.len()); - frame(&mut cut, frames[1].0, frames[1].1, 3); - let mut whole = header(); - for (delta, payload) in frames { - frame(&mut whole, 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_node: None, cost: None, going_away: Default::default(), @@ -5696,321 +5297,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, - previous: None, - 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_node: 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, - previous: None, - 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_node: 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)); - 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 header of the stream it names is final, so a - /// YIELDED reset of that stream ends the group as stale rather than pausing it. - #[kio::test] - async fn a_yielded_group_ends_on_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, - previous: None, - 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_node: 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(); - 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" - ); } /// A SUBSCRIBE_FINISH below a group already received contradicts that group. lite-05 @@ -11034,75 +10344,13 @@ 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. - #[kio::test] - async fn group_frame_start_past_any_count_is_refused() { - use crate::transport::poll::Session as _; - let version = Version::Lite07; - let session = crate::lite::test_transport::ScriptedSession::eof(Vec::new()); - let subscriber = Subscriber::new(SubscriberConfig { - runtime: crate::time::sim(), - session: crate::driver::Transport::new(session), - 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_node: 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(), - ); - subscriber.subscribes.lock().insert( - 7, - TrackEntry::accepted( - track.clone(), - lite::Framing { - timescale: Some(Timescale::MILLI), - ietf: None, - compressed: false, - length_first: false, - }, - Default::default(), - ), - ); - // The model takes the first frame at the last countable index, so the second frame's - // index is the one that cannot be counted. - let mut script = Vec::new(); - let mut encoder = crate::coding::Encoder::new(&mut script, version.into()); - lite::Group { - subscribe: 7, - key: 0, - previous: None, - frame_start: u64::MAX - 1, - } - .encode(&mut encoder, version) - .unwrap(); - for _ in 0..2 { - encoder.varint(crate::coding::varint::zigzag(0)).unwrap(); - encoder.bytes(b"x").unwrap(); - } - let incoming = crate::lite::test_transport::ScriptedSession::eof(script); - let (_, recv) = incoming.open_bi(Default::default()).await.unwrap(); - let mut group = GroupRecv::new( - 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:?}"); - } /// Distinct live GROUP starts are copies of the same group, including while /// readers retain every earlier tail. Exercise the real header/ingest path. #[kio::test] async fn live_group_tails_share_content_held_by_earlier_readers() { use crate::transport::poll::Session as _; - let version = Version::Lite07; + let version = Version::Lite06; let session = crate::lite::test_transport::ScriptedSession::eof(Vec::new()); let subscriber = Subscriber::new(SubscriberConfig { runtime: crate::time::sim(), @@ -13755,10 +13003,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())); - } match serve.subscriber.version.has_skip() && error == crate::StreamError::Old { // Lite-07 judged it stale, recorded under the narrowest budget it may have been // judged under. A reader no wider settles on it; a wider one asks for the group diff --git a/rs/moq-net/src/lite/version.rs b/rs/moq-net/src/lite/version.rs index 5a03d90c538..9db7519e448 100644 --- a/rs/moq-net/src/lite/version.rs +++ b/rs/moq-net/src/lite/version.rs @@ -166,6 +166,12 @@ impl Version { ) } + /// Whether GROUP carries `Frame Start`, the index of the stream's first frame. Lite-06 only: + /// earlier versions have no frame bounds, and a lite-07 subscription reads whole groups. + pub(crate) fn has_group_frame_start(self) -> bool { + matches!(self, Self::Lite06) + } + /// Whether SUBSCRIBE and SUBSCRIBE_UPDATE carry the subscriber's `Group Order`. /// Added in lite-07; an earlier subscription is served newest first. pub(crate) fn has_order(self) -> bool { @@ -309,17 +315,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 d93b69a8cbd..585ecf92979 100644 --- a/rs/moq-net/src/model/group.rs +++ b/rs/moq-net/src/model/group.rs @@ -3891,19 +3891,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 @@ -6812,21 +6799,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.finish().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/tail.rs b/rs/moq-net/src/tail.rs index c012fefa815..05f2f1600cc 100644 --- a/rs/moq-net/src/tail.rs +++ b/rs/moq-net/src/tail.rs @@ -106,11 +106,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/test_interop.rs b/rs/moq-net/src/test_interop.rs index 64e44c7697a..ffe2a011be3 100644 --- a/rs/moq-net/src/test_interop.rs +++ b/rs/moq-net/src/test_interop.rs @@ -274,7 +274,7 @@ fn lite_values() -> Vec { /// One GROUP header and its frames, as a publisher writes them: a zigzag timestamp delta on /// a timed track, the payload size, then the payload. The header names a previous start, -/// which only lite-07 carries. +/// which only lite-07 carries, and a frame start, which only lite-06 carries. fn lite_group(version: lite::Version, frames: impl IntoIterator, usize)>) -> Vec { let mut buf = Vec::new(); let w = &mut Encoder::new(&mut buf, version.into()); @@ -282,7 +282,7 @@ fn lite_group(version: lite::Version, frames: impl IntoIterator 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 be9ce12be9f..fdec725c616 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.finish().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 + crate::support::held_uni_streams(version)); - 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.finish().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.finish().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 + crate::support::held_uni_streams(version)); - 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.finish().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.finish().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}"); } } @@ -573,7 +447,15 @@ async fn a_waiting_late_group_lets_a_newer_group_through() { let mut newer = track.create_group(moq_net::group::Info { start: 3 }).unwrap(); newer.write_frame(newer.at_start(), b"new".as_slice()).unwrap(); newer.finish().unwrap(); - assert_eq!(next!("the newer group waits behind the late one").start, 3, "{version}"); + // Lite-07 drops the late group as OLD when the newer one takes its place, and this + // reader, whose budget still covers it, asks for it again on a request of its own. That + // copy can land once the reset returns the credit, and the reader takes the lowest + // group across copies first. + let mut next = next!("the newer group waits behind the late one"); + if next.start == 0 && version.to_string() == "moq-lite-07-wip" { + next = next!("the newer group waits behind the late one"); + } + assert_eq!(next.start, 3, "{version}"); drop(held); } } @@ -716,71 +598,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 + crate::support::held_uni_streams(version)); - 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 b2d45486651..00000000000 --- a/rs/moq-net/verify/subscribe/Continuation.cfg +++ /dev/null @@ -1,18 +0,0 @@ -\* A group of two frames, paused up to three times, without pressure: where each -\* continuation starts against the frames the subscriber holds, and every other rule. -CONSTANTS - Groups = {0} - MaxStreams = 4 - MaxPauses = 3 - Frames = 2 - Pressure = FALSE - -SPECIFICATION Spec - -INVARIANTS - ServedOnce - NamedOnce - QuietIsSettled - PausedIsOwed - OneReader - ContinuesWithinHeld diff --git a/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg b/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg index 9b0f2461929..7217a8cc09c 100644 --- a/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg +++ b/rs/moq-net/verify/subscribe/SubscribeAccounting.cfg @@ -1,13 +1,9 @@ -\* Two groups, six Group Streams, and two YIELDED resets per group. With no update to -\* reopen a drop, a group gets a second stream only as a continuation, so the bounds stay -\* cheap; the second group puts both groups' drops and skips on one Subscribe Stream. -\* Pressure lets the publisher's record fold at any point. Frames are left out: -\* Continuation.cfg checks where a continuation starts. +\* Three groups, each served on at most one Group Stream. With no update to reopen a drop +\* and no second stream of a group, the bounds stay cheap enough for a third group: a skip +\* of 1 can be held back for 2's GROUP, and a run of 1 and 2 for 3's. Pressure lets the +\* publisher's record fold at any point. CONSTANTS - Groups = {1, 2} - MaxStreams = 6 - MaxPauses = 2 - Frames = 0 + Groups = {1, 2, 3} Pressure = TRUE SPECIFICATION Spec @@ -16,5 +12,3 @@ INVARIANTS ServedOnce NamedOnce QuietIsSettled - PausedIsOwed - OneReader diff --git a/rs/moq-net/verify/subscribe/SubscribeAccounting.tla b/rs/moq-net/verify/subscribe/SubscribeAccounting.tla index 14e307ff8d4..a9adc40fc2a 100644 --- a/rs/moq-net/verify/subscribe/SubscribeAccounting.tla +++ b/rs/moq-net/verify/subscribe/SubscribeAccounting.tla @@ -13,10 +13,12 @@ (* order on the Subscribe Stream; each Group Stream travels on its own, *) (* unordered against them, and a reset can lose its header. *) (* *) -(* A lite-07 Group Stream whose group expires stale is closed, not reset: *) +(* A lite-07 Group Stream whose group expires stale is closed, not reset: *) (* FIN without GROUP_END, so the subscriber keeps what arrived as a closed *) (* group, and no drop names it. A stream that never opened is still given *) (* up with a drop, and a stream whose FIN went out is never reset for it. *) +(* A stream reset while serving, evicted or preempted with TOO_FAR_BEHIND *) +(* for a group that outranks it, is named in a final drop. *) (* *) (* Each GROUP carries the previous group's start, passing over the *) (* skipped starts directly below it as a SUBSCRIBE_SKIP would. The *) @@ -25,57 +27,39 @@ (* up instead. A drop of a group whose stream opened goes out after a *) (* skip of what its GROUP passed over, since the GROUP may never arrive. *) (* *) -(* 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. It is the only second Group Stream of a group. *) -(* *) (* 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) 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, and whether the cache holds the group above a skip, which *) -(* decides whether the skip is held back for its GROUP. Under `Pressure`, *) -(* so is when the pool's debt folds the publisher's record of the group. *) +(* routes. So are when a stream fails, standing in for stream credit, *) +(* expiry, eviction, and preemption, and whether the cache holds the group *) +(* above a skip, which decides whether the skip is held back for its *) +(* GROUP. Under `Pressure`, so is when the pool's debt folds the *) +(* publisher's record of the group. *) (* *) (* Scope: the subscription is already answered (SUBSCRIBE_OK) and covers *) -(* every group; ranges, starts, fills, and the tail's grace are left out. *) -(* Group sequences start at 1 in `SubscribeAccounting.cfg`: a GROUP passes *) -(* over a run only above start 0, the previous start it then names. *) -(* GROUP_END takeover is left out: only the YIELDED reset pauses a group. *) +(* every group; ranges, starts, fills, frames, and the tail's grace are *) +(* left out. Group sequences start at 1 in `SubscribeAccounting.cfg`: a *) +(* GROUP passes over a run only above start 0, the previous start it then *) +(* names. GROUP_END takeover is left out. *) (***************************************************************************) EXTENDS Integers, Sequences, FiniteSets CONSTANTS Groups, \* group sequences in range - 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 publisher's reader debt, so its record 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), "never" (never opened, so -\* nothing reached the wire), "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 +\* nothing reached the wire), "header" (filling the copy), "fin", or "reset" \* passes: the skipped groups its GROUP's Previous Start passes over -NewStream(g, of, start, passes) == - [g |-> g, pub |-> "open", err |-> "", sub |-> "none", of |-> of, - start |-> start, wrote |-> 0, read |-> 0, passes |-> passes] +NewStream(g, passes) == [g |-> g, pub |-> "open", err |-> "", sub |-> "none", passes |-> passes] \* A drop names one group; a skip names a run of them. DropMsg(g, err) == [type |-> "drop", gs |-> {g}, err |-> err] SkipMsg(gs) == [type |-> "skip", gs |-> gs, err |-> ""] -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" @@ -87,8 +71,6 @@ VARIABLES \* skipped groups its GROUP passed over held, \* `TrackRun::elided`: per group, the skipped run held back for its GROUP folded, \* the record folded the group into its floor - paused, \* the YIELDED stream a paused group waits to continue, or 0 - pauses, \* YIELDED resets sent for the group ndrop, \* history: drops naming the group nskip, \* history: skips naming the group on their own nahead, \* history: skips naming the group again, ahead of a drop of the group above @@ -97,17 +79,12 @@ VARIABLES resp, \* the Subscribe Stream, publisher to subscriber \* The subscriber's `Tail` and copy. acct, \* the tail's ledger: groups accounted for - reading, \* the part of `acct` only the stream filling the copy accounts for - sdrop, \* groups a drop named (`TrackEntry::dropped`) 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 - got, \* history: the subscriber read the group to its FIN - malformed \* a continuation started past the frames the copy holds + cached \* the copy's group: "none", "open", "whole", "aborted", "evicted" names == <> -pubVars == <> -subVars == <> +pubVars == <> +subVars == <> vars == <> Init == @@ -115,18 +92,13 @@ Init == /\ late = [g \in Groups |-> FALSE] /\ served = {} /\ dropped = {} /\ outq = <<>> /\ folded = {} /\ held = [g \in Groups |-> {}] - /\ paused = [g \in Groups |-> 0] - /\ pauses = [g \in Groups |-> 0] /\ ndrop = [g \in Groups |-> 0] /\ nskip = [g \in Groups |-> 0] /\ nahead = [g \in Groups |-> 0] /\ streams = <<>> /\ resp = <<>> - /\ acct = {} /\ reading = {} /\ sdrop = {} + /\ acct = {} /\ rec = [g \in Groups |-> "none"] /\ cached = [g \in Groups |-> "none"] - /\ have = [g \in Groups |-> 0] - /\ got = {} - /\ malformed = FALSE ----------------------------------------------------------------------------- (* Publisher *) @@ -141,8 +113,7 @@ SkippedBelow(g) == \* `serve`: open a Group Stream for g, which `mark_served` records. Its GROUP passes over \* the skipped run below it and the one held back for it, which the cache may have forgotten. Serve(g) == - /\ Len(streams) < MaxStreams - /\ streams' = Append(streams, NewStream(g, 0, 0, SkippedBelow(g) \cup held[g])) + /\ streams' = Append(streams, NewStream(g, SkippedBelow(g) \cup held[g])) /\ served' = served \cup {g} /\ held' = [held EXCEPT ![g] = {}] @@ -159,7 +130,7 @@ CursorHand(g) == /\ IF g \in served THEN UNCHANGED <> ELSE Serve(g) - /\ UNCHANGED <> + /\ UNCHANGED <> \* Count each group of `gs` named once more in `n`. Named(n, gs) == [x \in Groups |-> IF x \in gs THEN n[x] + 1 ELSE n[x]] @@ -196,75 +167,32 @@ CursorGiveUp(g, kind, hold) == /\ nskip' = CASE ~named \/ hold -> nskip [] kind = "skip" -> Named(nskip, run) [] OTHER -> Named(nskip, held[g]) - /\ 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`). +\* A serving stream FINs (`GroupState::Closed`): after its last frame, or at a frame +\* boundary once its group expires stale (`Ending::Expired`), without a GROUP_END, so what +\* it wrote stands as a closed group. Nothing names the group either way. StreamFinAt(i) == /\ streams[i].pub = "open" - /\ streams[i].start + streams[i].wrote = Frames - /\ streams' = [streams EXCEPT ![i].pub = "fin"] - /\ UNCHANGED <> - -\* A serving stream's group expires stale (`Ending::Expired`): the stream FINs at the next -\* frame boundary without a GROUP_END, and what it wrote stands as a closed group. Nothing -\* names the group. With frames left out, this is `StreamFin`. -StreamCloseAt(i) == - /\ streams[i].pub = "open" - /\ streams[i].start + streams[i].wrote < Frames /\ streams' = [streams EXCEPT ![i].pub = "fin"] /\ UNCHANGED <> -\* A stream fails and resets: it is evicted while serving, or expires stale before it -\* opened, waiting for stream credit (`give_way`, `shed`), so nothing of it reached the -\* wire. `settle_failed` queues the outcome, with what its GROUP passed over. +\* A stream fails and resets: it expires stale before it opened, waiting for stream credit +\* (`give_way`, `shed`), so nothing of it reached the wire; or it is reset while serving, +\* evicted or preempted with TOO_FAR_BEHIND for a group that outranks it, both final +\* ("evicted"). `settle_failed` queues the outcome, with what its GROUP passed over. StreamFailAt(i, err) == /\ streams[i].pub = "open" - /\ err = "old" => streams[i].sub = "none" /\ streams[i].wrote = 0 + /\ err = "old" => streams[i].sub = "none" /\ streams' = [streams EXCEPT ![i].pub = "reset", ![i].err = err, ![i].sub = IF err = "old" THEN "never" ELSE @] /\ 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. Its -\* GROUP carries the same Previous Start. -PubContinue(g) == - /\ paused[g] # 0 - /\ Len(streams) < MaxStreams - /\ LET k == paused[g] - IN streams' = Append(streams, NewStream(g, k, streams[k].start, streams[k].passes)) - /\ 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 <> + /\ UNCHANGED <> \* The peer acknowledged everything up to the FIN. StreamAckAt(i) == /\ streams[i].pub = "fin" - /\ streams[i].sub \in {"fin", "dup"} + /\ streams[i].sub = "fin" /\ streams' = [streams EXCEPT ![i].pub = "acked"] /\ UNCHANGED <> @@ -281,7 +209,7 @@ PubOutcomes == IN /\ resp' = IF passes = {} THEN Append(resp, drop) ELSE resp \o <> /\ ndrop' = [ndrop EXCEPT ![g] = @ + 1] /\ nahead' = Named(nahead, passes) - /\ UNCHANGED <> + /\ UNCHANGED <> \* The pool hands the reader debt and its record folds the group into its floor: a group \* the cursor reached a verdict on, once no failed stream of it is still to name. Every @@ -293,37 +221,19 @@ PubFold(g) == /\ \A i \in 1..Len(outq) : outq[i][1] # g /\ folded' = folded \cup {g} /\ dropped' = dropped \ {g} - /\ UNCHANGED <> + /\ UNCHANGED <> ----------------------------------------------------------------------------- (* Subscriber *) ----------------------------------------------------------------------------- -\* The streams of g waiting, unread, for the copy another stream is filling. -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: a -\* stream that finished it, a reset, a drop, or a skip. 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 - -\* 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} - \* The copy's record `r` once a skip names `gs`: a group that arrived keeps it \* (`skip_received`). SkipRec(r, gs) == [x \in Groups |-> IF x \in gs /\ cached[x] \notin {"open", "whole"} THEN "skip" ELSE r[x]] \* 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. Every -\* GROUP's Previous Start skips what it passes over, as a SUBSCRIBE_SKIP would; a -\* continuation's repeats its paused stream's, which already did. +\* accounts for the group, and the stream fills the copy, replacing a stale record of it. +\* Its GROUP's Previous Start skips what it passes over, as a SUBSCRIBE_SKIP would. SubHeaderAt(i) == /\ streams[i].sub = "none" /\ streams[i].pub \in {"open", "fin", "reset"} @@ -331,134 +241,65 @@ SubHeaderAt(i) == passes == streams[i].passes unstale == [rec EXCEPT ![g] = IF @ = "stale" THEN "none" ELSE @] IN /\ acct' = acct \cup {g} \cup passes - /\ CASE Filling(g) \/ cached[g] = "open" -> - /\ streams' = [streams EXCEPT ![i].sub = "held"] - /\ rec' = SkipRec(rec, passes) - /\ UNCHANGED <> - [] cached[g] = "whole" -> - /\ streams' = [streams EXCEPT ![i].sub = "dup"] - /\ rec' = SkipRec(rec, passes) - /\ UNCHANGED <> - [] cached[g] = "paused" -> - /\ streams' = [streams EXCEPT ![i].sub = "header"] - /\ cached' = [cached EXCEPT ![g] = "open"] - /\ rec' = SkipRec(unstale, passes) - /\ malformed' = (malformed \/ streams[i].start > have[g]) - /\ reading' = IF Settled(g) THEN reading ELSE reading \cup {g} - /\ UNCHANGED have - [] OTHER -> - /\ streams' = [streams EXCEPT ![i].sub = "header"] - /\ cached' = [cached EXCEPT ![g] = "open"] - /\ rec' = SkipRec(unstale, passes) - /\ 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 <> + /\ streams' = [streams EXCEPT ![i].sub = "header"] + /\ cached' = [cached EXCEPT ![g] = "open"] + /\ rec' = SkipRec(unstale, passes) + /\ UNCHANGED <> \* The stream filling the copy is read to its FIN: the group is whole after a GROUP_END, -\* or closed where an expired one stopped, which the copy keeps alike ("whole"). A held -\* stream of the group then only duplicates it, a continuation included. +\* or closed where an expired one stopped, which the copy keeps alike ("whole"). SubFinAt(i) == /\ streams[i].sub = "header" - /\ streams[i].read = streams[i].wrote /\ streams[i].pub \in {"fin", "acked"} - /\ LET g == streams[i].g - IN /\ streams' = [j \in 1..Len(streams) |-> - IF j = i THEN [streams[j] EXCEPT !.sub = "fin"] - ELSE IF j \in Held(g) THEN [streams[j] EXCEPT !.sub = "dup"] - ELSE streams[j]] - /\ cached' = [cached EXCEPT ![g] = "whole"] - /\ got' = got \cup {g} - /\ reading' = reading \ {g} - /\ 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 accounts for the group. A -\* YIELDED stream whose header arrived late, after another stream settled the group, -\* accounted nothing and takes nothing back. + /\ streams' = [streams EXCEPT ![i].sub = "fin"] + /\ cached' = [cached EXCEPT ![streams[i].g] = "whole"] + /\ UNCHANGED <> + +\* The reset of the stream filling the copy arrives and ends the group's readers with its +\* error. The header already accounted for the group, and the drop naming it follows. 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 - 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} - /\ UNCHANGED <> - -\* A held stream's own reset arrives. -SubResetHeldAt(i) == - /\ streams[i].sub = "held" - /\ streams[i].pub = "reset" /\ streams' = [streams EXCEPT ![i].sub = "reset"] - /\ UNCHANGED <> + /\ cached' = [cached EXCEPT ![streams[i].g] = "aborted"] + /\ UNCHANGED <> \* The copy's cache evicts a whole group. Evict(g) == /\ cached[g] = "whole" /\ cached' = [cached EXCEPT ![g] = "evicted"] - /\ UNCHANGED <> - -\* `apply_response` for SUBSCRIBE_DROP: the tail accounts for the group, and a paused copy -\* or a stream still reading it ends there (`TrackEntry::dropped`). A stale drop records -\* it in the copy unless the copy holds it (`give_up` keeps an open group, though not a -\* paused one), 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, when the drop is the slot's cut -\* (`Slot::verdict`): never lost, never beside a group. + /\ UNCHANGED <> + +\* `apply_response` for SUBSCRIBE_DROP: the tail accounts for the group. A stale drop +\* records it in the copy unless the copy holds 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, when the drop is the slot's cut (`Slot::verdict`): +\* never lost, never beside a group. OnDrop(m) == LET g == CHOOSE g \in m.gs : TRUE IN /\ acct' = acct \cup {g} - /\ sdrop' = sdrop \cup {g} /\ IF m.err = "old" THEN /\ rec' = IF cached[g] \notin {"open", "whole"} /\ rec[g] # "skip" THEN [rec EXCEPT ![g] = "stale"] ELSE rec - /\ cached' = [cached EXCEPT ![g] = IF @ = "paused" THEN "aborted" ELSE @] + /\ UNCHANGED cached ELSE /\ 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 @] + /\ cached' = [cached EXCEPT ![g] = IF @ = "open" THEN "aborted" ELSE @] \* `apply_response` for SUBSCRIBE_SKIP: final, overriding any drop, but a group that \* arrived keeps it (`skip_received`). OnSkip(m) == /\ acct' = acct \cup m.gs /\ rec' = SkipRec(rec, m.gs) - /\ UNCHANGED <> + /\ UNCHANGED cached SubRecv == /\ resp # <<>> /\ resp' = Tail(resp) /\ LET m == Head(resp) IN IF m.type = "drop" THEN OnDrop(m) ELSE OnSkip(m) - /\ UNCHANGED <> + /\ UNCHANGED <> ----------------------------------------------------------------------------- @@ -466,50 +307,38 @@ SubRecv == \* read to the reset, lost, or never sent. StreamDone(s) == \/ s.pub = "acked" - \/ s.pub = "reset" /\ s.sub \in {"none", "never", "fin", "reset", "dup"} + \/ s.pub = "reset" /\ s.sub \in {"none", "never", "reset"} \* Nothing in flight and nothing left for the publisher to do. Quiet == /\ resp = <<>> /\ outq = <<>> - /\ \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. +\* The end. Any other state where nothing can happen is a deadlock TLC reports. Done == - /\ Quiet \/ Len(streams) = MaxStreams + /\ Quiet /\ 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) -StreamClose == \E i \in 1..Len(streams) : StreamCloseAt(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) Next == \/ \E g \in Groups : CursorHand(g) \/ \E g \in Groups, kind \in {"old", "evicted", "skip"}, hold \in BOOLEAN : CursorGiveUp(g, kind, hold) - \/ StreamWrite \/ StreamFin - \/ StreamClose \/ StreamFail - \/ StreamYield \/ StreamAck - \/ \E g \in Groups : PubContinue(g) - \/ \E g \in Groups, err \in {"old", "evicted"} : PausedGiveUp(g, err) \/ PubOutcomes \/ \E g \in Groups : PubFold(g) \/ SubHeader - \/ SubFrame \/ SubFin \/ SubReset - \/ SubResetHeld \/ \E g \in Groups : Evict(g) \/ SubRecv \/ Done @@ -523,16 +352,10 @@ Spec == Init /\ [][Next]_vars \* Once quiet, the tail accounts for every group: none is waited for until the grace. QuietIsSettled == Quiet => acct = Groups -\* The publisher opens a second Group Stream for a group only to continue the group's -\* latest stream, which it reset with YIELDED. +\* The publisher opens at most one Group Stream for a group, so the subscriber never reads +\* two at once and no group's bytes go out twice. ServedOnce == - \A i, j \in 1..Len(streams) : - /\ (i < j /\ streams[i].g = streams[j].g) => streams[i].pub = "reset" /\ streams[j].of # 0 - /\ 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 i, j \in 1..Len(streams) : i # j => streams[i].g # streams[j].g \* Every name is final: the publisher names a group in at most one drop, and in at most \* one skip, which goes out over a drop of a group it never served and over nothing else. @@ -545,27 +368,4 @@ NamedOnce == /\ nahead[g] <= 1 /\ nskip[g] + nahead[g] > 0 => \A i \in 1..Len(streams) : streams[i].g # 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, 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 - -\* The subscriber reads at most one Group Stream of a group at a time. -OneReader == - \A g \in Groups : - Cardinality({i \in 1..Len(streams) : streams[i].g = g /\ streams[i].sub = "header"}) <= 1 - ============================================================================= diff --git a/rs/moq-tokio/tests/integration/receive_credit.rs b/rs/moq-tokio/tests/integration/receive_credit.rs index 25bb2d47ff0..ee3aa5324a8 100644 --- a/rs/moq-tokio/tests/integration/receive_credit.rs +++ b/rs/moq-tokio/tests/integration/receive_credit.rs @@ -626,9 +626,8 @@ fn group_stream(subscribe: u64, sequence: u64, size: usize) -> Vec { let mut header = Vec::new(); varint(&mut header, subscribe); varint(&mut header, sequence); - // No previous start: 0 says unknown, since the field carries it plus one. - varint(&mut header, 0); - // The first frame. + // No previous start: 0 says unknown, since the field carries it plus one. A lite-07 + // GROUP has no Frame Start: every Group Stream starts at frame 0. varint(&mut header, 0); // GROUP, then its size-prefixed header. 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 be771a56d20..c2a6645b56d 100644 --- a/test/stream-credit/src/main.ts +++ b/test/stream-credit/src/main.ts @@ -37,8 +37,9 @@ window.subscribe = async (plan) => { const request = origin.request(Moq.Path.from(plan.broadcast)); const active = await request.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) }, }), @@ -48,7 +49,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 }; @@ -63,8 +66,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[] = []; @@ -85,6 +95,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); } })(), ); @@ -96,3 +108,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. */