Repository navigation
fix(reader): bound a chunk-fetch body by the request, not the declared length (#255) - #257
Merged
Merged
Conversation
…d length (#255) `ChunkFetchDelegate` reserved whatever the response declared: `body.reserveCapacity(Int(http.expectedContentLength))`, with no ceiling. One of the requests that reaches that line is the HEAD size probe, which declares the entire source and delivers no body at all, so opening a 12.4 GB progressive HTTP source asked malloc for 12.4 GB in order to buffer nothing. On a 6 GB iPhone the allocation returned NULL and `__DataStorage.init(capacity:)` force-unwrapped it: EXC_BREAKPOINT on the URLSession delegate queue before a frame played, and a trap rather than a throw, so a host app could neither catch it nor degrade. The probe ladder makes the surface wider than the reported case. The HEAD fallback is launched 0.75 s after the range probes and runs whenever they have not resolved a size yet, so every non-prefetch open (still extraction, one-shot seekable) against a slow origin could take the crash, not only origins that reject Range outright. Both halves now come from the request instead of the response: - The reservation is the declared length clamped to what the request itself can deliver: the span of a bounded `Range: bytes=a-b`, nothing for a HEAD, or a flat 8 MB ceiling when the request is open-ended. - The body is truncated at that same span. An origin that ignores `Range` and answers a bounded chunk request with `200` plus the whole source's length used to be buffered in full and only then rejected; the reader now keeps the prefix it asked for and hangs up. Measured against a scripted origin: pre-fix both paths reserved 13,304,315,904 bytes up front, and the Range-ignoring case pulled 134 MB over the wire before failing the read outright. Post-fix the peak reservation is the chunk span, the origin writes single-digit MB, and the truncated chunk serves the read. Reported by dlev02 with a symbolicated crash report and register state. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N
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.
What
ChunkFetchDelegatereserved whatever the response declared, with no ceiling:One of the requests that reaches that line is the HEAD size probe. A HEAD declares the entire source and delivers zero body bytes, so opening a 12.4 GB progressive HTTP source asked malloc for 12.4 GB in order to buffer nothing. On a 6 GB iPhone the allocation returned NULL and
__DataStorage.init(capacity:)force-unwrapped it:EXC_BREAKPOINTon the URLSession delegate queue before a frame played. It traps rather than throwing, so a host app can neither catch it nor degrade.The surface is wider than the reported case.
probeFileSize()launches the HEAD fallback 0.75 s after the range probes and runs it whenever they have not resolved a size yet, so any non-prefetch open (still extraction, one-shot seekable) against a slow origin could take the crash, not only origins that reject Range outright.Fix
Both halves now come from the request instead of the response:
Range: bytes=a-b, nothing at all for a HEAD, a flat 8 MB ceiling when the request is open-ended.Datagrows on demand, so the cap costs at most one reallocation.Rangeand answers a bounded chunk request with200plus the whole source's length used to be buffered in full and only then rejected by the caller's status check; the reader now keeps the prefix it asked for and hangs up. The truncating cancel is not read as a failed fetch.HTTPDiscIOReaderis not affected: it never reserves from a declared length, and its init requires working Range support.Verification
Issue255BodyReserveTests: the arithmetic without a network, plus the wiring against a scripted origin (one that resolves no size from GETs and answers HEAD with the 12.4 GB length, one that ignoresRangeand answers 200 with the full length while streaming 64 MB).Red before the fix, on the same tests:
swift build --build-tests -Xswiftc -strict-concurrency=completecleanxcodebuild -scheme AetherEngine -destination 'generic/platform=tvOS Simulator'succeededReported by @dlev02 with a symbolicated crash report and register state (
x0NULL,x19/x2= 0x318ffc000 = the source's full length).Refs #255
🤖 Generated with Claude Code
https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N