Skip to content

fix(io): a redirect chain is one request budget, so a declared ceiling reaches the host that serves (#388) - #389

Merged
superuser404notfound merged 2 commits into
mainfrom
fix/388-redirect-chain-request-budget
Aug 17, 2026
Merged

superuser404notfound merged 2 commits into
mainfrom
fix/388-redirect-chain-request-budget

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Closes nothing yet: #388 stays open for the reporter's measurement against a real panel.

What was wrong

LoadOptions.maxConcurrentSourceRequests was registered for the origin of the URL the host loaded (AetherEngine.swift, setHostLimit(_:for: url)) and stopped there. On the shape it exists for, an Xtream portal that 302s to the media host that counts the provider's connections, that is the wrong host:

  • the pump takes its slot against the URL it asks for, follows the 302 and streams from the target with its ticket still booked against the portal;
  • the 200/206 pins the target, so every later request is keyed there, on an origin with no ceiling and no in-flight count;
  • detourEligible therefore reads requiresSerialRequests(pinned) == false and opens a detour block as a second request while the pump streams from the same server. On a one-slot panel that is the 509, and the halving that follows starts from a peak that never included the pump.

What this does

Folds an observed redirect into one chain, kept under the source's key. Every request keyed on either end is answered by the same server, so:

  • the declared ceiling covers the chain, and requiresSerialRequests(pinned) is true from the first byte, so the detour falls back to the reposition path it already has;
  • the pump's existing ticket is already the chain's, so nothing has to be re-keyed mid-connection;
  • inflight/peakInflight describe the host the bytes actually come from.

Linked at redirectPreservingHeaders (every fetch path funnels through it, including hops no response ever pins) and again when a target is pinned.

Deliberate departure from #377: a refusal now lowers the whole chain, portal included. That rule was per-host on the reasoning that the proxy did not refuse us, which is true and no longer the point, since a request to the proxy for this source IS a request to the host that refused. A target already on a chain keeps it, so two portals on one edge host cannot spread a ceiling declared for one of them to the other.

Measured

New Issue388RedirectChainBudgetTests, including the reporter's repro sketch on loopback (a portal that 302s to a media origin that counts its own concurrency). Before: a bounded 4 MB detour block lands at the media host, peak concurrency 2. After: no second request is issued and the backward read is served by repositioning.

ThrottledOriginServer grew a concurrency counter and an optional refuseAboveConcurrency, so a test can assert what the ORIGIN saw rather than what the reader believes it issued.

Full suite: 1912 tests, 276 suites, green.

Not claimed: a replacement connection still briefly overlaps the one it replaces (the #307/#380 teardown window), so a peak of one at the server is not something the reader can promise. Not opening a second request on purpose while the pump streams is.

🤖 Generated with Claude Code

https://claude.ai/code/session_01E5UztmCZtNYPQpzdmSdmWG

Vincent Herbst and others added 2 commits August 17, 2026 07:37
…g reaches the host that serves (#388)

`LoadOptions.maxConcurrentSourceRequests` was registered for the origin of the URL the
host loaded, and stopped there. On the shape it exists for (an Xtream portal that 302s to
the media host that counts the provider's connections) that is the wrong host: the pump
followed the redirect and streamed from the target while holding a slot booked against the
portal, and the first backward read - MKV cues at the tail, a non-faststart moov, a
backward scrub - opened a detour block against a target whose books showed nothing in
flight. The panel saw two requests where the host had declared one, and the switch-off
arrived only after the refusal that ceiling was meant to save.

The host cannot route around it: it only knows the URL it loads, and resolving the
redirect before load would spend the one connection the panel allows.

So the budget folds an observed redirect into one chain, kept under the source's key.
Every request keyed on either end is answered by the same server, which makes that the
only reading that matches what the bytes do: the ceiling covers the chain, the pump's
existing ticket is already the chain's (no re-keying a ticket mid-connection), and
`requiresSerialRequests` is true at the pinned target from the first byte, so the detour
falls back to the reposition path it already has. `peakInflight` becomes the chain's, so a
later refusal halves from the concurrency the origin actually saw.

Deliberate consequence, and the one place this departs from #377: a refusal now lowers the
whole chain, portal included. That rule was kept per-host on the reasoning that the proxy
did not refuse us, which is true and no longer the point - a request to the proxy for this
source IS a request to the host that refused, because the proxy only ever answers it with
a redirect there. A target that already belongs to a chain keeps it, so two portals on one
edge host cannot spread a ceiling declared for one of them to the other.

Reported by tschuegy from reading 6.28.0 and main @3777214e. The suggested fix was to
inherit the host limit onto the pin and re-key the redirected pump's ticket; folding the
chain is the same two effects with one mechanism, and it also closes what re-keying alone
would have left (and briefly widened): requests still keyed on the source - a size probe,
a second reader's pump - overlapping with requests keyed on the pinned target at the same
server.

Measured on the reporter's repro sketch, a loopback portal that 302s to a media origin
that counts its own concurrency: before, a bounded 4 MB detour block landed at the media
host at peak concurrency 2; after, no second request is issued and the backward read is
served by repositioning. What a replacement connection overlaps with the connection it
replaces (the #307/#380 teardown window) is unchanged and is not something the reader can
promise away.

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

CI's first run of it failed on a loaded runner with `parked` false and nothing granted,
i.e. the background acquire had not happened at all: the observation window went to
libdispatch, not to the budget. `cappedSerialises` already pays for this exact trap; this
test now waits on the same two-stage signal, so a block that never got a thread reports
that instead of a budget defect nobody measured.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01E5UztmCZtNYPQpzdmSdmWG
@superuser404notfound
superuser404notfound merged commit 6e80b61 into main Aug 17, 2026
4 checks passed
@superuser404notfound
superuser404notfound deleted the fix/388-redirect-chain-request-budget branch August 17, 2026 05:52
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.

1 participant