Skip to content

One origin request budget, and a 429 that stops ending the session (#377) - #384

Merged
superuser404notfound merged 2 commits into
mainfrom
feat/origin-request-budget
Aug 17, 2026
Merged

superuser404notfound merged 2 commits into
mainfrom
feat/origin-request-budget

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Closes nothing yet: the reporter's retest is what closes #377.

The two defects

A 429 was read as a death. AVIOReader.isRateLimitStatus exists and is used in seven places, all of them inside AVIOReader.swift. Sources/AetherEngine/Video/ contains no reference to rate limiting at all. The classification dies at the give-up arm, which returns a bare -1; FFmpeg renders that as "Operation not permitted", and handleVODReadErrorExit sees what a genuinely dead source produces. It spent both attempts of readErrorReviveGate(maxAttempts: 2) inside a minute, each one reopening from byte 0 against an origin refusing exactly that, then declared the source "not readable in this session". The reporter's own counter-evidence: backing out and pressing play works instantly.

Four connection caps that never composed. httpMaximumConnectionsPerHost is per URLSession, and the reader fetches over persistentSession (2), chunkSession (2), probeSession on URLSessionConfiguration.default (6) and a per-call streaming session, with the subtitle side reader sharing the same static pools. The reporter had this exactly right.

They also identified the part that cannot be resolved from outside the engine: over HTTP/2 a URLSession multiplexes every request of a session onto one connection, so a connection cap bounds nothing while the origin still counts the requests. That makes their own patch (three sessions collapsed to one, cap 1) most likely a no-op. URLSessionTaskMetrics.networkProtocolName was read nowhere in the tree; it now says which case an origin is, once per origin.

What this does

  • OriginRequestBudget: counts requests per origin (keyed scheme+host+port, so a rotating signed token shares one ceiling). Counting is unconditional, capping is not. A limit arrives from the host (LoadOptions.maxConcurrentSourceRequests) or from the origin, halving from the concurrency actually reached on each refusal.
  • Deadlock is excluded structurally, not managed: at one slot the speculative paths (detour blocks, tail prefetch) switch off instead of queueing, each falling back to a serial path it already had. Nothing ever blocks on a slot its own caller holds, and no acquire can block a read indefinitely.
  • A metered read error gets its own gate with a growing backoff (3s, 8s, 20s, 45s) and surfaces PlaybackErrorKind.sourceRateLimited, so a dual-engine host does not hand off to a second player that meets the same refusal.
  • The refusal is stamped against the source URL as well as the refusing one. A metered source is routinely a proxy that 302s to a CDN: the CDN refuses, the engine's revive arm only knows the proxy URL, and keying the verdict solely on the refusing host would have built a classification that is never once reached.

Measured, not assumed

Against a connection-metered origin (limit 1, real Range support, Scripts-free repro in the issue thread), playing one file:

run peak concurrency the origin saw 429s requests served
budget learns from the refusals 2 2 3 (session survives)
maxConcurrentSourceRequests: 1 1 0 1

The tail prefetch was the request that got the pump refused: it leaves microseconds before the first data connection, so the origin saw two on the very first open. That is the "429 before any real number of requests" from the report, and it was invisible to the budget until it took a slot too.

aetherctl play --max-concurrent-requests N is the reproduction knob.

Tests

1883 pass. New: OriginRequestBudgetTests (13, including FIFO fairness, the timeout race that would leak a slot permanently, and the redirect case above), RateLimitedSourceReviveTests (5), plus the slow-read line.

Vincent Herbst and others added 2 commits August 17, 2026 00:03
…he session (#377)

The reader classifies a 429 / 503 / 509 as metering rather than failure, then
threw that away on the way up: the give-up arm returns a bare -1, FFmpeg renders
it as "Operation not permitted", and the session's revive arm saw exactly what a
genuinely dead source produces. It spent both of its two attempts inside a
minute, each one reopening from byte 0 against an origin refusing precisely
that, and ended on "source not readable in this session" while the same stream
plays instantly on a fresh press of play. A metered read error now takes its own
larger budget with a growing backoff (3s, 8s, 20s, 45s) instead of an immediate
reopen, and surfaces PlaybackErrorKind.sourceRateLimited, so a host does not
hand off to a second engine that the same origin will refuse.

httpMaximumConnectionsPerHost is a per-URLSession cap and AVIOReader fetches
over four pools, so against one signed CDN URL a pump range, a detour block and
a probe could all be open at once, with the subtitle side reader sharing the
same static pools. Those caps never composed. OriginRequestBudget counts
requests per origin, which is what a metering origin counts, and unlike a
connection cap it is equally true over HTTP/2, where a session multiplexes every
request onto one connection. Counting is unconditional, capping is not: with no
limit nothing waits, and a limit arrives from the host
(LoadOptions.maxConcurrentSourceRequests) or from the origin itself, halving
from the concurrency actually reached. At one slot the speculative paths switch
off rather than queue, each falling back to the serial path it already had,
which is what keeps this deadlock-free by construction.

Measured against a metered origin serving one file, limit 1:

  budget learned from the refusals   peak=2  429s=2  served=3, session survives
  maxConcurrentSourceRequests: 1     peak=1  429s=0  served=1

The tail prefetch was the request that got the pump refused: it goes out
microseconds before the first data connection, so the origin saw two on the very
first open, which is the "429 before any real number of requests" in the report.
It is speculative, so it now takes a slot only if one is free.

Two things a report could not say before: the negotiated transport is named once
per origin from URLSessionTaskMetrics (h2 means a connection cap bounds
nothing), and the slow-read summary carries origin=<n>inflight/<peak>peak.

Reported by Rasmusmart57.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5UztmCZtNYPQpzdmSdmWG
…ugh to probably work

Both concurrency tests set up "a waiter is parked" with a 300 ms sleep and then
asserted against it. That is a margin against a derived time bound, and the CI
macOS runner took it away: the same tests that pass locally in milliseconds sat
behind a suite where individual cases were taking twenty seconds.

The budget now reports how many callers are parked, so the tests wait for the
state they mean to set up. They are deterministic and finish in 3-6 ms.

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

Rasmusmart57 commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the fast response on this!

I've vendored the branch and I'm running with maxConcurrentSourceRequests: 1.
Also reverted my own three patches (the window bump, the 256 MB range, and the
collapsed session pool) so the test isn't measuring my guesses on top of yours.
You were right that the session collapse was probably a no-op, I had no way to
tell whether the origin was on HTTP/2.

Going to test this now and tomorrow, I'll report back!

@superuser404notfound
superuser404notfound merged commit 3506471 into main Aug 17, 2026
4 checks passed
@superuser404notfound
superuser404notfound deleted the feat/origin-request-budget branch August 17, 2026 03:03
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.

One request per 32 MB gets me rate limited, and a 429 then kills the session for good

2 participants