Fix crash on reload when useImmediateExecutorInAndroidBridgeless is enabled (#45162)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45162

Previously we would crash in ReactInstance#callFunctionOnModule (P1443291303) when reloading (due to the onHostPause call) because we removed a source of synchronization by using the immediate executor.

Workaround it by making sure we always null out references to `mReactInstance` before we actually start destroying it.

Changelog: [Internal]

Reviewed By: RSNara

Differential Revision: D59002404

fbshipit-source-id: 3ee14cd1fe7d423bb6158356bb99b3d2d6af8d6f
This commit is contained in:
Pieter De Baets
2024-06-27 03:25:39 -07:00
committed by Facebook GitHub Bot
parent c128a517b8
commit c2e080419b
@@ -1416,6 +1416,9 @@ public class ReactHostImpl implements ReactHost {
final ReactContext reactContext = mBridgelessReactContextRef.getNullable();
if (reactContext != null) {
log(method, "Resetting ReactContext ref");
mBridgelessReactContextRef.reset();
log(method, "Destroying ReactContext");
reactContext.destroy();
}
@@ -1439,20 +1442,17 @@ public class ReactHostImpl implements ReactHost {
raiseSoftException(
method, "Skipping ReactInstance.destroy(): ReactInstance null");
} else {
log(method, "Resetting ReactInstance ptr");
mReactInstance = null;
log(method, "Destroying ReactInstance");
reactInstance.destroy();
}
log(method, "Resetting ReactContext ref");
mBridgelessReactContextRef.reset();
log(method, "Resetting ReactInstance task ref");
log(method, "Resetting createReactInstance task ref");
mCreateReactInstanceTaskRef.reset();
log(method, "Resetting ReactInstance ptr");
mReactInstance = null;
log(method, "Resetting preload task ref");
log(method, "Resetting start task ref");
mStartTask = null;
// Kickstart a new ReactInstance create
@@ -1462,7 +1462,7 @@ public class ReactHostImpl implements ReactHost {
.continueWithTask(
task -> {
final ReactInstance reactInstance =
reactInstanceTaskUnwrapper.unwrap(task, "7: Restarting surfaces");
reactInstanceTaskUnwrapper.unwrap(task, "6: Restarting surfaces");
if (reactInstance == null) {
raiseSoftException(method, "Skipping surface restart: ReactInstance null");
@@ -1610,7 +1610,6 @@ public class ReactHostImpl implements ReactHost {
reactInstanceTaskUnwrapper.unwrap(task, "4: Destroying ReactContext");
final ReactContext reactContext = mBridgelessReactContextRef.getNullable();
if (reactContext == null) {
raiseSoftException(method, "ReactContext is null. Destroy reason: " + reason);
}
@@ -1620,6 +1619,9 @@ public class ReactHostImpl implements ReactHost {
mMemoryPressureRouter.destroy(mContext);
if (reactContext != null) {
log(method, "Resetting ReactContext ref");
mBridgelessReactContextRef.reset();
log(method, "Destroying ReactContext");
reactContext.destroy();
}
@@ -1642,20 +1644,17 @@ public class ReactHostImpl implements ReactHost {
raiseSoftException(
method, "Skipping ReactInstance.destroy(): ReactInstance null");
} else {
log(method, "Resetting ReactInstance ptr");
mReactInstance = null;
log(method, "Destroying ReactInstance");
reactInstance.destroy();
}
log(method, "Resetting ReactContext ref ");
mBridgelessReactContextRef.reset();
log(method, "Resetting ReactInstance task ref");
log(method, "Resetting createReactInstance task ref");
mCreateReactInstanceTaskRef.reset();
log(method, "Resetting ReactInstance ptr");
mReactInstance = null;
log(method, "Resetting Preload task ref");
log(method, "Resetting start task ref");
mStartTask = null;
log(method, "Resetting destroy task ref");