Skip to content

fix(reader): bound a chunk-fetch body by the request, not the declared length (#255) - #257

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/issue255-body-reserve-cap
Jul 30, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/issue255-body-reserve-cap

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

What

ChunkFetchDelegate reserved whatever the response declared, with no ceiling:

let len = Int(http.expectedContentLength)
if len > 0 { body.reserveCapacity(len) }

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_BREAKPOINT on 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:

  • Reservation: the declared length clamped to what the request itself can deliver. The span of a bounded Range: bytes=a-b, nothing at all for a HEAD, a flat 8 MB ceiling when the request is open-ended. Data grows on demand, so the cap costs at most one reallocation.
  • Body: 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 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.

HTTPDiscIOReader is 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 ignores Range and answers 200 with the full length while streaming 64 MB).

Red before the fix, on the same tests:

pre-fix post-fix
peak up-front reservation, HEAD probe path 13,304,315,904 B chunk span
peak up-front reservation, Range-ignoring path 13,304,315,904 B chunk span
origin body bytes written, Range-ignoring path 134 MB single-digit MB
first read off the chunk fails (-541478725) serves
  • 1315 tests, 201 suites green
  • swift build --build-tests -Xswiftc -strict-concurrency=complete clean
  • xcodebuild -scheme AetherEngine -destination 'generic/platform=tvOS Simulator' succeeded

Reported by @dlev02 with a symbolicated crash report and register state (x0 NULL, x19/x2 = 0x318ffc000 = the source's full length).

Refs #255

🤖 Generated with Claude Code

https://claude.ai/code/session_01At1agC2qU2Y2MX2BKLdT4N

…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
@superuser404notfound
superuser404notfound merged commit c5e516f into main Jul 30, 2026
3 of 6 checks passed
@superuser404notfound
superuser404notfound deleted the fix/issue255-body-reserve-cap branch July 30, 2026 08:01
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