From 8eb4240d3661e00920af513ff0e864593c1978a0 Mon Sep 17 00:00:00 2001 From: ManasGuptaSprinklr <154529197+ManasGuptaSprinklr@users.noreply.github.com> Date: Fri, 26 Jun 2026 07:50:48 -0700 Subject: [PATCH] fix: add null checks for getViewState in SurfaceMountingManager (#57283) Summary: Fix SurfaceMountingManager null handling for methods that can be invoked after a view has been deleted or is otherwise missing. sendAccessibilityEvent and setJSResponder now use getNullableViewState(...) and guard against missing ViewState before accessing .view, logging a soft exception and returning instead of crashing. Also removed the now-unused getViewState helper. ## Changelog: [ANDROID][FIXED] Avoid crash in SurfaceMountingManager when sendAccessibilityEvent or setJSResponder is called for a missing or deleted view state. Pull Request resolved: https://github.com/react/react-native/pull/57283 Test Plan: This is a safe check added to avoid crashing the application with reference to similiar checks added in other related methods. Reviewed By: fabriziocucci Differential Revision: D109834942 Pulled By: javache fbshipit-source-id: 75d6e5e646f78efe9456aaacafd40b05362003d7 --- .../fabric/mounting/SurfaceMountingManager.kt | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.kt index c1ee884ac05..85216061a77 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/SurfaceMountingManager.kt @@ -760,9 +760,16 @@ internal constructor( return } - val view = getViewState(reactTag).view + val viewState = getNullableViewState(reactTag) + val view = viewState?.view if (view == null) { - throw RetryableMountingLayerException("Unable to find viewState view for tag $reactTag") + ReactSoftExceptionLogger.logSoftException( + ReactSoftExceptionLogger.Categories.SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE, + ReactNoCrashSoftException( + "Unable to find viewState for tag $reactTag for sendAccessibilityEvent" + ), + ) + return } view.sendAccessibilityEvent(eventType) @@ -1004,16 +1011,24 @@ internal constructor( return } - val viewState = getViewState(reactTag) - val view = viewState.view + val viewState = getNullableViewState(reactTag) + + val view = viewState?.view + if (view == null) { + ReactSoftExceptionLogger.logSoftException( + ReactSoftExceptionLogger.Categories.SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE, + ReactNoCrashSoftException( + "Unable to find viewState for tag $reactTag for setJSResponder" + ), + ) + return + } + if (initialReactTag != reactTag && view is ViewParent) { // In this case, initialReactTag corresponds to a virtual/layout-only View, and we already // have a parent of that View in reactTag, so we can use it. jsResponderHandler.setJSResponder(initialReactTag, view as ViewParent) return - } else if (view == null) { - SoftAssertions.assertUnreachable("Cannot find view for tag [$reactTag].") - return } if (viewState.isRoot) { @@ -1122,12 +1137,6 @@ internal constructor( ) } - private fun getViewState(reactTag: Int): ViewState = - getNullableViewState(reactTag) - ?: throw RetryableMountingLayerException( - "Unable to find viewState for tag $reactTag. Surface stopped: $isStopped" - ) - private fun getNullableViewState(reactTag: Int): ViewState? = registryLock.read { tagToViewState[reactTag] }