Skip to content

AVIOReader: unbounded reconnect spiral (zero backoff) when the origin refuses a range refill; pinned redirect URL never invalidated on 5xx #307

Closed

Description

@tschuegy

Media: 3.7 GB H.264 (avc1.640033, 3840x1920) + AAC MKV, VOD, served by an Xtream-style aggregator with max_connections=1, 302→http redirects, and no suffix-range support. Apple TV 4K, tvOS 26. AetherEngine 6.7.0, native dispatch path.

Symptom: Playback freezes ~1 min in, then dies. The log shows the reader wedged at a 32 MiB range frontier, reconnecting ~15×/s with zero backoff — 925 reconnects in 60 s — until the segment provider's forward-wait escalation tears the demuxer down (demuxer.readPacket threw: read failed (-1) → pump finished: reason=readError(-1)), after which the session is unrecoverable:

[AVIOReader] pump conn start gen=5 offset=131554695 len=32MB
[AVIOReader] pump conn gen=5 ended with error: cancelled
[AVIOReader] pump conn start gen=6 offset=131554695 len=32MB
[AVIOReader] pump conn gen=6 ended with error: cancelled
... (925 generations, same offset) ...
[AVIOReader] slow read: 60012ms at offset=131554695 detour=0(0ms,0fetch)
  stallWaits=925(59365ms,925signaled) reconnects=925 backoff=0ms gen=5->931
[HLSSegmentProducer] pump finished: reason=readError(-1)

Root cause (three stacked defects):

  1. A connection that ends in error with zero bytes delivered for its generation lands in the no-connection reposition branch (AVIOReader.swift readPersistent, the activeTask == nil, !connEndedByBackpressure, !windowCanServe guard). That branch reconnects via seekReconnect, which resets unproductiveReconnects and applies no backoff, so the ended-connection ladder (status accounting, Retry-After, backoff, .reconnecting, bounded give-up) is unreachable. The spiral is unbounded by design.
  2. The rejected response's status is never logged (only the 200-ignored-Range case is), so the storm is unexplained in the field log.
  3. The pinned post-redirect URL is only invalidated on 401/403/404/410. An aggregator whose redirect targets expire per connection answers every later range with a hard 500 from the pinned URL, and the reader never falls back to the source URL for a fresh redirect.

Bonus: once the pump dies, handleVODReadErrorExit's revive cap (#169) exhausts silently — onVODSourceFailed is only fired from the produced-nothing arm, so the host sees a forever-stalled AVPlayer with no error.

Repro: loopback origin that 302-redirects to a "CDN" which serves the first bounded range and answers 500 for every request at the next frontier (deterministic repro used for the tests in the linked PR; also reproduced with aetherctl play against such an origin).

Expected: an error-ended zero-byte connection takes the failure ladder (backoff, bounded give-up), a hard 5xx from a pinned URL drops the pin and re-resolves through the source URL, and revive-cap exhaustion surfaces onVODSourceFailed.

PR with fix + tests follows.

Activity

  1. added a commit that references this issue on Aug 7, 2026
  2. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Fixed on main: #308 (ed46b6e) plus a follow-up in 519ae26. Reopened because the merge auto-closed it; it stays open until a retest on the Apple TV against that aggregator confirms it, so please report back once a build with this is in hand.

    What the log should say on the next run against the same source:

    • [AVIOReader] ... gen=N rejected response status=500 at offset ... names the refusal that used to be silent. Every reconnect in the 925 had one of these behind it.
    • conn ended at offset ... status=500, reconnecting (streak=N retryAfter=0s) is the ladder charging the failure, with backoff between attempts, instead of the free reposition. The streak counts up to 12 and then reconnect exhausted at offset ... ends the read.
    • Dropped resolved URL cache (hard 500 from pinned URL) is the pin being released so the retry re-resolves through the portal for a fresh redirect. If those tokens really do expire per connection, this alone should let the session continue.
    • A rate-limited origin (429/503) deliberately keeps its pin: that is metering, not a dead target, and re-resolving would spend a second request on a panel that is already refusing them.

    If the source stays refusing, the session now ends with a surfaced error rather than a frozen picture: the #169 revive cap fires onVODSourceFailed, so the host gets a terminal state instead of AVPlayer parked in waitingToPlay.

    One thing worth watching in the retest, given max_connections=1: the backoff also gives the panel time to release the previous socket, so a 500 that was really "one connection already open" may now heal on the second or third attempt instead of never.

  3. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Shipped in 6.11.0. Still open for the retest against that aggregator.

  4. tschuegy commented on Aug 18, 2026

    @tschuegy
    ContributorAuthor

    Reporting back, and closing.

    The fix has been on the Apple TV since 6.12.1 (pinned 2026-08-07, now 6.32.0). Eleven days of daily VOD against the same aggregator (max_connections=1, 302→http, no suffix ranges), including multi-GB MKVs played to the end: the frontier spiral has not come back. No four-digit reconnects= in a slow read, no pump finished: reason=readError(-1), no session I had to restart by hand.

    Two of the tokens you listed did show up in the field, in the trace I captured for #380 (Apple TV 4K, tvOS 26.6, 6.30.0, series .mkv through the same portal, resumed after a 332 s pause):

    [AVIOReader] pump gen=5 rejected response status=509 at offset 84909512
    [AVIOReader] pump replacing a faulted connection at offset 84909512 with 5902KB of read-ahead left (streak=1 status=509, next attempt in 1.0s)
    [AVIOReader] pump gen=6 rejected response status=509 at offset 84909512
    [AVIOReader] pump replacing a faulted connection at offset 84909512 with 3342KB of read-ahead left (streak=2 status=509, next attempt in 2.0s)
    [AVIOReader] pump gen=7 rejected response status=509 at offset 84909512
    [AVIOReader] Dropped resolved URL cache (rate-limited x3 through pinned URL)
    [AVIOReader] pump conn start gen=8 offset=84909512 len=32MB
    [AVIOReader] pump gen=8 first data after 417ms
    

    rejected response status=… names the refusal that used to be silent — that alone is what turned this class of report from "reconnects forever, cause unknown" into something diagnosable — and the pin is dropped so the retry re-resolves through the portal, which is what heals it.

    Three things so the closure is honest about what that trace does and does not prove:

    • It is the faulted-refill arm (the window still had runway), not the zero-byte reposition branch your patch re-routed. So it confirms the shared machinery — status accounting, pacing, pin release — while the reposition fix itself I can only report negatively: nothing has wedged at a frontier since, which after eleven days on this origin is the evidence I have.
    • The refusal this panel gives at a stale frontier turned out to be 509, not the 500 from the report. The drop above therefore came from the rate-limited grace in fix(io): bound the rate-limited keep-pin grace, then spend one re-resolve (#380) #381, not from the hard-5xx rule here; the hard-5xx path has never fired in the field, because this origin does not send one. Your keep-pin follow-up (519ae26) had the class right and only needed a bound.
    • onVODSourceFailed on revive-cap exhaustion: untriggered here. Nothing has stayed refusing long enough since the fix landed. It stands on your tests, not on my device.

    And the thing you flagged for max_connections=1 is exactly what happened: the backoff gave the panel time, and the attempt after the pin drop got first data in 417 ms — it healed on the third attempt instead of never. About 12.5 s of media time, absorbed by the read-ahead, stall=0 keepUp=y, producerRestartCount unchanged. Before the fix the same shape was ~60 s of frozen picture that only ended because the producer tore everything down.

    The one thread worth keeping is the long-idle pin suggestion, which lives in #380. Thanks for the fix and the follow-up.

  5. superuser404notfound commented on Aug 18, 2026

    @superuser404notfound
    Owner

    Thank you for closing it with a measurement rather than a thumbs up, and for separating what the trace proves from what it does not. That separation is the part I would otherwise have had to guess at.

    On the reposition arm being confirmed only negatively: that is the strongest evidence this class admits from a device, since the whole symptom is an absence. The positive witness lives in the suite, and it is an odd one. With the guard disabled, an origin refusing every refill is retried with backoff and given up on does not go red, it hangs into its two minute limit, because the read never returns. A hanging test is the field shape exactly, and it is what the arm gets checked against whenever it moves.

    The 500 vs 509 correction is worth having on the record, because it means the rule this issue shipped never fired on the origin that motivated it. 509 is classified as rate-limit-shaped on purpose (isRateLimitStatus): on a connection-capped panel it is the slot still held by the connection just replaced, it frees in seconds, and re-resolving there spends the one request there is no room for. So the hard-5xx drop is unreachable on this panel by construction, and the heal came through the bounded grace instead. Same outcome, different rung, and the bound was the missing half.

    One note on the trace itself: it is 6.30.0, and on the 6.32.0 now pinned the same pause reads differently. The 332 s idle is past the pin-idle threshold from #392 (60 s without a single byte off the network), so the first 509 drops the pin instead of the third, and the re-resolve is not paced behind a backoff charged to an address it is no longer using. The 12.5 s the read-ahead absorbed should now be roughly one refusal round trip plus the re-resolve. That is the long-idle suggestion, in the cheaper form, released in 6.30.1.

    onVODSourceFailed on revive-cap exhaustion standing on tests rather than on the device: agreed, and I would rather it stayed that way.

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