Repository navigation
Conversation
…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.
This branch has not been deployed
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.
Summary:
On iOS,
RCTNetworkTaskappends every incoming chunk to anNSMutableDatabuffer so it can pass the full response to its completion block. For incremental text requests (like anXMLHttpRequestwith aprogressorreadystatechangelistener),RCTNetworkingalready forwards each chunk to JS as it arrives. By the time the request finishes, that buffer is only used to reportencodedDataLengthto 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.readWithProgressforwards each decoded chunk without buffering anything.This is the same buffer behind the crash in #41651 (
NSMallocException: Failed to grow bufferin-[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
buffersResponseDataproperty toRCTNetworkTask(defaults toYES). When set toNO, the task still tracks received bytes for progress reporting, but skips appending chunks and passesnilto the completion block.RCTNetworkingnow setsbuffersResponseData = NOfor incremental text requests, unless a text response handler claims the request.encodedDataLength.Other callers like
RCTImageLoaderdon't touch this property, so their behavior is unchanged.Review notes
nildata. That's already possible today for responses with an empty body (_dataisn't allocated until the first chunk arrives), so handlers need to handlenilregardless.encodedDataLength: For incremental text requests, this now comes from the running byte count instead ofdata.length. The two are identical whenever buffering is enabled.Fixes #60249
Changelog:
[IOS] [FIXED] - Incremental text requests (e.g.
XMLHttpRequestwith aprogresslistener) no longer buffer the entire response in memory until completionTest Plan:
Added two unit tests to
RCTNetworkTaskTests:buffersResponseData = NO, the completion block receivesnilwhile progress values remain cumulative.Retained memory with a 32 MiB response held open:
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:RCTNetworkTask'sNSMutableDatabacking buffer grew to 25.0 MiB.NSMutableDataallocation was 16.5 KiB (~51 KiB total).Note
I measured heap allocations instead of memory footprint because footprint masks most of this growth. When
reallocgrows the buffer, it moves it viavm_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.