Repository navigation
fix(demux): suspend the persistent transfer for backpressure instead of blocking the delegate (#174) - #175
Merged
superuser404notfound merged 1 commit intoJul 21, 2026
Conversation
…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
deleted the
fix/174-avio-persistent-backpressure-suspend
branch
July 21, 2026 11:37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #174.
Root cause
The persistent reader (loopback VOD/live playback path) applied window backpressure by blocking the URLSession delegate callback in
appendPersistentDatauntil the consumer drained belowwinHighWater(16 MB). Blocking the delegate has no flow-control contract. Whether the connection stops reading from the socket is a transport implementation detail: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_RESOURCEat the jetsam limit. The reporter's thread stacks show exactly this: every engine thread correctly backpressured or idle, only the network thread active midSSL_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 withcacheMB), whilemallocMBgrew ~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):appendPersistentDatanever blocks; it suspends the task once the window exceedswinHighWater.readPersistentresumes the task when the drain crosses the newwinLowWater(8 MB), and before parking in the frontier wait (a suspended task never delivers; mirrorsreadStreaming).startPersistentConnection,markClosed,close,resolveOptimisticOpen) balance a pending suspend before cancel so the suspend count stays level.Tests
Issue174PersistentReadBackpressureTestsruns a loopback throttled origin that counts every body byte it manages to write:Full suite: 895 tests green,
-strict-concurrency=completeclean, tvOS Simulator build clean.🤖 Generated with Claude Code