[iOS] Crash "attempt to insert nil object from objects[3]" in RCTNetworking handlers provider when a request runs after reload (BlobModule is nil)

Open
#59,804 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
66/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ios, objective-c, react-native
Domain
mobile

Research direction

Start with the default handlers provider in packages/react-native/Libraries/AppDelegate/RCTAppSetupUtils.mm and the handler lookup in packages/react-native/Libraries/Network/RCTNetworking.mm; read how the provider handles absent modules. Reproduce with the supplied reload and multipart request loop, then run the relevant iOS networking tests. Done when a request queued during reload no longer crashes if BlobModule is unavailable, while normal Blob URL handling still works.

Written by the indexing model from the issue text.

Description

Needs: Author Feedback Needs: Repro
Description

On iOS (New Architecture, bridgeless), the app can crash during a host reload with:

NSInvalidArgumentException: -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[3]

objects[3] is BlobModule in the default RCTNetworking handlers provider (RCTAppSetupUtils.mm:87):

return [@[
  [RCTHTTPRequestHandler new],
  [RCTDataRequestHandler new],
  [RCTFileRequestHandler new],
  [moduleRegistry moduleForName:"BlobModule"],
] arrayByAddingObjectsFromArray:URLRequestHandlerModules];

What happens:

  1. A reload (in our case RCTTriggerReloadCommandListeners from a CodePush restart → RCTHost _reloadWithShouldRestartSurfaces) invalidates the old RCTNetworking, which sets _handlers = nil (RCTNetworking.mm:204), and the old RCTTurboModuleManager. Once _invalidating is set, moduleForName: returns nil (RCTTurboModuleManager.mm:910).
  2. A request that was already queued on the old networking method queue still runs afterwards. prioritizedHandlers sees _handlers == nil and calls the provider again.
  3. The provider asks the dead registry for BlobModule, gets nil, and the array literal throws.

extractModuleConformingToProtocol in the same file already tolerates nil modules; only the BlobModule literal does not.

The code is unchanged in v0.88.1 and on main. We see it in production on 0.83.4 (about 85 crash events from 55 users over 5 months).

Proposed fix (we ship this as a local patch and it removes the crash in our repro):

NSMutableArray<id<RCTURLRequestHandler>> *handlers = [NSMutableArray
    arrayWithObjects:[RCTHTTPRequestHandler new], [RCTDataRequestHandler new], [RCTFileRequestHandler new], nil];
id<RCTURLRequestHandler> blobModule = [moduleRegistry moduleForName:"BlobModule"];
if (blobModule != nil) {
  [handlers addObject:blobModule];
} else {
  RCTLogWarn(@"RCTNetworking: BlobModule is nil; blob URLs have no handler");
}
[handlers addObjectsFromArray:URLRequestHandlerModules];
return handlers;
Steps to reproduce

It is a race, so it needs a loop:

  1. Release build on the iOS simulator, New Architecture on (default).
  2. In JS, keep sending multipart fetch requests whose FormData has a part with a uri (for example data:text/plain;base64,...). This starts a nested RCTNetworkTask, which widens the window. Plain POSTs did not reproduce it for us in ~140 reloads.
  3. Every ~2.5 s, trigger a host reload (we used CodePush.restartApp(false); any trigger of RCTTriggerReloadCommandListeners should hit the same path, but I only tested CodePush).
  4. Without the fix the app crashed after ~50 reloads (~150 s). With the fix: ~290 reloads, no crash.
React Native Version

0.83.4 (code unchanged in 0.88.1)

Affected Platforms

Runtime - iOS

Output of npx @react-native-community/cli info
react-native: 0.83.4
react: 19.2.4
Xcode: 27.2/27B52
iOS: hermesEnabled: true, newArchEnabled: true
Stacktrace or Logs
Crashed thread (RCTNetworking method queue):
  -[__NSPlaceholderArray initWithObjects:count:]
  +[NSArray arrayWithObjects:count:]
  RCTAppSetupDefaultModuleFromClass block (RCTAppSetupUtils.mm)   // handlers provider
  -[RCTNetworking prioritizedHandlers]
  -[RCTNetworking handlerForRequest:]
  -[RCTNetworking networkTaskWithDevToolsRequestId:request:completionBlock:]
  -[RCTNetworking sendRequest:callback:] block_invoke_2

Main thread at the same time:
  -[CodePush loadBundle]
  RCTTriggerReloadCommandListeners
  -[RCTHost _reloadWithShouldRestartSurfaces:]
MANDATORY Reproducer

I can't share the app. The steps above reproduce it on a new-arch app with a JS loop and a reload trigger. I can build a minimal public reproducer if needed.

Dominant language
C++
Stars
127k
Forks
25.5k
Avg merge
3d 22h
Merged PRs (30d)
235

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from react/react-native

All issues in react/react-native

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.