Skip to content

fix(io): a refused source fails with its HTTP status, not as invalid data (#378) - #379

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/378-refused-source-status
Aug 17, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/378-refused-source-status

Conversation

@tschuegy

Copy link
Copy Markdown
Contributor

Fixes #378.

What

An origin that answers the source request with an HTTP status instead of media (a 401/403 refusal, a 404, a 5xx) reached the host as sourceOpenFailed carrying FFmpeg's "Invalid data found when processing input", indistinguishable from a corrupt file — and one 403 on the tail prefetch switched the prefetch off for the origin for the rest of the process.

  • Streaming GET checks the status. StreamingDelegate hangs up at the response header on anything but a 200/206 and records the status; open() fails typed with a new AVIOReaderError.httpStatus(Int) (the AE#140/AE#154 shape). HLSVideoEngine.openFailure(from:) passes it through like the HLS classifications, and load() publishes it as the new PlaybackErrorKind.sourceRefused with underlyingCode = the status (additive; the struct is string-backed for exactly this). underlyingDomain stays nil: the sentence is the engine's.
  • No probe ladder after a refusal at byte 0. After a 401/403/404/410 on the open-time data connection the HEAD and bytes=0-1 probes are skipped; the one request still made is the unranged GET, so an origin that refuses Range but serves a plain GET plays forward-only, and one that refuses both fails with its status. 5xx and 429/503/509 keep the ladder (and their seekability).
  • The suffix-range latch takes only the origin's answer to the range form. A 200 that ignored bytes=-n or a 416 still latches (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 stays); a 401/403/404/429/5xx neither latches nor counts as a transport strike.

A readStreaming safety net returns EIO (not EOF) if the refusing header arrives after open()'s 15 s budget — untyped, but never "invalid data" for a refusal.

Why this shape

  • sourceRefused rather than an HTTP domain on sourceOpenFailed: the Machine-readable companion to .error(String) so a host can classify a failure #376 docs say a non-nil underlyingDomain marks OS-localized text, and .vodSourceFailed is the precedent for "the kind says what underlyingCode means".
  • The probe skip is limited to isResolvedExpiryStatus (401/403/404/410), the predicate the file already treats as "not transient"; a HEAD from the same client is answered the same way, and a size learned from it would only re-issue the refused range on the persistent path, which then dies after one retry with the status lost.
  • Not skipping the second open (probe → session reopen): that is the established failure matrix, and it costs one round on a refusing origin.

Test plan

  • swift test: 1874 Swift Testing tests + XCTest, all green on macOS 26 / Xcode 26.
  • New Issue378SourceRefusedTests against a scripted loopback origin: HEAD 200/0 + GET 403 with an error page → open() throws httpStatus(403), cumulativeBytesFetched == 0, exactly one suffix request, one unranged GET, no HEAD / bytes=0-1; the latch stays clear and a second open asks the suffix form again; a 416 still latches; a Range-refusing origin plays forward-only without probes; sequentialOnly is typed too; Demuxer.open throws the reader error, not DemuxerError; openFailure(from:) passes it through; publishLoadFailure publishes sourceRefused/403 vs sourceOpenFailed.
  • PublicAPIDocumentationTests / DocumentedConstantsTests green; docs/api.md names the kind and the contract, docs/architecture.md the reader behaviour.
  • Field: Apple TV 4K, tvOS 26, Xtream VOD MKV behind a portal 302, max_connections=1 panel (the reported origin) — the host now receives sourceRefused / 403 during the slot-held window and plays once the slot frees; will confirm on-device after the release lands in the host's pin.

🤖 Generated with Claude Code

…data (superuser404notfound#378)

An origin that answers the source request with a status instead of media
(a 401/403 refusal, a 404, a 5xx) reached the host as `sourceOpenFailed`
carrying FFmpeg's "Invalid data found when processing input": the
forward-only streaming delegate allowed any status, so the error page went
into the stream buffer and was probed as container bytes, and the 403 the
ranged data connection had already been refused with was read by open() as
"resolved no size". Nothing in the superuser404notfound#376 classification could tell a refused
origin from a corrupt file.

- The streaming GET hangs up at the response header on anything but a
  200/206 and open() fails typed with `AVIOReaderError.httpStatus`, which
  `HLSVideoEngine.openFailure(from:)` passes through like the HLS
  classifications and load() publishes as the new
  `PlaybackErrorKind.sourceRefused` with `underlyingCode` = the status.
- After a 401/403/404/410 on the open-time data connection the HEAD and
  `bytes=0-1` size probes are skipped: the one request still made is the
  unranged GET, so an origin that refuses `Range` but serves a plain GET
  plays forward-only, and one that refuses both fails with its status. 5xx
  and 429/503/509 keep the probe ladder.
- The tail-prefetch "no suffix range support" latch (superuser404notfound#281) is set only by
  the origin's answer to the range form (a 200 that ignored it, a 416). A
  401/403/404/429/5xx on that request neither latches nor counts as a
  transport strike, so one refusal no longer disables the prefetch for the
  origin for the rest of the process.

Measured against a scripted loopback origin (HEAD 200/0, GET 403 with an
error page; Range-refusing origin; 416 to the suffix form).

Co-Authored-By: Claude Fable 5 <[email protected]>
superuser404notfound pushed a commit that referenced this pull request Aug 17, 2026
fix(io): a refused source fails with its HTTP status, not as invalid data (#378)
@superuser404notfound
superuser404notfound merged commit 79ba1b6 into superuser404notfound:main Aug 17, 2026
4 checks passed
superuser404notfound pushed a commit that referenced this pull request Aug 17, 2026
…rges the budget (#378 follow-up)

Two things #379 could not know, because both landed on main after it was
written (#377, 6.28.0).

The reader now types the origin's answer to the open, and load() published all
of it as sourceRefused. But #377 split the rate-limit shapes off for a reason:
429/503/509 means the source is being metered, not lost, so the host is told to
wait rather than to hand off to a second player that the same origin will meter
in turn. Publishing a metered open as sourceRefused hands a host the one verdict
that contract exists to prevent. A 429/503/509 now publishes sourceRateLimited
with the status in underlyingCode; every other status keeps sourceRefused.

The second is a hole in the #377 accounting rather than in #379. A metering
origin is charged wherever a status is first read, and the streaming pump was
the one path that read one without charging it. On a sequential origin that GET
is the session's only request, no ranged open and no probe by construction, so
its 429 was seen by nobody: the budget kept offering that origin its full
concurrency, and the revive arm had no stamp saying the source was metered
rather than gone.

The live source the comment first named was the wrong example, and the test
said so: with prefetch on, a live open goes through the persistent connection,
which charges the budget already. The sequential origin is the real gap.

1901 tests green.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5UztmCZtNYPQpzdmSdmWG
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged as abbcd90. Thank you for this one: the diagnosis was exact down to the line, and the shape you picked (typed reader error passed through `openFailure(from:)`, additive kind on a string-backed struct) is the one the file already uses, so there was nothing to argue about. The scripted-origin tests are what made the probe-skip claim checkable rather than plausible.

The branch was written against 6.26.0 and main has since taken #377 (6.28.0), so two things were pulled in afterwards, in 4bb63c8:

  • A metered open publishes sourceRateLimited, not sourceRefused. One request per 32 MB gets me rate limited, and a 429 then kills the session for good #377 split 429/503/509 off precisely so a host waits instead of handing the source to a second player that the same origin will meter in turn. Typing the open made that split reachable at open time too, and publishing all of it as sourceRefused would hand a host the one verdict the contract exists to prevent. The status stays in underlyingCode either way; every other status keeps sourceRefused exactly as you built it.
  • The streaming pump charges a rate limit against the origin request budget. Not your defect, a hole in the One request per 32 MB gets me rate limited, and a 429 then kills the session for good #377 accounting that your change made visible: a metering origin is charged wherever a status is first read, and the pump was the one path that read one without charging it. On a sequential origin that GET is the session's only request, so its 429 was seen by nobody.

The comment there first named a raw live source as the case, and the test said otherwise: with prefetch on, a live open goes through the persistent connection, which charges the budget already. The sequential origin is the real gap.

Conflicts were only in CHANGELOG.md and docs/api.md; the sources merged clean. 1901 tests green, CI green on main. Release still to be cut, and the issue stays open for the on-device retest you offered.

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.

A refused source (HTTP 403 on every GET) reaches the host as "Invalid data found when processing input" and disables the tail prefetch for the origin

2 participants