Skip to content

A connection that dies silently while reads are satisfied from the window defers stall detection indefinitely (observed: 4.5 minutes) — the stall watchdog only ticks inside a waiting read() #309

Closed

Description

@rrgomes

On a direct-play MKV (h264 1080p, ~1.2 Mbps, persistent reader), the transport connection under activeTask died without URLSession surfacing any error (the parallel prefetch conn's error — "The network connection was lost." — appeared only when the task was later cancelled). The reader had ~15 MB windowed, so read() kept being satisfied from the window; when the app then paused, no reads ran at all. connStallTimeout is only evaluated inside the winCond.wait in the read path (AVIOReader.swift:1363), so nothing noticed the dead conn: bytesFetched sat frozen for ~4.5 minutes across pause+resume, and detection happened only after resume drained the window and a read finally blocked — one 20 s stall-wait, then the reconnect succeeded instantly (connect 2 ms, first data 40 ms) and playback caught up with a visible burst (+17 MB in one interval, 389 dropped frames rejoining the clock).

Log excerpt: pump stall at offset 54640640, reconnecting → pump conn gen=2 ended with error: cancelled → pump gen=3 first data after 40ms → slow read: 20049ms ... stallWaits=11(20047ms, 10signaled) reconnects=1.

Suggestion: arm a wall-clock delivery-gap watchdog whenever a connection is supposed to be filling (conn not ended, task not deliberately suspended, window below its fill target) rather than only while a read() waits — then a flow that dies mid-window is replaced in ~connStallTimeout regardless of consumer cadence, and a pause never masks a dead transport.

Activity

  1. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Confirmed, and the suggestion is what shipped, with one deviation and one addition. PR #325.

    The exposure was worse than a pause-shaped one. Measured against an origin that answers 206 headers and then goes silent with the socket held open: a request with timeoutInterval = 5 fails -1001 after 5.0 s, while timeoutInterval = 0, which is what the persistent path sets, was still running after 100 s even though the session configuration carries the default 60 s timeoutIntervalForRequest. A per-request 0 disables the idle timer and overrides the session value, so there was no transport-side backstop at any duration. The reader was the only detector, and it only ticked inside a waiting read(), exactly as reported.

    The watchdog is armed on wall-clock time as suggested, and it only ENDS. One pending check per connection generation: it either ends the generation or re-arms for the remaining gap, so a healthy transfer costs one timestamp comparison per connStallTimeout. Opening connections stays with the read thread, which keeps two invariants that a reconnecting watchdog would have broken: a paused player still holds no flow at all (#310), and a parked consumer cannot be driven into a timer-paced reconnect loop (#307, where a free reconnect path produced 925 generations on one offset). The gap now has a release-visible line naming it, since a frozen bytesFetched had no witness of its own.

    The second half, which the report's diagnosis does not cover, is where the burst came from. The frontier refill only ever fired for PLANNED ends (a range delivered in full, a high-water end). A generation that ended in fault was replaced only once the window reached EMPTY. So even with instant detection, the reader would still have spent all 15 MB of read-ahead before asking for a replacement, and playback would still have rejoined the clock with the +17 MB interval and the 389 dropped frames. The refill condition is now "no flow installed and the consumer has drawn down to low water" for every reason there is no flow, with the reason selecting the policy: a fault pays the failure ladder, and its backoff is a next-attempt timestamp rather than a sleep, because sleeping on the demux thread would starve the demuxer of the very read-ahead that replacing early is meant to protect.

    The trade this makes, in case it is the wrong one for a source in use here: an origin that pauses for longer than connStallTimeout mid-body while its socket stays alive now loses that connection, where a well-buffered reader used to ride the pause out. That is already the behaviour whenever the window is thin, which is the common case on a slow link, so this makes the policy consistent rather than new, and for a live source a 20 s delivery gap means the content is gone from that connection anyway. The threshold itself stays 20 s and stays out of LoadOptions, unchanged from the #272 conclusion.

    The witness for the fix is a loopback origin that stops writing mid-body without a FIN, which reproduces the reader-observable state of the report. Both parts were verified by falsification: with the watchdog disarmed the dead flow stays installed, with the runway refill disabled the replacement is asked for with 0 KB of read-ahead left. A real-media A/B through a rate-limited, delayed origin shows an identical request sequence to the current release on a 365 MB moov-at-end MP4.

    It will be in the next release. Worth a retest on the source that produced the original trace: the expected shape is a no delivery for Ns ... ending it line about connStallTimeout after delivery stops, a replacement range requested at the frontier while read-ahead is still held, and no visible catch-up burst on resume.

  2. added 3 commits that reference this issue on Aug 7, 2026
  3. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Released in 6.12.0, with the measurement on timeoutInterval = 0 written up in the notes.

  4. rrgomes commented on Aug 7, 2026

    @rrgomes
    ContributorAuthor

    Ran a released-build (6.12.0) session in the regime that produced the original trace — same low-bitrate title, ~89 minutes with two ~3.5-minute pauses. The watchdog stayed correctly silent on the healthy link (no delivery for: 0 occurrences), and nothing resembling the frozen-fetch-counter state appeared. One honest caveat on the retest you suggested: the original trace's shape — a dead flow hiding behind a full window across a pause — can no longer form at all, since 6.11.0's paused player holds no connection. Exercising the watchdog for real therefore needs a synthetic mid-play silent death (blackhole the origin flow >20 s while playing); we can run that if you'd find the confirmation useful, but the field regime that motivated the report is gone.

  5. superuser404notfound commented on Aug 7, 2026

    @superuser404notfound
    Owner

    Thanks for running it, and for the caveat, which is the right one to raise.

    The clean result is the half that mattered most here: a watchdog that fires on a healthy 89-minute low-bitrate link would be a worse defect than the one it was built for, and 0 occurrences over that duration answers that. One cross-check turns it into a stronger zero: no delivery for is emitted under the same category and at the same level as conn start gen=, both release-visible, both through the same handler. If the capture contains conn start gen= lines, the channel was live and the zero is a measured absence rather than a filtered one.

    On the caveat: correct about the shape, with the scope slightly wider than pause. The precondition the watchdog removed is that a consumer be BLOCKED on the read, not that a player be paused. A playing consumer sitting on 15 MB of read-ahead is not blocked either, and until the window drains to a read that the old path could not serve, a dead flow behind it was equally invisible. The pause is what kept the window full for minutes, which is how the field trace reached 4.5, but a mid-play silent death on a low-bitrate title holds the same undetected state for as long as the runway lasts. So the regime that produced the trace is indeed gone (6.11.0 ends the connection at high water, so a pause holds nothing that can die), while the state the fix addresses is still reachable during playback.

    The synthetic would be useful, and its shape decides which half it lands on:

    • Socket held open, no writes (SIGSTOP on the origin process, or a proxy that stops forwarding without closing) is the reader-visible state of the report and the only shape that exercises the watchdog. Expected: no delivery for <N>s at offset <X> (<K>KB read-ahead held, had delivered data); ending it about 20 s after the writes stop.
    • A packet-drop blackhole or a killed origin usually surfaces as a transport error instead, so it tests the recovery half only. Expected line there: replacing a faulted connection at offset <X> with <K>KB of read-ahead left.

    In both, the figure carrying the fix is that K. Before 6.12.0 a fault was replaced only at an EMPTY window, so the same line, had it existed, would have read 0KB, and the catch-up burst followed from that.

    One grep on the log already captured is worth it, because the recovery half is not synthetic-only: any mid-play connection fault, an ordinary transport error included, now goes through the same early replacement. If replacing a faulted connection appears anywhere in those 89 minutes, the read-ahead figure on it is field evidence for that half with no synthetic needed.

    Nothing in AVIOReader changed between 6.12.0 and 6.14.0, so that measurement stands for the current release and there is no reason to redo it on a newer build.

    Leaving this open for the synthetic.

  6. rrgomes commented on Aug 7, 2026

    @rrgomes
    ContributorAuthor

    Taking the correction — the precondition being an unblocked consumer rather than a paused player is the part we had wrong, and a playing consumer holding read-ahead is the same blind state for as long as the runway lasts. That makes the synthetic worth running rather than moot.

    Both greps on the 89-minute capture, run before building anything:

    conn start gen= appears 284 times, so the channel was live and the no delivery for zero is a measured absence.

    replacing a faulted connection appears 0 times. No transport fault occurred anywhere in the run, so the capture carries no free field evidence for the recovery half — the synthetic is the only route to both halves.

    We'll run the held-open-no-writes shape (the origin stopped without closing) so the watchdog is the thing under test, and report the read-ahead figure the line carries.

  7. rrgomes commented on Aug 8, 2026

    @rrgomes
    ContributorAuthor

    Ran the synthetic, in the held-open-no-writes shape. One Apple TV 4K (3rd gen), engine 6.14.0, the same ~1 Mbps h264 1080p title as before. The origin stand-in is your own Scripts/throttle-origin.py in whole-server mode at ~2x media rate with --head 0, and the stall is a SIGSTOP on that proxy: sockets stay open, nothing is written, nothing closes. --head 0 mattered — the default 8 MB unshaped head per body would have handed each bounded range its entire drain amount at full speed.

    Timing was the only fiddly part. Since 6.11.0 a low-bitrate title spends about half its wall clock with no connection at all, and a stall landing there tests nothing, so I waited for a re-request and froze the origin 7 s into it, with the reader holding read-ahead rather than blocked on a read.

    Both readers detected it independently, at the same instant, 20.1 s after the writes stopped:

    pump     gen=4 no delivery for 20.1s at offset 29911863 (7701KB read-ahead held, had delivered data); ending it
    prefetch gen=4 no delivery for 20.1s at offset 30147766 (7701KB read-ahead held, had delivered data); ending it
    

    That 7701KB is the report's state exactly: not a blocked consumer, a consumer with 7.7 MB in hand and roughly a minute of runway, in front of a flow that was never going to deliver again.

    The recovery half came with it in the same run, so the earlier zero on this line now has a positive control:

    pump     replacing a faulted connection at offset 29911863 with 7445KB of read-ahead left (streak=0 status=206, next attempt in 0.0s)
    prefetch replacing a faulted connection at offset 30147766 with 7445KB of read-ahead left (streak=0 status=206, next attempt in 0.0s)
    

    7445KB is the figure carrying the fix — pre-6.12.0 that replacement waited for an empty window, so the same line would have read 0KB and the catch-up burst followed from there.

    One detail worth recording because it exercises the path further than intended: I held the freeze for 26 s, so the replacement connections were themselves requested while the origin was still frozen. gen=5 went out at 00:33:17.9 and 00:33:19.7, and the response headers landed at 00:33:22.9 — 3189 ms and 5001 ms — which is the moment the origin thawed. The replacement waits on a dead origin rather than spinning against it.

    Playback was never disturbed. 26 s of outage against ~58 s of held runway: no player error, no rebuffer, avBufAhead flat at 5.5 s across the window, and by 00:33:47 the pump was back on its ordinary 16 MB high-water cycle. Nothing left open on our side.

    Nothing in AVIOReader changed between 6.14.0 and 6.15.0, which went out while this run was under way, so the measurement stands for the current release.

  8. superuser404notfound commented on Aug 8, 2026

    @superuser404notfound
    Owner

    Both halves are confirmed, and the run goes past what the retest was asked for. Thanks for building it, and for the --head 0 catch in particular: with the default head, each bounded range gets its drain amount unshaped, so the measurement would have been of the proxy rather than of the reader.

    The figures are internally consistent with the code, which is what makes them evidence rather than log lines.

    • 7701KB at detection and 7445KB at replacement differ by 256KB, which is exactly one AVIO buffer. So the replacement was issued on the FIRST read after the generation ended, not several reads later.
    • Both figures sit just under winLowWater (8 MB), the gate the frontier refill waits on, so the run also shows the gate deferring nothing here: the 20 s of drain during detection had already carried the window across it.
    • streak=0 status=206 is the substantive one. The streak resets when a generation delivered at least minReconnectProgress (512 KB) before it died, and backoffDelay(0) is 0, which is where the next attempt in 0.0s comes from. That is the code distinguishing a connection that was healthy until it stopped from a dead-on-arrival one, which would have carried a non-zero streak, a ladder delay, and the reduced never-productive budget.
    • 20.1 s on both readers is two watchdogs, one per reader per generation, not one shared timer. Independent agreement to a tenth of a second is the arming being wall-clock, as intended.

    The detail called "further than intended" is the path worth having on record. A replacement generation is armed with the same watchdog at connection start, and its gap is measured from connStartedAt rather than from first delivery. So had the freeze outlasted the replacement by another 20 s, gen=5 would have ended itself with a never delivered data line instead of hanging. That is load-bearing rather than incidental: the persistent path sets timeoutInterval = 0, which disables the idle timer and overrides the session's 60 s, so without that arming a replacement issued into a still-dead origin would inherit the exact unbounded wait the report opened on. The 3189 ms and 5001 ms are the below-threshold case of it, and they answer the question that matters: waiting, not spinning.

    One limit the run does not measure, so its headline figure should not travel as a general one. Detection is 20 s from the last delivery, and what that costs is a function of bitrate. At ~1 Mbps it cost about 2.5 MB of runway, which is the whole difference between the freeze point and the 7701KB, and 7.7 MB of read-ahead survives it. On a title where 20 s of drain exceeds the 16 MB window, the window is empty by the time the watchdog fires, and the replacement is issued into an empty window. It is still strictly earlier than before the fix, because the blocked-read forward wait only starts its own 20 s once the consumer is actually blocked, so the old path detected at 20 s after the window emptied rather than 20 s after delivery stopped. The read-ahead preserved, though, goes to zero as bitrate rises. 7.7 MB is a low-bitrate figure.

    The timing note is worth recording too: half the wall clock with no connection at all is 6.11.0 working, and it makes a mid-play stall genuinely awkward to land. Freezing 7 s into a re-request, with the reader holding runway rather than blocked, is the right window and it is the state the report described.

    AVIOReader.swift is unchanged between 6.14.0 and 6.15.1, so the measurement carries to the current release.

    Closing this. Both halves now have a positive control with a number on it, and the replacing a faulted connection line has its first non-synthetic-shaped witness of what K is worth.

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