Skip to content

fix(io): notice a silently dead connection on wall-clock time (#309) - #325

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/309-delivery-gap-watchdog
Aug 7, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/309-delivery-gap-watchdog

Conversation

@superuser404notfound

@superuser404notfound superuser404notfound commented Aug 7, 2026 •

Copy link
Copy Markdown
Owner

Addresses #309. Two defects, and only the first one is in the report.

Detection: the watchdog only ticked inside a waiting read()

connStallTimeout was evaluated in exactly one place, the read loop's forward wait (AVIOReader.swift:1351 before this change), so a transport that died while the sliding window could still serve reads was noticed only once a consumer happened to block on it. The transport does not cover that gap either: the persistent request is issued with request.timeoutInterval = 0 on purpose, so the reader is the only detector there is.

A delivery-gap watchdog now ends a generation that has an installed transfer and no delivery for connStallTimeout, whether or not a read is waiting on it. Same threshold, same action as the read-side wait; what is removed is the accidental exemption for a well-buffered reader. One pending closure per generation on a shared serial queue: it either ends the connection or re-arms itself for the remaining gap, so a healthy transfer costs one timestamp comparison per connStallTimeout and an ended generation arms nothing.

It only ENDS, it never reconnects. Opening connections stays with the read thread, so a paused player still holds no flow (the #310 invariant) and a parked consumer cannot be turned into a timer-paced reconnect loop (the #307 failure mode). The gap is named in a release-visible line, which is what the field case had no witness for.

The threshold stays 20 s and stays out of LoadOptions: #272 measured that a shorter one is worse under CPU starvation, and that conclusion is unchanged. The new hook is a test clock only.

Recovery: a faulted connection waited for an EMPTY window

This is where the measured harm was, and detection alone would not have moved it. The frontier refill fired only for PLANNED ends (connEndedAtRangeEnd || connEndedByBackpressure); a generation that ended in fault was replaced only once available == 0. The reader therefore spent its entire read-ahead before asking for a replacement, which is why playback rejoined the clock with a burst (+17 MB in one interval, 389 dropped frames in the report) rather than simply carrying on.

The refill condition is now "no flow installed and the consumer has drawn down to low water", for every reason there is no flow. The reason still selects the policy: a planned end costs nothing, a fault pays the failure ladder (status accounting, pin invalidation, bounded give-up), with two deliberate differences from the empty-window ladder:

  • No backoff sleep. It runs on the demux thread with megabytes still resident, and sleeping there would starve the demuxer of exactly the read-ahead that replacing early exists to protect. The wait is a next-attempt timestamp, so reads keep being served at full speed between attempts.
  • It never fails the read. A window that can still serve must not kill a session holding seconds of playback. At the cap it stops attempting and leaves termination to the empty-window ladder, where it has always been. It also emits no .reconnecting phase while the window serves: playback is uninterrupted there, and a phase flapping per read would describe bookkeeping rather than what the viewer sees.

The trade this makes, stated plainly

An origin that pauses for more 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 engine's policy whenever the window is thin (which is the common case on a slow link), so this makes it consistent rather than new; and for a live source a 20 s delivery gap means the content is gone from that connection anyway. The replacement is asked for at low water rather than immediately, so a hiccup shorter than the remaining runway costs one range request and no visible interruption.

Verification

  • Issue309SilentTransportDeathTests, against a loopback origin that stops writing mid-body without closing the socket and without a FIN, which is the reader-observable shape of the field case (ThrottledOriginServer.Directive.serveThenGoSilent). Four cases: a flow that dies mid-window is ended with no read blocking on it; a generation that never sees a first byte is ended too; the replacement is requested while read-ahead remains; and a backpressure-parked reader is NOT treated as a dead flow.

  • Falsification, both parts separately: with the watchdog disarmed the dead flow stays installed after 6x the threshold; with the runway refill disabled the replacement is asked for with 0 KB of read-ahead left. The regression case passes in both arms.

  • Full suite: 1548 tests, all passing.

  • Real media, aetherctl play --seconds 40 on a 365 MB moov-at-end MP4 through a Range-logging origin paced at 4 MB/s with a 60 ms header delay: identical request sequence and cadence to main (six requests, same offsets, ~9 s apart), zero watchdog lines, zero faults.

  • Measured, with a positive control, that the transport really does not cover this: against an
    origin that answers 206 headers and then goes silent, a request with timeoutInterval = 5
    fails -1001 after 5.0 s, while timeoutInterval = 0 (what the persistent path uses) was
    still running after 100 s even though the session config carries the default 60 s
    timeoutIntervalForRequest. So a per-request 0 disables the idle timer and overrides the
    session value, and the pre-fix exposure was unbounded rather than capped at 60 s.

The runway assertion is read from the ORIGIN's thread at the moment it receives the replacement request, not from a wall clock: a loopback origin answers in microseconds, so timing could not express it.

🤖 Generated with Claude Code

`connStallTimeout` was evaluated in exactly one place, the read loop's forward
wait, so a transport that died while the sliding window could still serve reads
was detected only once a consumer happened to block on it. In the field report
`bytesFetched` sat frozen for 4.5 minutes across a pause. The transport does not
cover that gap either: the persistent request runs with `timeoutInterval = 0` on
purpose, so the reader is the only detector there is.

A delivery-gap watchdog now ends a generation that has an installed transfer and
no delivery for `connStallTimeout`, whether or not a read is waiting on it, and
names the gap in the log. Same threshold and same action as the read-side wait:
the only thing removed is the accidental exemption for a well-buffered reader.
It only ENDS. Opening connections stays with the read thread, so a paused player
still holds no flow (#310) and a parked consumer cannot be driven into a
timer-paced reconnect loop (#307).

Detection alone would not have fixed what the reporter measured. The frontier
refill fired only for PLANNED ends, so a faulted generation was replaced only
once the window hit empty: the reader spent its whole read-ahead before asking
for a replacement, and playback rejoined the clock with a burst (+17 MB in one
interval, 389 dropped frames). Any reason for having no flow now refills at low
water, with the reason selecting the policy. A fault pays the failure ladder
(status accounting, pin invalidation, bounded give-up) with its backoff as a
next-attempt timestamp rather than a sleep, because sleeping on the demux thread
would starve the demuxer of the very read-ahead the early replacement protects.
It emits no `.reconnecting` phase while the window still serves, and it never
fails the read; termination stays with the empty-window ladder.

Tests: a loopback origin that stops writing mid-body without a FIN, which is the
reader-observable shape of the field case. Both parts verified by falsification
(watchdog disabled: the dead flow stays installed; runway refill disabled: the
replacement is asked for with 0 KB of read-ahead left). Real-media A/B through a
rate-limited, delayed origin: identical request sequence to main on a 365 MB
moov-at-end MP4, zero watchdog lines.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01HX5zV3Fcf7Nzq4DYGdXvQE
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