Skip to content

Commit 250ab82

Browse files
EvertEtalanjhughes
authored andcommitted
Fix possible deadlock in dispatchViewUpdates (#43643)
Summary: In AppAndFlow/react-native-safe-area-context#448 it was noticed that from 0.72 (bc766ec #35889) it was possible to get a deadlock in dispatchViewUpdates. ([More details](AppAndFlow/react-native-safe-area-context#448 (comment))) This deadlock resulted in a laggy experience for all users using https://github.com/th3rdwave/react-native-safe-area-context/ on Android. To avoid this problem, the author of the original fix [proposed](AppAndFlow/react-native-safe-area-context#448 (comment)) this solution which was tested by Discord and many other users. It would be great to have this backported to 0.72 and 0.73 because of the large userbase using react-native-safe-area-context since it's recommended by expo and react-navigation. <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: [ANDROID] [FIXED] - Fixed possible deadlock in dispatchViewUpdates For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [ANDROID] [FIXED] - Fixed possible deadlock in dispatchViewUpdates Pull Request resolved: #43643 Test Plan: The original memory leak remains fixed, and can be verified in https://github.com/feiyin0719/RNMemoryLeakAndroid. To verify the deadlock is gone, every app using https://github.com/th3rdwave/react-native-safe-area-context will work smoothly and not log any excessive `Timed out waiting for layout` (https://github.com/17Amir17/SafeAreaContext) Reviewed By: arushikesarwani94 Differential Revision: D55339059 Pulled By: zeyap fbshipit-source-id: c067997364fbec734510ce99b9994e89d044384a
1 parent d5648f8 commit 250ab82

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void removeRootView(int rootViewTag) {
175175
*
176176
* @return The num of root view
177177
*/
178-
private int getRootViewNum() {
178+
public int getRootViewNum() {
179179
return mOperationsQueue.getNativeViewHierarchyManager().getRootViewNum();
180180
}
181181

@@ -608,12 +608,6 @@ public void measureLayoutRelativeToParent(
608608

609609
/** Invoked at the end of the transaction to commit any updates to the node hierarchy. */
610610
public void dispatchViewUpdates(int batchId) {
611-
if (getRootViewNum() <= 0) {
612-
// If there are no RootViews registered, there will be no View updates to dispatch.
613-
// This is a hack to prevent this from being called when Fabric is used everywhere.
614-
// This should no longer be necessary in Bridgeless Mode.
615-
return;
616-
}
617611
SystraceMessage.beginSection(
618612
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "UIImplementation.dispatchViewUpdates")
619613
.arg("batchId", batchId)

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,12 @@ public void onBatchComplete() {
719719
listener.willDispatchViewUpdates(this);
720720
}
721721
try {
722-
mUIImplementation.dispatchViewUpdates(batchId);
722+
// If there are no RootViews registered, there will be no View updates to dispatch.
723+
// This is a hack to prevent this from being called when Fabric is used everywhere.
724+
// This should no longer be necessary in Bridgeless Mode.
725+
if (mUIImplementation.getRootViewNum() > 0) {
726+
mUIImplementation.dispatchViewUpdates(batchId);
727+
}
723728
} finally {
724729
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
725730
}

0 commit comments

Comments
 (0)