Skip to content

maxConcurrentSourceRequests binds the origin of the loaded URL only; after a 302 the pinned target is fetched under its own, uncapped key (#377 follow-up) #388

Closed

Description

@tschuegy

Follow-up to #377, from reading 6.28.0 and main @3777214e rather than from a trace — I have not measured this against a real panel yet and can, if it helps narrow anything.

Shape

An Xtream panel that advertises max_connections=1, loaded through the portal URL, which answers the media request with a 302 to a separate media host (a redirect that in this ecosystem is the rule rather than the exception, and the media host is what actually counts the connections). The host knows the provider allows one connection and says so:

LoadOptions(maxConcurrentSourceRequests: 1)

What happens

The ceiling is registered once, for the origin key of the URL the host loaded (AetherEngine.swift:2723, setHostLimit(_:for: url), keyed on scheme+host+port). Everything after the redirect runs under the media host's key, which has no host limit:

  • At open, the tail prefetch and the optimistic bytes=0- pump both go out against the source URL (open(), AVIOReader.swift:689/694). The prefetch is correctly skipped (requiresSerialRequests(source) == true), the pump takes the source key's slot, follows the 302 and streams from the media host — with its ticket sitting on the source key for the connection's whole life (PersistentReadDelegate.originURL = the request URL it was opened with).
  • The 200/206 pins the target (persistentReceivedResponse(resolvedURL:) → recordResolvedURL), so from here requestURL() is the media host. Every later ticket is taken for request.url = that pinned URL (startPersistentConnection, syncRequest, the size probe), i.e. under the target key.
  • detourEligible asks requiresSerialRequests(requestURL()) — the target key, uncapped — so a detour block is still opened as a second request while the pump is streaming from the same host. On a one-slot panel that is the 509, and only after it does noteRefusal(for: target) bring the target down to max(1, peak/2); peakInflight there did not include the redirected pump, so the basis is whatever the detour alone reached.

Net: with the host declaring 1, the media host still sees two concurrent requests the first time a backward/parse read needs a detour block (MKV cues, a non-faststart moov, a backward scrub), and the switch-off arrives only after that refusal — once per process, which is what the declared limit was meant to save.

The reverse direction is already handled (noteRefusalWitnessed: the target's refusal stamps the source URL without throttling the proxy). It is the forward direction — the host's declared ceiling reaching the origin the bytes actually come from — that stops at the redirect. A host cannot route around it: it only knows the URL it loads, and resolving the redirect itself before load would spend the one connection the panel allows.

Suggested fix

Two independent parts, either alone helps:

  1. Inherit the host limit onto the pinned target. When a pin is recorded (recordResolvedURL, or willPerformHTTPRedirection for the persistent connection), copy the host-declared limit of the source origin to the target origin if the target has none — OriginRequestBudget.inheritHostLimit(from: source, to: target). A host-declared limit describes the provider (the whole chain), not the proxy hop; a learned limit should probably not be inherited, since it was learned about one host. Then requiresSerialRequests(pinned) is true from the first byte and the detour falls back to the reposition path it already has.
  2. Re-key the redirected pump's ticket. In didReceive response, when dataTask.currentRequest?.url's origin differs from the ticket's, move the slot to the target key (release on the source, acquire-or-count on the target). That makes inflight/peakInflight on the media host true while it is being streamed from, so the halving on a later refusal starts from the concurrency the host actually saw, and a second reader on the same target queues where it should.

Part 1 is the one that matters for the declared-limit case; part 2 only tightens the learned case.

Repro sketch

The loopback rig from #380 (leases per source) with one addition: a proxy port that 302s GET /media to the lease origin, the lease origin refusing above one in-flight request; load the proxy URL with maxConcurrentSourceRequests: 1 on a file whose parse needs one backward read (an MKV with cues at the tail). Expected: peak 1 at the lease origin, 0 refusals. Observed (from reading): the detour block is issued against the lease origin while the pump streams from it → one 509, then the learned cap.

Not claimed

Whether real panels count a parallel Range request on the same URL as a second connection is theirs to say; the ones behind #307/#380 answer 509 for it, which is why the host would like to declare the ceiling rather than learn it once per launch.

Activity

  1. added 2 commits that reference this issue on Aug 17, 2026
  2. superuser404notfound commented on Aug 17, 2026

    @superuser404notfound
    Owner

    Read correctly, all three steps, and the fix is in main as c42531f (merged in 6e80b61, PR #389). No release yet.

    What I built, and where it differs from the suggestion

    Rather than two mechanisms (inherit the host limit onto the pin, re-key the redirected pump's ticket), the budget now folds an observed redirect into ONE chain, kept under the source's origin key. Every request keyed on either end of that chain is answered by the same server, so that is the only reading that matches what the bytes do, and it produces both effects at once:

    • requiresSerialRequests(pinned) is true from the first byte, so the detour falls back to the reposition path it already has, and the tail prefetch and probe fan stay switched off past the hop as well;
    • the pump's existing ticket is already the chain's, so no ticket has to be re-keyed mid-connection, and inflight/peakInflight describe the host the bytes actually come from, which is what the halving basis needed.

    The reason I did not stop at part 1 plus a re-key is the hole part 1 leaves and part 2 briefly widens: requests still keyed on the SOURCE reach the media host too, because the portal answers them with the same 302. A size probe or a second reader's pump (the subtitle side demuxer) sits on the source key while the streaming pump sits on the pinned key, and two buckets each capped at 1 permit two at the server. Re-keying the pump's ticket also frees the source slot during the open phase, which is exactly when the probe fan may run against the source URL. Folding the chain closes both.

    Linked at redirectPreservingHeaders (every one of the five delegates funnels through it, so hops that no response ever pins are seen too, e.g. a target that answers the first request with a 509) and again when a target is pinned.

    One deliberate departure from #377, worth naming since it inverts a rule stated there: a refusal now lowers the whole chain, portal included. "The proxy did not refuse us" is true and is no longer the point, because a request to the proxy for this source IS a request to the host that refused. A target that already belongs to a chain keeps it, so two portals handing out links on one edge host cannot spread a ceiling declared for one of them to the other. A learned ceiling on a host we have never been redirected through still stays exactly where it was learned.

    Measured

    Your repro sketch, on loopback: a portal that 302s to a media origin, setHostLimit(1) on the portal URL, a read that goes backward past the retained head. Before, a bounded 4 MB detour block landed at the media host with peak concurrency 2 there; 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, green on CI.

    What I did not assert, and cannot: a replacement connection still briefly overlaps the connection it replaces, which is the teardown window #307 and #380 are about. 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.

    If a measurement is worth taking

    The confirmation that the ceiling arrived where the bytes are is one new line at the first 302, default level, demux category:

    [OriginBudget] <media host> is served through <portal>; one request budget for both (limit 1)

    The detour has no log line of its own, so the second observable is the request shape at the media host: no bounded 4 MB range while a connection is open there. aetherctl play <url> --max-concurrent-requests 1 drives the whole path against a real panel from a Mac, if that is easier than a device capture.

    Leaving this open for that. Thanks for reading the code this closely; the part-1 vs part-2 split in the report is what made the counting half easy to reason about.

  3. tschuegy commented on Aug 17, 2026

    @tschuegy
    ContributorAuthor

    Measured against the real panel, from a Mac, aetherctl play built from main @6e80b612 (#389) and from 6.28.0 for the before/after. Hosts redacted below as <portal> / <upstream> / <edge>.

    Setup

    • Source: an Xtream series episode through the aggregator portal, max_connections=1 on the account. The chain turned out to be four hops, not two: http://<portal> → 301 → https://<portal> (Cloudflare, h2 for that hop) → 302 → http://<upstream>:8080 → 302 → http://<edge>:8089. MKV, 1.93 GB, 2516 s, cues at the tail. Transport at both ends http/1.1 (connection new, one request per connection).
    • Concurrency at the edge measured client-side: netstat -anp tcp sampled at 5 Hz for ESTABLISHED sockets to the resolved addresses of every hop (URLSession's sockets are not attributable per PID via lsof on this macOS, so system-wide; nothing else on the Mac talked to those hosts). Every run: --seconds 45 --start-position 600 --seek-every 12 --seek-pattern 900,300 --seek-count 2 — a forward far seek so the pump anchors at ~900 s, then a backward far seek to 300 s while it streams, which is the read that has to choose between a detour block and a reposition.

    Observable 1: the chain line

    Three of them at the first open, one per hop, all folded under the loaded origin:

    [AVIOReader] pump tail prefetch skipped: this origin is down to one request at a time (#377)
    [AVIOReader] pump conn start gen=1 offset=0 len=32MB
    [OriginBudget] https://<portal>:- is served through http://<portal>:-; one request budget for both (limit 1)
    [OriginBudget] http://<upstream>:8080 is served through http://<portal>:-; one request budget for both (limit 1)
    [OriginBudget] http://<edge>:8089 is served through http://<portal>:-; one request budget for both (limit 1)
    [AVIOReader] Cached resolved URL host=<edge>
    

    Observable 2: the request shape at the edge

    build --max-concurrent-requests backward seek 900 → 300 served by edge peak concurrent sample instants with ≥ 2 at edge
    6.28.0 1 detour fill block=62/63/64 offset=260046848.. size=4194304, pump stays anchored at 756 MB, re-anchor after the third block 2 (for ~13 s) 59 of ~200
    main #389 1 pump conn start gen=7 offset=261231735 len=32MB (reposition) 1 0
    main #389 none tail prefetch at open, then the same three detour blocks 2 60
    6.28.0 none same as above 2 (not sampled)

    So the declared ceiling now reaches the host that serves: with 1, the detour is out of service past three redirect hops and the edge never sees a second request from this reader; with nothing declared, behaviour is unchanged (count only, tail prefetch and detours run), which is the contract.

    Two things the open phase showed that are worth having on record: the head/cues ping-pong at open (gen=2 at the tail, gen=3 at 262 KB, gen=4 at the tail again, gen=5 at 6 KB) is served by repositioning on BOTH builds regardless of the limit — the tail ranges are len=0MB and end before the backward read arrives, so activeTask == nil and the read loop reconnects before it ever reaches the detour branch. The detour-while-anchored case needs a backward read against a live pump, which is why the far-seek pattern above and not the open phase is the measurement.

    Not observed: any 429/503/509 from this panel in any run, including the peak-2 windows. This particular edge tolerated two concurrent requests for the ~13 s the detour blocks took, so on it the ceiling only ever comes from the declaration, never from a refusal — the case the host limit exists for.

    Field data on the side

    • The tail prefetch's suffix form is answered by this edge with a 206 whose Content-Range: bytes 0-65536/1925545862 starts at zero, i.e. it ignores bytes=-65536 and serves the head. The reader logs tail prefetch rejected after ~650ms: Content-Range … does not describe the 65536B asked for; not asking this origin again this session — the 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 #378 latch reading (set only by the origin's answer to the range form) is exactly right for this shape.
    • Minor: the detour does have a log line, [AVIOReader] detour fill block=<n> offset=<o> size=4194304 (resident=<k>), so a device capture can show it directly rather than only via the request shape.
    • Seek landing for the backward seek: 2472 ms (6.28.0, detour) vs 3122 ms (main, reposition), one sample each; the no-limit runs landed the same seek in 3840/4134 ms and stopWait on the producer restart varied from 3 ms to 1.6 s across all six runs independent of build, so I would not read a cost into that difference without more samples.
  4. superuser404notfound commented on Aug 17, 2026

    @superuser404notfound
    Owner

    Released in 6.29.0. Thank you for the report.

    The deliberate consequence noted in the PR ships with it: a refusal now lowers the whole chain, portal included, and 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.

  5. superuser404notfound commented on Aug 17, 2026

    @superuser404notfound
    Owner

    Measured where it had to be measured, and against a longer chain than the sketch: four hops, the first of them a 301 that only changes the scheme. Thank you for taking it to a real panel.

    Two things from that run I want on the record.

    The 301 is the hop I would not have thought to ask for. http://<portal> and https://<portal> are the same machine and two different origin keys, so without the fold a ceiling declared on the loaded URL leaks at hop one, before any media host is even in the picture. The fold hangs off the redirect delegate rather than off a status code, so 301/302/303/307/308 all end up in the same chain; the three [OriginBudget] lines are exactly that, one per hop, every one of them pointing back at the loaded origin.

    The claim that the detour has no log line of its own was wrong. It has one, and always had: [AVIOReader] detour fill block=<n> offset=<o> size=<s> (resident=<k>), default level, demux category (AVIOReader.swift:1928). Sending a reporter to the request shape when the line was in the log the whole time is the more expensive mistake of the two, so thanks for correcting it. Having both in the table is the better record anyway, since the shape at the origin is the part the reader cannot fake.

    The rest reads the way the contract intends:

    • with a limit declared, the edge sees peak 1 and none of the ~200 sample instants above one; with nothing declared, behaviour is unchanged (count only, prefetch and detours run). That is the whole intended difference.
    • the open-phase ping-pong going through reposition on both builds regardless of the limit is precisely why the wire test uses a far-seek pattern instead of the open. activeTask == nil there, so the read loop reconnects before the detour branch is reachable at all, and a test written on the open phase would have passed on a build that still had the defect.
    • no 429/503/509 in any run, including the peak-2 windows. On that edge the ceiling only ever comes from the declaration, never from a refusal, which is the case setHostLimit exists for: there is nothing to learn from, so the number has to be told.
    • the suffix-form tail prefetch answered with Content-Range: bytes 0-65536/1925545862 is a clean instance of the 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 #378 latch, set by the origin's answer to the range form rather than by a status code. An origin that silently serves the head instead of the tail is the shape that reading a status code would miss.

    On the seek landing: agreed, one sample per cell carries no cost signal, and stopWait varying from 3 ms to 1.6 s across the six runs puts the noise floor well above the 650 ms difference. If a real cost does turn up later, it is the one worth paying at limit 1, since a reposition costs a producer restart and a detour costs a second request, and on a one-slot account there is no version of the second one that works.

    Shipped in 6.29.0. Marking this done on the strength of the measurement above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions