Skip to content
15 changes: 15 additions & 0 deletions js/net/src/ietf/join.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, jest, onTestFinished, test } from "bun:test";
import { Elapsed, Instant, Milli, Nano, now, timeout } from "@moq/clock";
import * as Connection from "../connection/index.ts";
import { StreamCode } from "../error.ts";
import { GRACE } from "../ledger.ts";
import { createMockTransportPair, textFrame } from "../mock.ts";
import { Producer as OriginProducer } from "../origin.ts";
Expand Down Expand Up @@ -329,6 +330,20 @@ test("SUBSCRIPTION_ENDED cancels a head nothing claimed", async () => {
await expect(frames(group)).rejects.toThrow();
});

test("MALFORMED_TRACK fails the track as malformed, not as a relay bug", async () => {
const { consumer, fetching, peer } = await join();
await accept(fetching);

await peer.writer.u53(PublishDone.id);
const done = new PublishDone({
statusCode: PublishDoneStatus.MALFORMED_TRACK,
streamCount: 0n,
reasonPhrase: "malformed track",
});
await done.encode(peer.writer, VERSION);
expect(await consumer.closed).toMatchObject({ code: StreamCode.MalformedTrack });
});

test("a whole group that precedes the head wins the sequence", async () => {
const { subscriber, consumer, fetching, fetch } = await join();
if (!fetch) throw new Error("no fetch");
Expand Down
2 changes: 2 additions & 0 deletions js/net/src/ietf/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export const PublishDoneStatus = {
/** Unassigned in draft-22. */
SUBSCRIPTION_ENDED: 0x3,
UPDATE_FAILED: 0x8,
/** A relay found the track malformed, so it ended every subscription to it. */
MALFORMED_TRACK: 0x12,
} as const;

/**
Expand Down
12 changes: 11 additions & 1 deletion js/net/src/ietf/publisher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { Signal } from "@moq/signals";
import type { Credential, Grant } from "../auth.ts";
import { Producer as BroadcastProducer } from "../broadcast.ts";
import * as Epoch from "../epoch.ts";
import { error, ProtocolViolation, SessionCode, SessionError, StreamCode, unauthorized } from "../error.ts";
import {
error,
ProtocolViolation,
SessionCode,
SessionError,
StreamCode,
Stream as StreamError,
unauthorized,
} from "../error.ts";
import { type Hop, HopSchema } from "../hop.ts";
import { hooks } from "../internal.ts";
import { createMockTransportPair, SessionBroadcast } from "../mock.ts";
Expand Down Expand Up @@ -2067,6 +2075,8 @@ test("subscription completion sends PUBLISH_DONE on every supported draft", asyn
[new Error("failed"), 0x0],
[unauthorized("test"), 0x1],
[new SessionError(SessionCode.Expired), 0x1],
// So does a malformed upstream: MALFORMED_TRACK, so the subscriber can tell it from a bug here.
[new StreamError(StreamCode.MalformedTrack), 0x12],
] as const) {
const pair = createMockTransportPair(ALPN.DRAFT_18);
const session = new NativeSession(pair.server, version, true);
Expand Down
18 changes: 12 additions & 6 deletions js/net/src/ietf/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,23 +1024,29 @@ export class Publisher {
// PUBLISH_DONE is required for every supported draft. The peer may have already
// closed its side to unsubscribe, in which case there is nowhere to send it.
const updateFailed = publishError instanceof UpdateFailed;
// A malformed upstream, such as a moq-lite MALFORMED_TRACK reset, is passed on as one.
const malformed = publishError instanceof StreamError && publishError.code === StreamCode.MalformedTrack;
try {
const done = new PublishDone({
statusCode: unauthorized
? PublishDoneStatus.UNAUTHORIZED
: updateFailed
? PublishDoneStatus.UPDATE_FAILED
: publishError
? PublishDoneStatus.INTERNAL_ERROR
: PublishDoneStatus.TRACK_ENDED,
: malformed
? PublishDoneStatus.MALFORMED_TRACK
: publishError
? PublishDoneStatus.INTERNAL_ERROR
: PublishDoneStatus.TRACK_ENDED,
streamCount: BigInt(streams.opened),
reasonPhrase: unauthorized
? "not granted"
: updateFailed
? "update failed"
: publishError
? "internal error"
: "track ended",
: malformed
? "malformed track"
: publishError
? "internal error"
: "track ended",
});
await Message.write(request.writer, PublishDone.id, done, version);
} catch {
Expand Down
7 changes: 7 additions & 0 deletions js/net/src/ietf/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,13 @@ export class Subscriber {
const done = await PublishDone.decode(framed.reader, version);
// A revocation reads the same as one from a moq-lite peer.
if (done.statusCode === PublishDoneStatus.UNAUTHORIZED) throw unauthorized(broadcast);
// The peer's code, as a moq-lite MALFORMED_TRACK reset reads, so a consumer can tell a
// malformed upstream apart from a relay bug.
if (done.statusCode === PublishDoneStatus.MALFORMED_TRACK) {
throw new StreamError(StreamCode.MalformedTrack, {
message: `publish done: malformed track reason=${done.reasonPhrase}`,
});
}
if (!publishDoneClean(done.statusCode, version)) {
throw new Error(`publish done: status=0x${done.statusCode.toString(16)} reason=${done.reasonPhrase}`);
}
Expand Down
1 change: 0 additions & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ lite-07; bitrate enforcement is not part of this release.
- [Lint override and deprecation audit](/quest/m2/audit-lints.md) - repeat the suppression and deprecation audit after the release ships, starting with clippy on the macOS and Windows targets
- [Tenant matrix on both runtimes](/quest/m2/tenant-matrix.md) - tenant, mount, HTTP, normalization, and revalidation boundaries are tested on Tokio and io_uring
- [Archive reads against a real store](/quest/m2/archive-store-bench.md) - LIST/GET counts and latency over track count
- [A malformed track says so downstream](/quest/m2/malformed-track-status.md) - subscribers and bindings see a malformed track, not INTERNAL_ERROR
- [Branch checks lint examples](/quest/m2/lint-examples.md) - `just check` runs clippy on the selected crates' examples
- [Drop kio's epoch fence](/quest/m2/crossbeam-pin-fence.md) - waiting on crossbeam#1352, then the `Read::drop` fence goes
- [A cancelled miss stops waiting](/quest/m2/fill-wait-cancel-test.md) - a test proves a relay miss waiting on its own fill ends when no reader wants it
Expand Down
27 changes: 0 additions & 27 deletions quest/m2/malformed-track-status.md

This file was deleted.

4 changes: 0 additions & 4 deletions quest/m2/publish-done-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,3 @@ Follow-up of [moq-dev/moq#7396](https://github.com/moq-dev/moq/pull/7396), which
Decided 2026-10-30 (maintainer, follow-up planning after the /quest-afk run). Reuse #7396's path: the publisher picks the PUBLISH_DONE code from the same error-to-code mapping that resets the track's streams, and the subscriber reads it back as the peer's stream code. Cover drafts 18 and 22, both bindings, and JS, each test failing without its fix.

Public API: none. Wire: existing registered codes only.

## Required

- [A malformed track says so downstream](/quest/m2/malformed-track-status.md) - its PR, #7396, adds the PUBLISH_DONE code path this extends
14 changes: 14 additions & 0 deletions rs/moq-c/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,18 @@ mod tests {
super::moq_protocol_kind::MOQ_PROTOCOL_KIND_TOO_MANY_REQUESTS as u32
);
}

/// A relay that found its upstream malformed says so, apart from a relay bug's INTERNAL_ERROR.
#[test]
fn received_malformed_track_keeps_its_kind() {
let error = super::Error::Moq(moq_net::StreamError::MalformedTrack.into())
.protocol()
.expect("a stream code");
assert_eq!(error.scope, super::moq_error_scope::MOQ_ERROR_SCOPE_STREAM as u32);
assert_eq!(error.code, 0x12);
assert_eq!(
error.kind,
super::moq_protocol_kind::MOQ_PROTOCOL_KIND_MALFORMED_TRACK as u32
);
}
}
15 changes: 15 additions & 0 deletions rs/moq-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,21 @@ mod tests {
}
}

/// A relay that found its upstream malformed says so, and a binding can tell that
/// apart from a relay bug's INTERNAL_ERROR.
#[test]
fn stream_malformed_track_keeps_its_kind() {
let err = MoqError::from(moq_net::Error::from(moq_net::StreamError::MalformedTrack));
match err {
MoqError::Protocol { details: protocol } => {
assert_eq!(protocol.scope, MoqErrorScope::Stream);
assert_eq!(protocol.code, 0x12);
assert_eq!(protocol.kind, MoqProtocolKind::MalformedTrack);
}
other => panic!("expected Protocol, got {other}"),
}
}

#[test]
fn stream_app_code_is_verbatim() {
let err = MoqError::from(moq_net::Error::from(moq_net::StreamError::App(7)));
Expand Down
17 changes: 16 additions & 1 deletion rs/moq-net/src/ietf/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub(crate) enum PublishDoneStatus {
TrackEnded,
/// A requested subscription update could not be applied.
UpdateFailed,
/// A relay found the track malformed, so it ended every subscription to it.
MalformedTrack,
}

impl PublishDoneStatus {
Expand All @@ -140,6 +142,7 @@ impl PublishDoneStatus {
Self::Unauthorized => 0x1,
Self::TrackEnded => 0x2,
Self::UpdateFailed => 0x8,
Self::MalformedTrack => 0x12,
}
}
}
Expand Down Expand Up @@ -169,6 +172,10 @@ impl PublishDone<'_> {
match self.status_code {
code if code == PublishDoneStatus::TrackEnded.code() => Ok(()),
code if code == PublishDoneStatus::Unauthorized.code() => Err(crate::Error::Unauthorized),
// The peer's code, as a moq-lite MALFORMED_TRACK reset reads, so bindings name it.
code if code == PublishDoneStatus::MalformedTrack.code() => {
Err(crate::Error::Stream(crate::StreamError::MalformedTrack))
}
// SUBSCRIPTION_ENDED: the subscription reached the end its filter asked for.
// Draft-22 removed it and left 0x3 unassigned.
0x3 if matches!(version, Version::Draft18) => Ok(()),
Expand Down Expand Up @@ -475,10 +482,11 @@ mod tests {

#[test]
fn test_publish_done_registered_statuses_all_versions() {
for version in [Version::Draft18] {
for version in [Version::Draft18, Version::Draft22] {
for (status, expected) in [
(PublishDoneStatus::InternalError, 0x0),
(PublishDoneStatus::TrackEnded, 0x2),
(PublishDoneStatus::MalformedTrack, 0x12),
] {
assert_eq!(status.code(), expected);

Expand Down Expand Up @@ -517,6 +525,13 @@ mod tests {
matches!(done(0x0).end(version), Err(crate::Error::Remote(0x0))),
"{version:?}"
);
assert!(
matches!(
done(0x12).end(version),
Err(crate::Error::Stream(crate::StreamError::MalformedTrack))
),
"{version:?}"
);
}

// SUBSCRIPTION_ENDED is clean until draft-22 unassigned it.
Expand Down
32 changes: 32 additions & 0 deletions rs/moq-net/src/ietf/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ impl Served {
Self::Track(Err(Error::Expired | Error::Session(crate::SessionError::Expired))) => {
(ietf::PublishDoneStatus::Unauthorized, "token expired")
}
// The code its group streams reset with, so a relay passes a malformed upstream on
// rather than hiding it behind INTERNAL_ERROR.
Self::Track(Err(err)) if crate::StreamError::from(err) == crate::StreamError::MalformedTrack => {
(ietf::PublishDoneStatus::MalformedTrack, "malformed track")
}
Self::Track(Err(_)) => (ietf::PublishDoneStatus::InternalError, "internal error"),
Self::UpdateFailed(_) | Self::RenewalRefused(_) => (ietf::PublishDoneStatus::UpdateFailed, "update failed"),
// Pausing ends no subscription, so nothing reports it.
Expand Down Expand Up @@ -7365,6 +7370,33 @@ mod serve_tests {
drop(group);
}

/// A track aborted as malformed, as a relay does over an upstream's unstamped object on a
/// timed track, ends each subscription with PUBLISH_DONE(MALFORMED_TRACK), on either draft.
/// A moq-lite or JS upstream's MALFORMED_TRACK reset says the same.
#[kio::test]
async fn publish_done_after_a_malformed_track_says_so() {
for version in [Version::Draft18, Version::Draft22] {
for err in [Error::MalformedTrack, Error::Stream(crate::StreamError::MalformedTrack)] {
let h = serve(version);
let stream = Stream::open(&mut crate::driver::Transport::new(h.session.clone()), version)
.await
.unwrap();
let msg = subscribe(Filter::NextObject, None);
let mut serve = std::pin::pin!(h.publisher.clone().run_subscribe_stream(stream, msg));
serve_until(serve.as_mut(), "SUBSCRIBE_OK was sent", || !h.log.trail().is_empty()).await;

h.track.abort(err.clone()).unwrap();
kio::clock::timeout(std::time::Duration::from_secs(10), serve)
.await
.expect("the malformed track ends the subscription")
.ok();

let (status, _) = Wire::read(&h.log, version).publish_done.expect("PUBLISH_DONE was sent");
assert_eq!(status, 0x12, "{version:?} {err:?}");
}
}
}

/// A subscription that runs to the track's end counts every data stream it opened,
/// the fill and the END_OF_TRACK marker included, and closes each one before
/// PUBLISH_DONE(TRACK_ENDED).
Expand Down