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
This commit is contained in:
ManasGuptaSprinklr authored and meta-codesync[bot] committed 2026-06-26 07:50:48 -07:00
1 parent 83719830d6
commit 8eb4240d36
1 file changed
+22 -13
@@ -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]
}