Skip to content

A source can be warmed before anything asks to play it (#551) - #554

Merged
superuser404notfound merged 3 commits into
mainfrom
feat/551-source-prewarm
Sep 18, 2026
Merged

superuser404notfound merged 3 commits into
mainfrom
feat/551-source-prewarm

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Closes #551.

The engine caches VOD bytes well once a session exists, and not at all before one. Every open therefore starts cold, and a cold open is two to three sequential round trips before the first sample read on a non-fast-start MP4 (#281). A host whose UI knows what comes next can afford those seconds in advance. Reported over Discord by the author of Snappier IPTV, who embeds the engine.

AetherEngine.prewarm(url:httpHeaders:byteBudget:) fetches the opening bytes of a source the engine is not playing. The next load() of that exact URL adopts them: its parse reads are served out of RAM, its size comes with the bytes, and its data connection starts at the warm frontier instead of at byte zero. Nothing waits on a first byte.

Shape

  • SourcePrewarmStore, process-wide and URL-keyed, shaped after SourceContentLengthCache. Adoption TAKES the entry: the session that has the bytes holds them in its own reader, so a copy left behind would hold megabytes for an item that is now playing. That rule is what keeps the store free of an expiry policy. Everything is dropped under memory pressure, because this is the one hoard of bytes in the engine that no session owns.
  • SourcePrewarmPlan decides whether a second request for the trailing object is worth making. It walks the top-level box chain and asks for one only where a cold open would go looking for it: an MP4 whose moov sits behind the media. Matroska never gets one, because it reads no cues at open at either end.
  • SourcePrewarmFetcher, one ranged GET that decides at the response HEADER. The trailing object is asked for as an explicit range rather than bytes=-n, because the head response has already stated the total by then and VOD cold-start pays a mandatory sequential head→tail→re-anchor round trip on non-fast-start MP4 sources — request for parallel fetch or moov-offset hint #281 measured a whole class of origins that decline the suffix form.
  • Adoption in AVIOReader.open(), where the retained head is installed today.
  • aetherctl play --prewarm so it can be measured against a real origin.

Rules it inherits rather than invents

It is a speculative path, so it takes an origin slot only if one is free and never queues for one (#377). Warms are serialised process-wide, since a slot is held for a whole body rather than a round trip; they queue rather than decline, which costs the origin nothing because a queued warm holds no slot and has issued nothing.

Out of scope by construction

nativeRemoteHLS (AVPlayer issues those requests, the engine sees none), persistence across launches, and offline download. This is a head start, not a copy.

What the review of the first cut caught

Four real findings, two of which would have shipped: a 206 wider than the request was buffered whole; the warm was keyed by URL alone while its bytes and its size are also a function of the request headers, and the adopted size could never be corrected; a warm open lost the open-time refusal classification, so an expired token died later and untyped; and the continuation could be stranded when the task was already cancelled, holding the origin ticket for the life of the process. All four are fixed in the second commit, with the reasoning in its message.

Tests run against an origin with a real first-byte cost, never a bare loopback one: a loopback origin answers before the race can be lost, which is how the #281 prefetch passed six tests while never once winning in the field.

Full suite green on both runners, strict concurrency clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F

Vincent Herbst and others added 3 commits September 18, 2026 21:01
The engine caches VOD bytes well once a session exists, and not at all before one.
Every open therefore starts cold, and a cold open is two to three sequential round
trips before the first sample read on a non-fast-start MP4 (#281). A host whose UI
knows what comes next can afford those seconds in advance.

`AetherEngine.prewarm(url:httpHeaders:byteBudget:)` fetches the opening bytes of a
source the engine is not playing. The next `load()` of that exact URL adopts them:
its parse reads are served out of RAM, its size comes with the bytes, and its data
connection starts at the warm frontier instead of at byte zero. Nothing waits on a
first byte.

Four pieces, each with one job:

- `SourcePrewarmStore`, process-wide and URL-keyed, shaped after
  `SourceContentLengthCache`. Adoption TAKES the entry: the session that has the
  bytes holds them in its own reader, so a copy left behind would hold megabytes for
  an item that is now playing. That rule is what keeps the store free of an expiry
  policy.
- `SourcePrewarmPlan`, which decides whether a second request for the trailing object
  is worth making. It walks the top-level box chain and asks for one only where a
  cold open would go looking for it: an MP4 whose `moov` sits behind the media.
  Matroska never gets one, because it reads no cues at open at either end.
- `SourcePrewarmFetcher`, one ranged GET that decides at the response HEADER. An
  origin that ignores `Range` and answers 200 with a whole film is hung up on there,
  not in a completion handler with the film in hand (#255).
- Adoption in `AVIOReader.open()`, where the retained head is installed today.

Two rules it inherits rather than invents. It is a speculative path, so it takes an
origin slot only if one is free and never queues for one (#377): a prewarm that would
wait for the playing session's uplink has stopped helping, and the report says so. And
the trailing object is asked for as an explicit range rather than `bytes=-n`, because
the head response has already stated the total by then and #281 measured a whole class
of origins that decline the suffix form.

Out of scope by construction: `nativeRemoteHLS` (AVPlayer issues those requests, the
engine sees none), persistence across launches, and offline download. This is a head
start, not a copy.

Tests run against an origin with a real first-byte cost, never a bare loopback one: a
loopback origin answers before the race can be lost, which is how the #281 prefetch
passed six tests while never once winning in the field.

Reported over Discord by the author of Snappier IPTV, who embeds the engine.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
The guard the test pins is small and its failure is not: a data connection asked for
the range past the last byte is a 416, and an open that takes one looks like a refused
source rather than a source it already held whole.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
…ts own promise (#551)

Six findings from an adversarial review of the first cut. Four were real, and two of
those would have shipped.

**A 206 wider than the request was buffered whole.** The delegate checked the status
and then sized its buffer from the RESPONSE. An origin (or a range-mangling edge) that
answers `bytes=0-8388607` with `Content-Range: bytes 0-4294967295/4294967296` would
have been read to the end before anything looked at the number, which is the exact
runaway the 200 check exists to prevent, through the door next to it. The served range
is now checked against the requested one at the header, and the body is clamped to what
the header promised. The engine's suffix prefetch has made this check since #281
(`suffixRangeStart(expectedLength:)`); this path was written without it.

**A warm was keyed by URL alone, and its size could never be corrected.** An origin that
varies on Referer / User-Agent / Authorization answers a different body under one URL,
and the adopted `fileSize` then fixed a wrong EOF point for the entire session, because
the write-once rule treats any positive size as settled. Two fixes, because one is not
enough: the warm carries the headers it was fetched with and is adopted only by a
session whose headers match, and the connection actually serving the session now
overrules a warm whose size disagrees with it, dropping the spans with it.

**A warm open lost the open-time refusal classification.** It asserts `gotData` rather
than observing it, which is the feature, so the 401/403 ladder never ran for it: a
Jellyfin URL whose token expired between the warm and the play would parse happily out
of warm bytes and die seconds later as an untyped read failure. It is now checked for,
not waited for: a refusal that has already landed when the open finishes is thrown typed
exactly as the cold path throws it, and one that has not still ends the session through
the read loop, later and less precisely. Waiting for the answer is the round trip this
feature removes.

**The continuation could be stranded.** `withTaskCancellationHandler` runs `onCancel`
on the spot when the task is already cancelled, so `task.cancel()` can reach the delegate
before the body installs the handler that resumes the caller. The outcome was dropped
there, the caller waited forever, and the origin ticket it held was never released. The
delegate now holds an outcome that lands early and replays it, under its own lock.

Two smaller ones. Warms are serialised process-wide: a slot is held for a whole body,
not just a round trip, so three warms in flight held three slots while a session played.
They queue rather than decline, which looks like a contradiction of this path's own rule
and is not, because a queued warm holds no slot and has issued nothing. And the store
drops everything under memory pressure: it is the one hoard of bytes in the engine that
no session owns, so it is the cheapest thing in the process to give back.

Two findings were not defects and are recorded as such: the box walker handles
non-zero-`startIndex` slices, cannot loop and cannot overflow, and the oversized adopted
head serves and releases correctly because both sites derive its extent from the buffer
rather than from `headSpanMaxBytes`.

The test origin gained two knobs for this: an over-serving 206, and a total size that
changes between requests.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_015PM3xUJB6ZQyqnmGK1fp6F
@superuser404notfound
superuser404notfound merged commit 4b0c40f into main Sep 18, 2026
7 checks passed
@superuser404notfound
superuser404notfound deleted the feat/551-source-prewarm branch September 18, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prewarm a VOD source before load(): the cache exists, but only after the session that fills it

1 participant