Skip to content

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

Open
huntie wants to merge 1 commit into
mainfrom
pr60187
Open

huntie wants to merge 1 commit into
mainfrom
pr60187

Conversation

@huntie

@huntie huntie commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

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.
  • 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 for arm64-v8a with the NDK and 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
Collaborator 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);
}

#include <react/networking/NetworkReporter.h>

#include <cstddef>
#include <mutex>

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.

Nit: should this go inside the REACT_NATIVE_DEBUGGER_ENABLED block with <unordered_map>?

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. 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