Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ public void onViewDetachedFromWindow(View v) {
@Deprecated
public void onHostDestroy() {
UiThreadUtil.assertOnUiThread();
destroyInspectorTarget();

if (mUseDeveloperSupport) {
mDevSupportManager.setDevSupportEnabled(false);
Expand Down Expand Up @@ -752,7 +751,6 @@ public void destroy() {
}

mHasStartedDestroying = true;
destroyInspectorTarget();

if (mUseDeveloperSupport) {
mDevSupportManager.setDevSupportEnabled(false);
Expand Down Expand Up @@ -782,6 +780,12 @@ public void destroy() {
}
}

// If the host is being destroyed, now that the current context/instance
// has been destroyed, we can safely destroy the host's inspector target.
if (mLifecycleState == LifecycleState.BEFORE_CREATE) {
destroyInspectorHostTarget();
}

mHasStartedCreatingInitialContext = false;
if (!mKeepActivity) {
mCurrentActivity = null;
Expand Down Expand Up @@ -835,6 +839,10 @@ private synchronized void moveToBeforeCreateLifecycleState() {
if (mLifecycleState == LifecycleState.BEFORE_RESUME) {
currentContext.onHostDestroy(mKeepActivity);
}
} else {
// There's no current context that requires the host inspector target to
// be kept alive, so we can destroy it immediately.
destroyInspectorHostTarget();
}
mLifecycleState = LifecycleState.BEFORE_CREATE;
}
Expand Down Expand Up @@ -1542,7 +1550,8 @@ public void onResume() {
return mInspectorTarget;
}

private void destroyInspectorTarget() {
@ThreadConfined(UI)
private void destroyInspectorHostTarget() {
if (mInspectorTarget != null) {
mInspectorTarget.close();
mInspectorTarget = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package com.facebook.react.bridge;

import static com.facebook.infer.annotation.Assertions.assertCondition;
import static com.facebook.infer.annotation.ThreadConfined.UI;
import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;

Expand Down Expand Up @@ -112,6 +113,8 @@ public String toString() {

public native NativeMethodCallInvokerHolderImpl getNativeMethodCallInvokerHolder();

private @Nullable ReactInstanceManagerInspectorTarget mInspectorTarget;

private CatalystInstanceImpl(
final ReactQueueConfigurationSpec reactQueueConfigurationSpec,
final JavaScriptExecutor jsExecutor,
Expand All @@ -134,6 +137,7 @@ private CatalystInstanceImpl(
mJSExceptionHandler = jSExceptionHandler;
mNativeModulesQueueThread = mReactQueueConfiguration.getNativeModulesQueueThread();
mTraceListener = new JSProfilerTraceListener(this);
mInspectorTarget = inspectorTarget;
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);

FLog.d(ReactConstants.TAG, "Initializing React Xplat Bridge before initializeBridge");
Expand All @@ -146,7 +150,7 @@ private CatalystInstanceImpl(
mNativeModulesQueueThread,
mNativeModuleRegistry.getJavaModules(this),
mNativeModuleRegistry.getCxxModules(),
inspectorTarget);
mInspectorTarget);
FLog.d(ReactConstants.TAG, "Initializing React Xplat Bridge after initializeBridge");
Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);

Expand Down Expand Up @@ -346,6 +350,11 @@ public void destroy() {
return;
}

if (mInspectorTarget != null) {
assertCondition(
mInspectorTarget.isValid(),
"ReactInstanceManager inspector target destroyed before instance was unregistered");
}
unregisterFromInspector();

// TODO: tell all APIs to shut down
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

package com.facebook.react.bridge;

import com.facebook.infer.annotation.Nullsafe;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStripAny;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;

@DoNotStripAny
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ReactInstanceManagerInspectorTarget implements AutoCloseable {
public interface TargetDelegate {
public void onReload();
Expand Down Expand Up @@ -46,6 +48,10 @@ public void close() {
mHybridData.resetNative();
}

/*internal*/ boolean isValid() {
return mHybridData.isValid();
}

static {
ReactBridge.staticInit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,11 @@ private Task<Void> getOrCreateStartTask() {
@ThreadConfined(UI)
private void moveToHostDestroy(@Nullable ReactContext currentContext) {
mReactLifecycleStateManager.moveToOnHostDestroy(currentContext);
destroyReactHostInspectorTarget();
if (currentContext == null) {
// There's no current context/instance that requires the host inspector
// target to be kept alive, so we can destroy it immediately.
destroyInspectorHostTarget();
}
setCurrentActivity(null);
}

Expand Down Expand Up @@ -1333,9 +1337,7 @@ private Task<ReactInstance> getOrCreateReloadTask(String reason) {
final ReactInstance reactInstance =
reactInstanceTaskUnwrapper.unwrap(task, "1: Starting reload");

if (reactInstance != null) {
reactInstance.unregisterFromInspector();
}
unregisterInstanceFromInspector(reactInstance);

final ReactContext reactContext = mBridgelessReactContextRef.getNullable();
if (reactContext == null) {
Expand Down Expand Up @@ -1512,9 +1514,7 @@ private Task<Void> getOrCreateDestroyTask(final String reason, @Nullable Excepti
final ReactInstance reactInstance =
reactInstanceTaskUnwrapper.unwrap(task, "1: Starting destroy");

if (reactInstance != null) {
reactInstance.unregisterFromInspector();
}
unregisterInstanceFromInspector(reactInstance);

// Step 1: Destroy DevSupportManager
if (mUseDevSupport) {
Expand Down Expand Up @@ -1672,10 +1672,29 @@ public void setJsEngineResolutionAlgorithm(
return mReactHostInspectorTarget;
}

private void destroyReactHostInspectorTarget() {
@ThreadConfined(UI)
private void destroyInspectorHostTarget() {
if (mReactHostInspectorTarget != null) {
mReactHostInspectorTarget.close();
mReactHostInspectorTarget = null;
}
}

@ThreadConfined(UI)
private void unregisterInstanceFromInspector(final @Nullable ReactInstance reactInstance) {
if (reactInstance != null) {
if (InspectorFlags.getFuseboxEnabled()) {
Assertions.assertCondition(
mReactHostInspectorTarget != null && mReactHostInspectorTarget.isValid(),
"Host inspector target destroyed before instance was unregistered");
}
reactInstance.unregisterFromInspector();
}
if (mReactLifecycleStateManager.getLifecycleState() == LifecycleState.BEFORE_CREATE) {
// If the host is being destroyed, now that the current context/instance
// has been unregistered, we can safely destroy the host's inspector
// target.
destroyInspectorHostTarget();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ internal class ReactHostInspectorTarget(private val reactHostImpl: ReactHostImpl
mHybridData.resetNative()
}

fun isValid(): Boolean {
return mHybridData.isValid()
}

private companion object {
init {
SoLoader.loadLibrary("rninstance")
Expand Down