Skip to content

Commit fb386fc

Browse files
mdvaccafacebook-github-bot
authored andcommitted
Fix NullPointerException caused by race condition in ReactInstanceManager.getViewManagerNames method
Summary: This diff fixes a NullPointerException that is caused when the method ReactInstanceManager.getViewManagerNames is called at the same time ReactInstanceManager is being destroyed. Following the stacktrace I noticed that this crash can only happen when RN was destroyed by another thread while this method was being executed This diff fixes the NullPointerException, but it doesn't fix the root cause race condition that cuases this bug changelog: [Android][Fixed] Fix NullPointerException caused by race condition in ReactInstanceManager.getViewManagerNames method Reviewed By: JoshuaGross Differential Revision: D29616401 fbshipit-source-id: 6ae8ecdd765d2fe3529fef3237f08b182d8ed243
1 parent 8ed4068 commit fb386fc

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

+27-25
Original file line numberDiff line numberDiff line change
@@ -918,38 +918,40 @@ public List<ViewManager> getOrCreateViewManagers(
918918

919919
public @Nullable List<String> getViewManagerNames() {
920920
Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactInstanceManager.getViewManagerNames");
921-
if (mViewManagerNames == null) {
922-
ReactApplicationContext context;
923-
synchronized (mReactContextLock) {
924-
context = (ReactApplicationContext) getCurrentReactContext();
925-
if (context == null || !context.hasActiveReactInstance()) {
926-
return null;
927-
}
921+
List<String> viewManagerNames = mViewManagerNames;
922+
if (viewManagerNames != null) {
923+
return viewManagerNames;
924+
}
925+
ReactApplicationContext context;
926+
synchronized (mReactContextLock) {
927+
context = (ReactApplicationContext) getCurrentReactContext();
928+
if (context == null || !context.hasActiveReactInstance()) {
929+
return null;
928930
}
931+
}
929932

930-
synchronized (mPackages) {
931-
if (mViewManagerNames == null) {
932-
Set<String> uniqueNames = new HashSet<>();
933-
for (ReactPackage reactPackage : mPackages) {
934-
SystraceMessage.beginSection(
935-
TRACE_TAG_REACT_JAVA_BRIDGE, "ReactInstanceManager.getViewManagerName")
936-
.arg("Package", reactPackage.getClass().getSimpleName())
937-
.flush();
938-
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
939-
List<String> names =
940-
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
941-
if (names != null) {
942-
uniqueNames.addAll(names);
943-
}
933+
synchronized (mPackages) {
934+
if (mViewManagerNames == null) {
935+
Set<String> uniqueNames = new HashSet<>();
936+
for (ReactPackage reactPackage : mPackages) {
937+
SystraceMessage.beginSection(
938+
TRACE_TAG_REACT_JAVA_BRIDGE, "ReactInstanceManager.getViewManagerName")
939+
.arg("Package", reactPackage.getClass().getSimpleName())
940+
.flush();
941+
if (reactPackage instanceof ViewManagerOnDemandReactPackage) {
942+
List<String> names =
943+
((ViewManagerOnDemandReactPackage) reactPackage).getViewManagerNames(context);
944+
if (names != null) {
945+
uniqueNames.addAll(names);
944946
}
945-
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
946947
}
947-
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
948-
mViewManagerNames = new ArrayList<>(uniqueNames);
948+
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
949949
}
950+
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
951+
mViewManagerNames = new ArrayList<>(uniqueNames);
950952
}
953+
return mViewManagerNames;
951954
}
952-
return new ArrayList<>(mViewManagerNames);
953955
}
954956

955957
/** Add a listener to be notified of react instance events. */

0 commit comments

Comments
 (0)