Stop using bolts for memory pressure handling

Summary:
We believe a problem in Bolts is leading to an ANR issue in Fb4a: T152128771

This diff migrates the memory pressure handling path off Bolts tasks.

There is server-side gating, in case we want to disable this fix server-side.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D45461419

fbshipit-source-id: e000a2a8da5f86a0681e6bbd2e058726110d6dea
This commit is contained in:
Ramanpreet Nara
2023-05-01 22:24:37 -07:00
committed by Facebook GitHub Bot
parent a690a4af6b
commit 4337dbcdf4
2 changed files with 26 additions and 1 deletions
@@ -54,6 +54,7 @@ import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.events.BlackHoleEventDispatcher;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -93,7 +94,7 @@ public class ReactHost {
private final QueueThreadExceptionHandler mQueueThreadExceptionHandler;
private final Set<ReactSurface> mAttachedSurfaces = Collections.synchronizedSet(new HashSet<>());
private final MemoryPressureRouter mMemoryPressureRouter;
private final MemoryPressureListener mMemoryPressureListener;
private MemoryPressureListener mMemoryPressureListener;
private final boolean mAllowPackagerServerAccess;
private final boolean mUseDevSupport;
private final Collection<ReactInstanceEventListener> mReactInstanceEventListeners =
@@ -169,6 +170,19 @@ public class ReactHost {
mUseDevSupport = useDevSupport;
}
private MemoryPressureListener createMemoryPressureListener(ReactInstance reactInstance) {
WeakReference<ReactInstance> weakReactInstance = new WeakReference<>(reactInstance);
return (level) -> {
mBGExecutor.execute(
() -> {
@Nullable ReactInstance strongReactInstance = weakReactInstance.get();
if (strongReactInstance != null) {
strongReactInstance.handleMemoryPressure(level);
}
});
};
}
public LifecycleState getLifecycleState() {
return mReactLifecycleStateManager.getLifecycleState();
}
@@ -772,6 +786,10 @@ public class ReactHost {
mReactJsExceptionHandler,
mUseDevSupport);
if (ReactFeatureFlags
.unstable_bridgelessArchitectureMemoryPressureHackyBoltsFix) {
mMemoryPressureListener = createMemoryPressureListener(instance);
}
mMemoryPressureRouter.addMemoryPressureListener(mMemoryPressureListener);
log(method, "Loading JS Bundle");
@@ -862,6 +880,10 @@ public class ReactHost {
mReactJsExceptionHandler,
mUseDevSupport);
if (ReactFeatureFlags
.unstable_bridgelessArchitectureMemoryPressureHackyBoltsFix) {
mMemoryPressureListener = createMemoryPressureListener(instance);
}
mMemoryPressureRouter.addMemoryPressureListener(mMemoryPressureListener);
log(method, "Loading JS Bundle");
@@ -56,6 +56,9 @@ public class ReactFeatureFlags {
*/
public static boolean enableBridgelessArchitecture = false;
/** Server-side gating for a hacky fix to an ANR in the bridgeless core, related to Bolts task. */
public static boolean unstable_bridgelessArchitectureMemoryPressureHackyBoltsFix = false;
/**
* Does the bridgeless architecture log soft exceptions. Could be useful for tracking down issues.
*/