From 516b058256f42935d886dd1db9b9a0dfd3b522fc Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Mon, 1 Jul 2019 14:55:27 -0700 Subject: [PATCH] Initialize TurboModuleManager before we run JS Summary: NativeModuleRegistry is initialized in `ReactInstanceManager.createReactContext` and passed to the CatalystInstanceImpl's constructor. After we create the CatalystInstanceImpl, we then run the JS by calling `CatalystInstance.runJSBundle`. This makes it so that NativeModules are available by the time we run JS code that could require them. With TurboModules, however, the JS startes executing first, and then we initialize TurboModuleManager. This is bad because by the time JS calls `TurboModuleRegistry.getEnforcing`, TurboModuleManager may yet not be ready. So, `TurboModuleRegistry.get` will just return null for all TurboModules. This will cause TurboModuleRegistry to raise errors. I suspect this is what's causing all RN surfaces to crash with "Unexpected error occurred" in prod builds of the app for users that are in the TurboModules FB4A test. But even if it doesn't fix the error, I think this is a better way to initialize TurboModules. Reviewed By: ejanzer Differential Revision: D16064364 fbshipit-source-id: 6053c251ace1668a331110d0cc64aba9d41a4837 --- .../com/facebook/react/ReactInstanceManager.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index e507b60d2f9..3bbe584ecf9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -1158,9 +1158,16 @@ public class ReactInstanceManager { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_END); } + + reactContext.initializeWithInstance(catalystInstance); + if (mJSIModulePackage != null) { catalystInstance.addJSIModules(mJSIModulePackage .getJSIModules(reactContext, catalystInstance.getJavaScriptContextHolder())); + + if (ReactFeatureFlags.useTurboModules) { + catalystInstance.setTurboModuleManager(catalystInstance.getJSIModule(JSIModuleType.TurboModuleManager)); + } } if (mBridgeIdleDebugListener != null) { catalystInstance.addBridgeIdleDebugListener(mBridgeIdleDebugListener); @@ -1173,12 +1180,6 @@ public class ReactInstanceManager { catalystInstance.runJSBundle(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); - reactContext.initializeWithInstance(catalystInstance); - - if (ReactFeatureFlags.useTurboModules && mJSIModulePackage != null) { - catalystInstance.setTurboModuleManager(catalystInstance.getJSIModule(JSIModuleType.TurboModuleManager)); - } - return reactContext; }