From 6dd5a838c340aabc1ad385ccfc5cc3b478d5fda3 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 28 Mar 2025 04:17:20 -0700 Subject: [PATCH] Do not crash when event is emitted when ReactInstance is reloaded Summary: Regression introduced in D71735505 where I tried to ensure fabricEventEmitter was always non-null. Instead log a soft error when this happens, so we don't drop the event silently. Changelog: [Android][Fixed] Fixed crash when event is emitted after instance is shutdown Reviewed By: mdvacca Differential Revision: D71967092 fbshipit-source-id: 990b6414b41a2709d70a6deae38f5aa043203a20 --- .../uimanager/events/EventEmitterImpl.kt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventEmitterImpl.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventEmitterImpl.kt index fe7b7ef5a45..1733667dc3b 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventEmitterImpl.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/EventEmitterImpl.kt @@ -83,7 +83,7 @@ internal class EventEmitterImpl( logSoftException( TAG, ReactNoCrashSoftException( - "Cannot get RCTEventEmitter from Context, no active Catalyst instance!")) + "Cannot get RCTEventEmitter without active Catalyst instance!")) } } return legacyEventEmitter @@ -100,15 +100,15 @@ internal class EventEmitterImpl( ) { @UIManagerType val uiManagerType = getUIManagerType(targetTag, surfaceId) if (uiManagerType == UIManagerType.FABRIC) { - checkNotNull(fabricEventEmitter) - .receiveEvent( - surfaceId, - targetTag, - eventName, - canCoalesceEvent, - customCoalesceKey, - params, - category) + val fabricEventEmitter = fabricEventEmitter + if (fabricEventEmitter == null) { + logSoftException( + TAG, + ReactNoCrashSoftException("No fabricEventEmitter registered, cannot dispatch event")) + } else { + fabricEventEmitter.receiveEvent( + surfaceId, targetTag, eventName, canCoalesceEvent, customCoalesceKey, params, category) + } } else if (uiManagerType == UIManagerType.LEGACY) { ensureLegacyEventEmitter()?.receiveEvent(targetTag, eventName, params) }