Skip to content

fix(io): drain the reader's autorelease pool per read (#243) - #245

Merged
superuser404notfound merged 3 commits into
mainfrom
fix/issue243-reader-autorelease-retention
Jul 29, 2026
Merged

superuser404notfound merged 3 commits into
mainfrom
fix/issue243-reader-autorelease-retention

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Fixes the unbounded memory growth bitxeno reported when playing a remote Blu-ray ISO over HTTP (#243).

Root cause

HTTPDiscIOReader is pull-based: FFmpeg's read callback runs the range GET synchronously on the demux pump thread, and that thread spends the whole session inside one dispatch block, so nothing ever drains its autorelease pool. Every response body bridged out of the completion handler is stranded there until playback ends: up to 8 MB per request at the top of the adaptive window, several requests a second, once per reader fork (main demuxer, subtitle side demuxer, forward prefetcher). That is the ~30 MB/s of mallocMB growth in the report. RSS looks flat because the pages are never touched again and go to the compressor, which is why vmCmp and physFP are the tell.

The reporter's diagnosis and suggested fix were correct. Two things came out of verifying them.

The local twin has the same defect. FileIOReader.read hands back an NSData-backed Data from FileHandle.read on the same thread, so a local disc image leaks one byte per byte played. SMBIOReader copies out of a dispatch_data-backed body on that thread too and gets the same wrapper.

A per-request URLSession is not a fix. Measured against a local range origin, 480 MB fetched:

variant malloc in use
shared session, completion handler, no pool +968 MB
session per request + invalidate, no pool +973 MB
shared session, completion handler, pool per request 0 MB
shared session, delegate-based, no pool 0 MB

That retires the older reading of the AVIOReader leak (the persistentSession comment) as URLSession retaining completed completion-handler bodies until invalidation. The owner was always the caller thread's pool; delegate-based delivery fixed that path by keeping the body off it.

Changes

  • HTTPDiscIOReader.rangeGet drains per range request (covers probeSize and every retry).
  • FileIOReader.read and SMBIOReader.read drain per read.
  • discFetchedMB in the 30 s memprobe, printed only on a disc-image session and session-scoped like t=. The disc pull path had no byte counter at all (avioFetchedMB covers the AVIOReader path only), so this report had to be argued from arithmetic while every engine-tracked pool read flat.

Tests

Issue243ReaderRetentionTests reads 128 MB through each reader inside a pool the test owns and asserts that draining it frees ~nothing. Reverting either wrapper frees 134.4 MB and 135.1 MB there, so the tests fail on the pre-fix readers. Measuring the drain rather than the loop keeps them immune to allocations from tests running in parallel: concurrent allocation can only shrink the observed drop.

Full suite green (1255 tests), plus swift build -Xswiftc -strict-concurrency=complete and a tvOS Simulator build.

🤖 Generated with Claude Code

https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N

Vincent Herbst and others added 3 commits July 29, 2026 09:07
The disc readers are driven from FFmpeg's read callback, i.e. from a demux
pump thread that spends the whole session inside one dispatch block and
therefore never drains its autorelease pool. Every body they bridged out on
that thread was stranded until playback ended: up to 8 MB per range request,
several a second, once per reader fork (main demuxer, subtitle side demuxer,
forward prefetcher), which is the ~30 MB/s of mallocMB growth reported on a
remote UHD ISO. RSS stays flat because the pages are never touched again and
go to the compressor, so the tell is vmCmp and physFP, and the process is
eventually jetsammed.

HTTPDiscIOReader now drains per range request and FileIOReader per read (same
defect on the local twin, one leaked byte per byte played), and SMBIOReader
gets the same wrapper around its copy out of the SMB body.

Measured against a local range origin, 480 MB fetched:

  shared session, no pool        +968 MB in use   (the defect)
  session per request, no pool   +973 MB in use   (fixes nothing)
  shared session, pool           0 MB
  delegate-based, no pool        0 MB

The per-request-session line matters beyond this fix: it retires the older
reading of the AVIOReader leak as URLSession retaining completed
completion-handler bodies until invalidation. The owner was always the caller
thread's pool, and delegate-based delivery fixed that path by keeping the body
off it.

The regression tests read 128 MB through each reader inside a pool the test
owns and assert that draining it frees ~nothing; reverting either wrapper
frees 134.4 MB and 135.1 MB there.

Also adds discFetchedMB to the memprobe. The disc pull path had no byte
counter at all, so this report had to be argued from arithmetic: every
engine-tracked pool reads flat while the reader forks pull tens of MB/s.

Reported by bitxeno (#243).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
…urement

Both comments explained the delegate-based fetch by a task-pool accumulation
inside URLSession. A session per request leaks identically to a shared one
(+973 MB vs +968 MB per 480 MB fetched), so the retention was never the
session's: it is the never-draining autorelease pool of the thread that
bridges the body out. Delegate-based delivery works here because the body is
released on the delegate queue and never reaches that pool.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
@superuser404notfound
superuser404notfound merged commit 7d6e6cf into main Jul 29, 2026
3 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue243-reader-autorelease-retention branch July 29, 2026 07:18
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