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

Ouverte
#59,804 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 2 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
66/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
ios, objective-c, react-native
Domaine
mobile

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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.

Langage dominant
C++
Étoiles
127k
Forks
25.5k
Merge moyen
3 j 22 h
PR mergées (30 j)
235

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de react/react-native

Toutes les issues de react/react-native

Issues similaires

Plus d'issues C++

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.