From 2bf866e4018ea72c1f1c92c806db85378c801fb7 Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Wed, 3 Feb 2021 18:40:01 -0800 Subject: [PATCH] Make NativeModules immediately initializable Summary: ## Problem: NativeModules can only be initialized after we mark them initializable on the NativeModules thread. This work is scheduled in ReactInstanceManager.setupReactContext(), after we schedule the execution of the JS bundle on the JavaScript thread in ReactInstanceManager.createReactContext(): https://www.internalfb.com/intern/diffusion/FBS/browse/master/xplat/js/react-native-github/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java?commit=9b58f58b8eb12281567b8d24d6d000e868e61a1f&lines=1256-1257%2C1331%2C1334-1335%2C1333-1334 So, if the timings don't work out, the JavaScript thread could start executing the JS bundle before the NativeModule thread makes all NativeModules initializable. In this case, those NativeModule will just appear to be null in C++/JavaScript. ## Fix This diff makes all NativeModules initializable immediately after their ModuleHolder is created. ModuleHolder.markInitializable() simply initializes initializes modules that were eagerly created. Changelog: [Android][Fixed] - Make NativeModules immediately initializable Reviewed By: JoshuaGross Differential Revision: D26233372 fbshipit-source-id: a9223ff093da5b80781169be88e6ec9516c7a29b --- .../main/java/com/facebook/react/bridge/ModuleHolder.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleHolder.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleHolder.java index 0398c85a4731b1..d674438d57517b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleHolder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ModuleHolder.java @@ -50,7 +50,6 @@ public class ModuleHolder { private @Nullable @GuardedBy("this") NativeModule mModule; // These are used to communicate phases of creation and initialization across threads - private @GuardedBy("this") boolean mInitializable; private @GuardedBy("this") boolean mIsCreating; private @GuardedBy("this") boolean mIsInitializing; @@ -89,7 +88,6 @@ public ModuleHolder(NativeModule nativeModule) { boolean shouldInitializeNow = false; NativeModule module = null; synchronized (this) { - mInitializable = true; if (mModule != null) { Assertions.assertCondition(!mIsInitializing); shouldInitializeNow = true; @@ -193,7 +191,7 @@ private NativeModule create() { boolean shouldInitializeNow = false; synchronized (this) { mModule = module; - if (mInitializable && !mIsInitializing) { + if (!mIsInitializing) { shouldInitializeNow = true; } } @@ -227,7 +225,7 @@ private void doInitialize(NativeModule module) { boolean shouldInitialize = false; // Check to see if another thread is initializing the object, if not claim the responsibility synchronized (this) { - if (mInitializable && !mIsInitializing) { + if (!mIsInitializing) { shouldInitialize = true; mIsInitializing = true; }