Skip to content

[iOS] Do not keep the response of incremental text requests in RCTNet… - #60250

Open
rexxars wants to merge 2 commits into
react:mainfrom
rexxars:fix/ios-incremental-text-buffering
Open

rexxars wants to merge 2 commits into
react:mainfrom
rexxars:fix/ios-incremental-text-buffering

Conversation

@rexxars

@rexxars rexxars commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary:

On iOS, RCTNetworkTask appends every incoming chunk to an NSMutableData buffer so it can pass the full response to its completion block. For incremental text requests (like an XMLHttpRequest with a progress or readystatechange listener), RCTNetworking already forwards each chunk to JS as it arrives. By the time the request finishes, that buffer is only used to report encodedDataLength to DevTools and to feed any text response handlers. As a result, long-lived streams (like server-sent events) end up holding the entire response in memory until the connection closes.

Android doesn't have this issue: NetworkingModule.readWithProgress forwards each decoded chunk without buffering anything.

This is the same buffer behind the crash in #41651 (NSMallocException: Failed to grow buffer in -[RCTNetworkTask URLRequest:didReceiveData:]). Since #41717 catches that exception and fails the request instead, any stream that stays open long enough now dies with "Request's received data too long."

What changed

  • Added a buffersResponseData property to RCTNetworkTask (defaults to YES). When set to NO, the task still tracks received bytes for progress reporting, but skips appending chunks and passes nil to the completion block.
  • RCTNetworking now sets buffersResponseData = NO for incremental text requests, unless a text response handler claims the request.
  • The incremental data block tracks the total received bytes so DevTools still gets the right encodedDataLength.

Other callers like RCTImageLoader don't touch this property, so their behavior is unchanged.

Review notes

  • Handler timing: Checking for a text response handler now happens when the request starts, whereas the existing handler loop runs at completion. If a handler is registered mid-request and claims it, it'll receive nil data. That's already possible today for responses with an empty body (_data isn't allocated until the first chunk arrives), so handlers need to handle nil regardless.
  • encodedDataLength: For incremental text requests, this now comes from the running byte count instead of data.length. The two are identical whenever buffering is enabled.

Fixes #60249

Changelog:

[IOS] [FIXED] - Incremental text requests (e.g. XMLHttpRequest with a progress listener) no longer buffer the entire response in memory until completion

Test Plan:

Added two unit tests to RCTNetworkTaskTests:

  1. Verifies the default behavior still passes the full response to the completion block.
  2. Verifies that with buffersResponseData = NO, the completion block receives nil while progress values remain cumulative.

Retained memory with a 32 MiB response held open:

  • Before: 32 MiB retained (35.39 MiB allocated capacity).
  • After: 0 bytes retained.

RNTester repro:
Tested using the playground from this reproducer branch, which opens Wikimedia's recent changes stream as an incremental text request via Networking. Heap inspection after 5 minutes:

  • Before: 17.1 MiB received; RCTNetworkTask's NSMutableData backing buffer grew to 25.0 MiB.
  • After: 16.1 MiB received; largest NSMutableData allocation was 16.5 KiB (~51 KiB total).

Note

I measured heap allocations instead of memory footprint because footprint masks most of this growth. When realloc grows the buffer, it moves it via vm_copy, and those copied pages aren't counted toward the footprint until they're read again. In the baseline run, the footprint actually dropped twice while the buffer was still growing and reading those retained pages afterward spiked the footprint by 11.44 MiB with no new data coming in.

…workTask

RCTNetworkTask appends every chunk it receives to a buffer, so that it can give the full response to the completion block. For incremental text requests, RCTNetworking sends each chunk to JavaScript as it arrives, and the buffer is only used for the DevTools length and for text response handlers. A response that stays open, for example a stream of server-sent events, then keeps all of its data in memory until it ends.

RCTNetworkTask gets a `buffersResponseData` property, YES by default. RCTNetworking sets it to NO for incremental text requests that no text response handler handles, and counts the received bytes for the DevTools network reporter itself. Other users of RCTNetworkTask, such as RCTImageLoader, do not change.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 24, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 24, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS] RCTNetworkTask keeps the full response of incremental text requests in memory

1 participant