Skip to content

[NetworkReporter] Fix data race on response body buffers - #60187

Merged
huntie merged 1 commit into
mainfrom
pr60187
Nov 24, 2026
Merged

huntie merged 1 commit into
mainfrom
pr60187

Conversation

@huntie

@huntie huntie commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

RCTInspectorNetworkReporter (iOS) and JInspectorNetworkReporter (Android) buffer incremental text response bodies in a global map with no lock:

  • Android: NetworkingModule reads response bodies on OkHttp's dispatcher threads, so two concurrent requests can insert into the std::unordered_map at once, and a rehash during another request's reportResponseEnd lookup is undefined behavior.
  • iOS: RCTNetworking only reaches the NSMutableDictionary from one serial queue, but the class is public, and a second reporter calling reportResponseEnd from another queue, as Expo's does from its URLSession delegate, mutates the dictionary while RCTNetworking appends to it.

Changes

  • Guard every responseBuffers access with one std::mutex on each platform.
  • Flush to storeResponseBody after releasing the lock, so it never nests with NetworkReporter's own locks.
  • Clear the Android buffer when a request fails or is cancelled, as iOS does, so a failed streamed request no longer holds its partial body for the life of the process.
  • Debug only: everything sits inside REACT_NATIVE_DEBUGGER_ENABLED, so release builds are unchanged.

Changelog:
[General][Fixed] - React Native DevTools: Fix a data race when concurrent requests buffer response bodies

Test Plan

iOS: Built an Expo app on React Native 0.89.0-rc.0, whose copy of this file is identical to main, from source with Thread Sanitizer, on an iPhone simulator (iOS 27.2). A local server streamed 40 chunked text lines per response; each round sent 60 XHR text requests through RCTNetworking and 60 expo/fetch requests at once, with the Network domain enabled over CDP.

  • ✅ lldb on the unpatched build: maybeStoreResponseBodyIncremental and RCTNetworking's reportResponseEnd ran on com.meta.react.turbomodulemanager.queue, while Expo's reportResponseEnd ran on com.apple.NSURLSession-delegate, so the race is reachable.
  • ✅ Patched build, 5 rounds (600 requests): no crash, and Network.getResponseBody returned all 40 lines for 300 of 300 XHR and 300 of 300 expo/fetch requests.
  • Thread Sanitizer reported nothing before or after; the conflicting accesses are inside uninstrumented Foundation, so it cannot see this race. A crash before the fix did not reproduce in 5 rounds.

Android: ✅ Compiled JInspectorNetworkReporter.cpp with the NDK, with and without REACT_NATIVE_DEBUGGER_ENABLED. Not run on a device.

@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 20, 2026
@huntie
huntie added this pull request to stack #60189 November 20, 2026 17:36
@huntie
huntie removed this pull request from stack #60189 November 20, 2026 18:17
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at 58182eafe25d.

@huntie
huntie added this pull request to stack #60236 November 23, 2026 16:23
@huntie huntie changed the title Fix data race on response body buffers in RCTInspectorNetworkReporter [NetworkReporter] Fix data race on response body buffers Nov 23, 2026
@huntie
huntie marked this pull request as ready for review November 23, 2026 16:23
@huntie

huntie commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@huntie-bot Pick this and the next PR to 0.89 when merged; they are independent.

@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 23, 2026

@robhogan robhogan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. One pre-existing gap while you're here (not blocking)? Android's reportRequestFailed never clears responseBuffers, unlike iOS, so a streamed text request that fails or is cancelled keeps its buffer for the life of the process. Should be a few lines under the new lock:

/* static */ void JInspectorNetworkReporter::reportRequestFailed(
jni::alias_ref<jclass> /*unused*/,
jni::alias_ref<jstring> requestId,
jboolean cancelled) {
NetworkReporter::getInstance().reportRequestFailed(
requestId->toStdString(), cancelled != 0u);
}

@huntie
huntie merged commit 5afc5d3 into main Nov 24, 2026
58 checks passed
huntie added a commit to expo/expo that referenced this pull request Nov 24, 2026
…53160)

## Summary

**Context - 1P Network Reporting in Expo**

Expo's CDP `Network` support predates React Native's first party support
in 0.83, with an Expo CLI based stack that bypasses `RCTNetworking` and
the modern CDP infra in core.

This stack aims to migrate Expo to the upstream
`InspectorNetworkReporter` stack, gaining tighter native functionality
and allowing us to drop the Expo CLI relay.

**This diff**

Follow-up to #52866 from review.

- Make `ExpoNetworkReporter` a singleton, `ExpoNetworkReporter.shared`,
as `RCTInspectorNetworkReporter` is one. Each method takes the request
id, and in-flight requests live in one dictionary behind one `Mutex`,
the core backport rather than `NSLock`, so a call out of order or after
the request has finished never reaches `RCTInspectorNetworkReporter`.
- Store text bodies through the string branch of
`maybeStoreResponseBody`, by way of a small Objective-C helper since the
header types the argument as `NSData`, with a TODO for the text variant
in react/react-native#60188. That path writes
under the core reporter's lock, unlike
`maybeStoreResponseBodyIncremental`, whose process-wide buffer
`RCTNetworking` appends to on its own queue without one.
- Store and end a finished request outside the lock, so decoding a body
up to the 1 MB preview cap never blocks other requests.
- Stop buffering once a response exceeds the 1 MB preview cap, instead
of appending every later chunk that fits.
- Rename the lifecycle methods to match
`ExpoRequestInterceptorProtocolDelegate`: `willSendRequest`,
`didReceiveResponse`, `didReceiveData`, `didFinishLoading` and
`didFailLoading`.

`reportResponseEnd` still reads and removes from that buffer without a
lock in React Native; react/react-native#60187
adds the lock upstream.
Linear:
[ENG-28824](https://linear.app/expo/issue/ENG-28824/integrate-rn-networkreporter-stack-into-core-expo-modules-ios)
(parent ENG-27596, SDK 59)

## Test Plan

Built `bare-expo` at each layer of this stack, on an iPhone simulator
(iOS 27.2) and an Android emulator (API 37), against React Native
0.89.0-rc.2, rebuilding the `expo` and `expo-image` JS at each layer.
With `EXPO_UNSTABLE_CORE_NETWORK_INSPECTOR=1` and the Network domain
enabled over CDP, `Runtime.evaluate` drove an `expo/fetch` request,
`Image.loadAsync` and `Asset.downloadAsync`:

| Layer | iOS | Android |
|---|---|---|
| #53160 (this) | ✅ Builds; nothing reported yet | — |
| #52867 | ✅ fetch: text body, initiator stack | — |
| #52868 | ✅ + image: base64 body | — |
| #52870 | ✅ + image initiator stack | — |
| #53028 | — | ✅ Builds; nothing reported yet |
| #53029 | — | ✅ fetch: text body, initiator stack |
| #53030 | — | ✅ + image: base64 body, initiator stack |
| #53031 | ✅ + asset, no body | ✅ + asset: base64 body |
| #53720, #53721 | ✅ unchanged | ✅ unchanged |

At the top of the stack on iOS:

- ✅ 30 concurrent `expo/fetch` requests all finish with their text
bodies.
- ✅ `Image.prefetch` is reported.
- ✅ With `EXPO_UNSTABLE_CORE_NETWORK_INSPECTOR` unset, fetch, image,
prefetch and asset requests each show once.

## Screenshots

No UI change.

## Checklist

<!--
Please check the appropriate items below if they apply to your diff.
-->

- [ ] I added a changeset and followed [this short
guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
gabrieldonadel pushed a commit that referenced this pull request Nov 30, 2026
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. p: Expo Partner: Expo Partner 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.

2 participants