From fc35a65a7eb63291df4826766fa4deead7cad60e Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 18 Feb 2022 11:01:00 -0800 Subject: [PATCH] moveToResumedLifecycleState must be called from UI thread Summary: I was seeing a SoftException in our apps due to this method being called as part of ReactInstanceManager's init. It seems inconsistent that we would call this from a background thread when all other `onHost*` methods are marked as confined to the UI thread. Changelog: [Internal] Reviewed By: ryancat Differential Revision: D34340210 fbshipit-source-id: 0104eda66b2ca6bb315e64e806e9a30409d0d45c --- .../com/facebook/react/ReactInstanceManager.java | 12 +++++++----- .../java/com/facebook/react/bridge/ReactContext.java | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index e98a41716fb005..e046f6b5bc370f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -1175,7 +1175,6 @@ private void setupReactContext(final ReactApplicationContext reactContext) { mDevSupportManager.onNewReactContextCreated(reactContext); mMemoryPressureRouter.addMemoryPressureListener(catalystInstance); - moveReactContextToCurrentLifecycleState(); ReactMarker.logMarker(ATTACH_MEASURED_ROOT_VIEWS_START); for (ReactRoot reactRoot : mAttachedReactRoots) { @@ -1197,6 +1196,8 @@ private void setupReactContext(final ReactApplicationContext reactContext) { new Runnable() { @Override public void run() { + moveReactContextToCurrentLifecycleState(); + for (com.facebook.react.ReactInstanceEventListener listener : finalListeners) { // Sometimes this listener is null - probably due to race // condition between allocating listeners with a certain @@ -1208,10 +1209,6 @@ public void run() { } } }); - Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); - ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); - // Mark end of bridge loading - ReactMarker.logMarker(ReactMarkerConstants.REACT_BRIDGE_LOADING_END); reactContext.runOnJSQueueThread( new Runnable() { @Override @@ -1227,6 +1224,11 @@ public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); } }); + + Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); + ReactMarker.logMarker(SETUP_REACT_CONTEXT_END); + // Mark end of bridge loading + ReactMarker.logMarker(ReactMarkerConstants.REACT_BRIDGE_LOADING_END); } private void attachRootViewToInstance(final ReactRoot reactRoot) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index e45f93b4ba1e34..aadcadfa72c046 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -254,6 +254,7 @@ public void removeWindowFocusChangeListener(WindowFocusChangeListener listener) } /** Should be called by the hosting Fragment in {@link Fragment#onResume} */ + @ThreadConfined(UI) public void onHostResume(@Nullable Activity activity) { mLifecycleState = LifecycleState.RESUMED; mCurrentActivity = new WeakReference(activity); @@ -282,6 +283,7 @@ public void onNewIntent(@Nullable Activity activity, Intent intent) { } /** Should be called by the hosting Fragment in {@link Fragment#onPause} */ + @ThreadConfined(UI) public void onHostPause() { mLifecycleState = LifecycleState.BEFORE_RESUME; ReactMarker.logMarker(ReactMarkerConstants.ON_HOST_PAUSE_START);