Skip to content

Backpressure: end the connection at high water instead of suspending the task - #317

Merged
superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
rrgomes:fix/310-backpressure-dormancy
Aug 7, 2026
Merged

superuser404notfound merged 1 commit into
superuser404notfound:mainfrom
rrgomes:fix/310-backpressure-dormancy

Conversation

@rrgomes

@rrgomes rrgomes commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Closes #310.

What this changes. The persistent reader no longer suspends its data task at winHighWater. It ends the connection — the mechanism the winHardCap escape hatch already used — and the low-water frontier refill re-requests exactly where delivery stopped, through the same path a completed range uses. Nothing is discarded and nothing is re-fetched; the reader now either has an actively delivering connection or no connection at all, and a stalled or paused consumer holds no flow whatsoever.

Why. Per #310: a task left suspended holding an undelivered window keeps a dormant established flow whose closed receive window sits unread for as long as the consumer takes to drain — ~100 s per cycle at 1 Mbps, indefinitely while paused — and on tvOS that dormant state correlates, dose-response by media bitrate, with 10-80 s episodes in which every Network.framework flow in the app process goes deaf at once. And by your own #220 measurement, suspend() was never contractual flow control (911 MB arrived after a suspend) — the hard cap existed precisely because the suspend could fail to take. Ending at high water removes the pathological state instead of bounding it.

Field verification. Two Apple TV 4K (3rd gen), tvOS 26, 6.7.0 plus this change, a 71-minute synchronized-playback session on the worst-case title from #310's dose-response table (h264 1080p, ~1 Mbps — the regime that previously produced ~9-11 episodes in 80 minutes): zero episodes, zero WebSocket receive errors, zero connection-setup timeouts. ~110 end-and-rerequest cycles per device (one per ~39 s; pump and subtitle-prefetch readers both cycling); the low-water refill won every cycle — the empty-window backstop never fired, so a boundary never became a stall. Two deliberate 3-minute pauses each held zero connections and resumed cleanly. Observed cost: one extra range request per drain cycle, ~2 ms connects on LAN.

What rides along (separable if you'd rather take them differently):

The core is the high-water end plus the low-water refill; everything else is negotiable.

…the task

A data task suspended for window backpressure holds a dormant established
flow: its unread receive window closes and the connection then sits idle
for as long as the consumer takes to drain from high water to low —
~100 s per cycle at 1 Mbps media rate, indefinitely while paused. On
user-space-networking OSes (tvOS/iOS, where TCP for Network.framework
flows runs inside the app process) that dormant state correlates,
dose-response by media bitrate, with 10-80 s episodes in which every nw
flow in the process goes deaf at once — established WebSockets time out
unACKed and no new nw handshake completes, while raw BSD sockets from
the same process keep exchanging normally. Field dose-response and wire
captures are in issue superuser404notfound#310.

suspend() was also never contractual flow control here: superuser404notfound#220 measured
911 MB arriving after a suspend, which is why the hard cap already ended
the connection when the suspend demonstrably had not taken. This makes
that end the one and only backpressure mechanism, at winHighWater:

- appendPersistentData cancels the task past high water and marks the
  end with connEndedByBackpressure; nothing is discarded and the next
  request starts at the delivered frontier, so nothing is re-fetched.
- The low-water frontier refill now covers backpressure ends as well as
  completed ranges, so the reconnect is in flight before the window runs
  dry; the empty-window re-request remains as the backstop.
- The reader therefore either has an actively delivering connection or
  no connection at all. A stalled or paused consumer holds no flow, so
  there is no parked state to go dormant. Cost: one extra range request
  per drain cycle (connects measure ~2 ms on LAN).
- winHardCap is subsumed: the window is now bounded by construction at
  high water plus one delivery's in-flight overshoot, well under the
  cap and the realloc-doubling peak it was sized against.
- The suspend/resume machinery (flag, balance-before-cancel paths,
  post-suspend delivery accounting) is removed; the deliberate cancel's
  completion is no longer logged as a transport error.
- windowDiagnostics reports parked (backpressure-ended, refill pending)
  in place of suspended/postSuspendBytes, and the memprobe fragment says
  Parked= instead of Susp=/PostMB=.
- Backpressure tests pin the new contract (stalled consumer: no live
  connection, parked flag, no refill churn, bounded origin bytes; refill
  liveness and monotonic frontier after a drain). The superuser404notfound#295 test's
  no-overlap proxy is retightened to strictly increasing request starts,
  which is the invariant that still holds now that a cancelled range's
  undelivered tail is legitimately re-requested.
@superuser404notfound

Copy link
Copy Markdown
Owner

Merged in 1e45bf7, thank you. The dose-response table in #310 is what carried this: three bitrates, the dormant stretch predicted from window size over media rate, and the episode count tracking it. That is a much stronger case than the usual "this looks suspicious", and the 71-minute two-device run on the worst-case title closes it from the other side.

Taking the whole thing, not just the core: the hard cap and the suspend machinery going away is the point, since a mechanism that is only sometimes honoured is worse than no mechanism plus an honest reconnect. The Parked= rename is fine, pumpIOWindow and ioWindowDiagnostics are internal and nothing outside the engine reads that tuple.

Two things worth knowing:

Full suite green on main, 1544 tests.

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