From 9ddc038e547eafc48b71195634dad210882149f6 Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Sat, 19 Oct 2019 02:27:35 -0700 Subject: [PATCH] WebSocketModule should verify that Catalyst is alive before getting a JS module Summary: WebSocketModule can be called asynchronously while the ReactContext/CatalystInstance is being torn down. Trying to get a JS module at this time will result in a crash if the ReactContext has already torn down Catalyst. Check explicitly that Catalyst is still alive before trying to emit an event to JS via some JS module. Changelog: [Internal] Reviewed By: mdvacca Differential Revision: D18020359 fbshipit-source-id: 1b77abd457c7d97bd241389251890bb682b6fde3 --- .../modules/websocket/WebSocketModule.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java index 4ab4d2e99db..6684d9e33b5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java @@ -7,6 +7,7 @@ package com.facebook.react.modules.websocket; +import android.util.Log; import androidx.annotation.Nullable; import com.facebook.common.logging.FLog; import com.facebook.react.bridge.Arguments; @@ -14,12 +15,14 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReactSoftException; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableMapKeySetIterator; import com.facebook.react.bridge.ReadableType; import com.facebook.react.bridge.WritableMap; import com.facebook.react.common.ReactConstants; +import com.facebook.react.common.build.ReactBuildConfig; import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.network.ForwardingCookieHandler; @@ -61,9 +64,21 @@ public final class WebSocketModule extends ReactContextBaseJavaModule { } private void sendEvent(String eventName, WritableMap params) { - mReactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(eventName, params); + if (mReactContext.hasActiveCatalystInstance()) { + mReactContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(eventName, params); + } else { + // We want to collect data about how often this happens, but raising a SoftException in Debug + // will cause a crash, which isn't desirable. + String msg = + "sendEvent: trying to update app state when Catalyst Instance has already disappeared"; + if (ReactBuildConfig.DEBUG) { + Log.e(NAME, msg); + } else { + ReactSoftException.logSoftException(NAME, new IllegalStateException(msg)); + } + } } @Override