Repository navigation
fix(io): drain the reader's autorelease pool per read (#243) - #245
Merged
superuser404notfound merged 3 commits intoJul 29, 2026
Merged
Conversation
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
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
superuser404notfound
deleted the
fix/issue243-reader-autorelease-retention
branch
July 29, 2026 07:18
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 the unbounded memory growth bitxeno reported when playing a remote Blu-ray ISO over HTTP (#243).
Root cause
HTTPDiscIOReaderis 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 ofmallocMBgrowth in the report. RSS looks flat because the pages are never touched again and go to the compressor, which is whyvmCmpandphysFPare 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.readhands back an NSData-backedDatafromFileHandle.readon the same thread, so a local disc image leaks one byte per byte played.SMBIOReadercopies out of a dispatch_data-backed body on that thread too and gets the same wrapper.A per-request
URLSessionis not a fix. Measured against a local range origin, 480 MB fetched:That retires the older reading of the AVIOReader leak (the
persistentSessioncomment) 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.rangeGetdrains per range request (coversprobeSizeand every retry).FileIOReader.readandSMBIOReader.readdrain per read.discFetchedMBin the 30 s memprobe, printed only on a disc-image session and session-scoped liket=. The disc pull path had no byte counter at all (avioFetchedMBcovers the AVIOReader path only), so this report had to be argued from arithmetic while every engine-tracked pool read flat.Tests
Issue243ReaderRetentionTestsreads 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=completeand a tvOS Simulator build.🤖 Generated with Claude Code
https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N