From f00795dc90d05180014eeea4b3215b0166a90692 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Fri, 17 Jul 2020 15:30:56 -0700 Subject: [PATCH] Prevent NPE in measure during measure/teardown race Summary: When stopSurface is called, Fabric might be processing a commit and performing measurements even as the context is being removed from the FabricUIManager map. Just return a 0 from `measure` if we can't get a context. This prevents crashes during teardown for measured views that will never be visible anyway. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D22604716 fbshipit-source-id: 67be8d272afd35fc4c2b51b371939c5623e97f73 --- .../com/facebook/react/fabric/FabricUIManager.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index 1ab26f3349d..603089a28d2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -466,8 +466,19 @@ public class FabricUIManager implements UIManager, LifecycleEventListener { float minHeight, float maxHeight, @Nullable float[] attachmentsPositions) { + + // This could be null if teardown/navigation away from a surface on the main thread happens + // while a commit is being processed in a different thread. By contract we expect this to be + // possible at teardown, but this race should *never* happen at startup. + @Nullable ReactContext context = rootTag < 0 ? mReactApplicationContext : mReactContextForRootTag.get(rootTag); + + // Don't both measuring if we can't get a context. + if (context == null) { + return 0; + } + return mMountingManager.measure( context, componentName,