Skip to content

fix(demux): suspend the persistent transfer for backpressure instead of blocking the delegate (#174) - #175

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/174-avio-persistent-backpressure-suspend
Jul 21, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/174-avio-persistent-backpressure-suspend

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Fixes #174.

Root cause

The persistent reader (loopback VOD/live playback path) applied window backpressure by blocking the URLSession delegate callback in appendPersistentData until the consumer drained below winHighWater (16 MB). Blocking the delegate has no flow-control contract. Whether the connection stops reading from the socket is a transport implementation detail:

  • Plain HTTP/1.1 (verified against a loopback origin) happens to park the transfer after a few MB of internal buffering.
  • The field crash is an HTTPS origin (boringssl in the crashing stack, iPadOS): that path keeps pulling from the origin at line rate while the delegate is parked, and the undelivered body accumulates in unbounded URLSession-internal allocations (~1.8 KB malloc blocks at ~5k blocks/s in the reporter's memprobe, matching TLS-record-sized fragments).

With a realtime consumer, that buffering grows at line rate minus playback rate, goes cold, gets compressed (vmCmp climbing ~500 MB per 30 s with rss flat), and trips EXC_RESOURCE at the jetsam limit. The reporter's thread stacks show exactly this: every engine thread correctly backpressured or idle, only the network thread active mid SSL_read.

The reporter's own numbers confirm the delivery/ingest split: avioFetchedMB (bytes delivered to the window) grew ~34 MB per 30 s (playback rate, consistent with cacheMB), while mallocMB grew ~409 MB per 30 s (the origin's line rate).

Fix

Replace the blocking wait with task suspend/resume, the contractual flow control ("a task, while suspended, produces no network traffic") and the same mechanism the streaming path already uses (streamHighWater/streamLowWater):

  • appendPersistentData never blocks; it suspends the task once the window exceeds winHighWater.
  • readPersistent resumes the task when the drain crosses the new winLowWater (8 MB), and before parking in the frontier wait (a suspended task never delivers; mirrors readStreaming).
  • All reconnect/teardown paths (startPersistentConnection, markClosed, close, resolveOptimisticOpen) balance a pending suspend before cancel so the suspend count stays level.

Tests

Issue174PersistentReadBackpressureTests runs a loopback throttled origin that counts every body byte it manages to write:

  • stalled consumer parks the transfer (suspend state asserted, plus an origin-served byte bound as regression guard),
  • resume liveness: consumption after a long stall keeps delivering well past the window,
  • teardown while suspended releases the task without hanging.

Full suite: 895 tests green, -strict-concurrency=complete clean, tvOS Simulator build clean.

🤖 Generated with Claude Code

…of blocking the delegate (#174)

The persistent reader applied window backpressure by blocking the
URLSession delegate callback until the consumer drained below
winHighWater. Blocking the delegate has no flow-control contract:
whether the connection stops reading from the socket is a transport
implementation detail. Plain HTTP/1.1 happens to park after a few MB
of internal buffering, but on the TLS/H2 path the connection keeps
pulling from the origin at line rate and the undelivered body
accumulates in unbounded URLSession-internal allocations. On a fast
origin with a realtime consumer that grows at line rate minus playback
rate, goes cold, gets compressed, and trips EXC_RESOURCE at the jetsam
limit (issue #174: vmCmp climbing ~500 MB per 30 s with rss flat, the
network thread mid SSL_read while every engine thread sat correctly
backpressured).

Replace the blocking wait with task suspend/resume, the same mechanism
the streaming path already uses: appendPersistentData suspends the task
once the window exceeds winHighWater and never blocks; readPersistent
resumes it when the drain crosses winLowWater (8 MB), or before parking
in the frontier wait (a suspended task never delivers). Reconnect and
teardown paths (startPersistentConnection, markClosed, close,
resolveOptimisticOpen) balance a pending suspend before cancel so the
suspend count stays level.

Covered by Issue174PersistentReadBackpressureTests against a loopback
throttled origin: a stalled consumer parks the transfer (suspend state
plus origin-served byte bound), resume liveness after a stall, and
teardown while suspended.

Co-Authored-By: Claude <[email protected]>
@superuser404notfound
superuser404notfound merged commit 8713df6 into main Jul 21, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/174-avio-persistent-backpressure-suspend branch July 21, 2026 11:37
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.

EXC_RESOURCE memory-limit crash: native HLS pipeline keeps reading from origin while muxer is correctly backpressure-stalled

1 participant