Skip to content

fix(io): bound the rate-limited keep-pin grace, then spend one re-resolve (#380) - #381

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/rate-limited-pin-repin
Aug 17, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
tschuegy:fix/rate-limited-pin-repin

Conversation

@tschuegy

Copy link
Copy Markdown
Contributor

Fixes #380.

What

A pause of a few minutes, then resume, froze VOD playback for ~60 s on a connection-capped
panel: the pinned post-redirect URL answered 509 to 20 consecutive generations (~85 s) while a
fresh redirect through the source connected in 452 ms. Three coupled defects kept the reader
grinding the dead pin until the segment layer tore the producer down from outside — the full
trace and analysis are in the issue.

How

  • The keep-pin grace is now bounded. The rule from the AVIOReader: unbounded reconnect spiral (zero backoff) when the origin refuses a range refill; pinned redirect URL never invalidated on 5xx #307 follow-up (8a8e00b0) models
    the lingering slot, which frees in seconds — and for that shape nothing changes: the pin
    survives rateLimitRepinStreak (3) paced attempts, which is the existing two-attempt tests'
    territory plus margin. A rate-limited streak that reaches the rung is the other shape (a
    pinned edge session that expired during a long idle and refuses forever), and there the pin
    is dropped for exactly one re-resolve through the source; the fresh target's 200/206 re-pins.
    Both ladders (chargeFaultedRunwayRefill and the empty-window path) share the rung; the
    hard-error rung (unproductiveReconnects >= 2) is unchanged.
  • Window-served reads no longer reset the streaks. Draining read-ahead is not network
    progress: the reset ran in the same read iteration as the faulted-refill decision, so every
    attempt against the refusing origin was charged streak=1 while the runway drained, the
    bounded give-up and the new rung were unreachable, and one served byte after an exhaustion
    restarted the whole ladder. The streaks now reset only when the current generation has
    delivered data (connFirstDataSeen, already winCond-guarded in that branch).
  • The faulted-refill pacing survives the reconnect it authorises.
    startPersistentConnection reset nextFaultedRefillAt as part of starting the very attempt
    the charge had just paced, so "next attempt in Ns" never held and attempts ran on read
    cadence. The reset moved to the two events that genuinely end a faulted lineage: first data
    (appendPersistentData) and an intentional reposition (seekReconnect). The .distantFuture
    give-up latch actually latches now, and first data re-arms it for a later, unrelated fault.

Test plan

  • swift test on macOS 26: 1867 tests green.
  • New in ResolvedURLInvalidationTests (both verified failing on the unfixed base):
    • "a 509 outliving the keep-pin grace re-resolves once through the source" — a two-server
      rig where the source mints per-request lease URLs and the CDN 509s dead leases forever.
      Fixed: heals at rateLimitRepinStreak + 1 boundary attempts with exactly one post-fault
      source resolve. Unfixed base: 7 attempts, zero resolves, read dies at the boundary.
    • "a draining window does not reset the rate-limit ladder" — a paced consumer (2 MB/s)
      draining 2 MB of runway against a permanent 509. Fixed: bounded at ≤ 7 boundary attempts,
      every pre-boundary byte served, then a clean −1. Unfixed base: 20 attempts.
  • Updated: "a permanent 509 pays the rate-limit ladder and gives up cleanly" now expects the
    bounded re-resolve (≥ 1, ≤ 4 source hits) inside the same seven attempts.
  • Unchanged and green: the two-attempt lingering-slot cases (503 and 509 keep the pin end to
    end), the hard-500 fallback, ErrorEndedReconnectTests, Issue309SilentTransportDeathTests
    (including the parked-is-not-stalled regression), the live-window suites, and the detour cap
    unit test.
  • Field shape: Apple TV 4K (tvOS 26) against an Xtream aggregator (max_connections=1) whose
    edge host expires idle sessions; device retest against that origin planned as soon as a
    release with this lands on the app's pin.

🤖 Generated with Claude Code

…olve (superuser404notfound#380)

A pause of a few minutes, then resume, froze VOD playback for ~60 s on a
connection-capped panel: the pinned post-redirect URL answered 509 to 20
consecutive generations (~85 s) while a fresh redirect through the source
connected in 452 ms. Three coupled defects kept the reader grinding the
dead pin until the segment layer tore the producer down from outside:

- The keep-pin rule for rate-limit statuses (8a8e00b, superuser404notfound#307 follow-up)
  had no bound. It models the lingering slot, which frees in seconds;
  a pinned edge session that expired during a long idle answers 509
  forever, and only a re-resolve heals it. The pin now survives
  rateLimitRepinStreak (3) paced attempts and is then dropped for
  exactly one re-resolve; the fresh target's 200/206 re-pins. Both
  ladders share the rung, and the hard-error rung is unchanged.
- The window-serve branch reset both streaks on every served read, in
  the same iteration as the faulted-refill decision, so every attempt
  against the refusing origin was charged streak=1 while the runway
  drained, and one served byte after an exhaustion restarted the whole
  ladder. The streaks now reset only when the current generation has
  delivered data (connFirstDataSeen).
- startPersistentConnection reset nextFaultedRefillAt as part of the
  very reconnect the charge had just authorised, so the announced
  pacing never held and attempts ran on read cadence. The timestamp now
  survives the reconnect and is released by first data or a seek - the
  give-up latch actually latches now too.

Test plan: swift test on macOS 26 (1867 tests green). New cases in the
resolved-URL suite: a stale-lease origin (source mints per-request lease
URLs, dead leases 509 forever) heals with exactly one source resolve at
grace+1 attempts, and a paced consumer draining 2 MB of runway keeps the
ladder charged (bounded at 7 boundary attempts, read fails only after
serving every pre-boundary byte). Both verified failing on the unfixed
base (20 unbounded attempts; read died without healing). The two-attempt
lingering-slot cases (503 and 509 keep the pin) stay green unchanged;
the permanent-509 case now expects the bounded re-resolve inside the
same seven attempts. Field shape from an Apple TV 4K (tvOS 26) against
an Xtream aggregator with max_connections=1 whose edge host expires
idle sessions.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@superuser404notfound
superuser404notfound merged commit d35d078 into superuser404notfound:main Aug 17, 2026
4 checks passed
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged as d35d078. Thank you for this one: a report that arrives with the mechanism, the two smaller defects that turn the policy gap into the full freeze, and a loopback rig that reproduces it is about as good as it gets.

Two things worth adding to the record.

Independent corroboration from #377, filed the same week

A different reporter, a different origin, 429 rather than 509, and mid-playback rather than post-pause. Their trace after retesting the #377 branch:

[AVIOReader] slow read: 32513ms at offset=0 ... reconnects=6 backoff=31968ms ... refusals=9
[AVIOReader] pump conn start gen=8 offset=0 len=32MB
[AVIOReader] Cached resolved URL host=nexus-175.neur.tb-cdn.st
[AVIOReader] pump gen=8 first data after 210ms

45 s of refusals against the pin, then 210 ms once a fresh demuxer's reader re-resolved. That is your 452 ms, on someone else's CDN. Their pinned host also changes between sessions, so those are per-session signed targets expiring on their own schedule, which is the shape your grace bound is written for. They reached your issue on their own and asked whether it was their root cause seen from the other end. It reads like it is.

What I pulled in afterwards (#386)

Reviewing the PR against main turned up that the read loop serves from memory in three places, and your fix covers one of them.

Also dropped the #if DEBUG from both pin lines. With your bounded grace, dropping the pin is a decision the ladder makes rather than a reaction to an expiry status, and a rung that only fires in the field is worth nothing if the field cannot see it.

rateLimitRepinStreak is left at 3 as you set it, and the changed expectation in permanent509TakesTheRateLimitLadder stands: a bounded number of re-resolves inside the same seven attempts is the right trade against keeping a pin that can only ever be refused.

Verified on the merge with main (your branch was 12 commits behind, and the #377 budget lands in the same file): 1886 tests, 273 suites, green.

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

Labels

None yet

Projects

None yet

2 participants