Handle missing viewState gracefully in SurfaceMountingManager.addViewAt (#56389)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/56389

Pull Request resolved: https://github.com/facebook/react-native/pull/56376

Changelog:

[ReactAndroid][Fixed] - Prevented a crash in Fabric mounting by handling missing viewState in SurfaceMountingManager.addViewAt() (log + no-op instead of throwing).

## Problem
`SurfaceMountingManager.addViewAt()` throws `RetryableMountingLayerException` when a view tag is not found in the `tagToViewState` map. This exception is **not handled** in the `IntBufferBatchMountItem` execution path — the retry logic in `MountItemDispatcher` only catches this for `DispatchCommandMountItem`. The exception propagates and crashes the app.

**Crash**: `RetryableMountingLayerException: Unable to find viewState for tag X. Surface stopped: false`
**Process**: `com.oculus.firsttimenux:login`
**Impact**: 44,763 occurrences / 14,674 affected users in 30 days

Logview link: [252c85116a7ab5c4ec93ef3c3373cf9d](https://www.internalfb.com/logview/system_vros_crashes/252c85116a7ab5c4ec93ef3c3373cf9d)

## Root Cause
`addViewAt()` uses `getViewState()` which throws on missing tags, while other methods like `removeViewAt()`, `updateProps()`, `updateLayout()`, and `deleteView()` all use `getNullableViewState()` with graceful null handling (soft exception log + early return). This inconsistency means `addViewAt` is the only mount operation that crashes when encountering a timing-related missing view state.

## Fix
Changed `addViewAt()` to use `getNullableViewState()` for both parent and child tag lookups, matching the established pattern used by `removeViewAt`, `updateProps`, `updateLayout`, and `deleteView`. On null, a soft exception is logged with the `SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE` category and the operation returns early.

Reviewed By: cortinico

Differential Revision: D99760257

fbshipit-source-id: 3ee9baadfaf1ccb9094effe5bfc2bf9d14bb4ddb
This commit is contained in:
generatedunixname1074695667417611 authored and meta-codesync[bot] committed 2026-04-09 19:00:35 -07:00
1 parent 81c99682a4
commit 64002fbed1
1 file changed
+16 -2
@@ -282,7 +282,14 @@ internal constructor(
return
}
val parentViewState = getViewState(parentTag)
val parentViewState = getNullableViewState(parentTag)
if (parentViewState == null) {
ReactSoftExceptionLogger.logSoftException(
ReactSoftExceptionLogger.Categories.SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE,
ReactNoCrashSoftException("Unable to find viewState for tag: [$parentTag] for addViewAt"),
)
return
}
if (parentViewState.view !is ViewGroup) {
val message =
"Unable to add a view into a view that is not a ViewGroup. ParentTag: $parentTag - Tag: $tag - Index: $index"
@@ -290,7 +297,14 @@ internal constructor(
throw IllegalStateException(message)
}
val parentView = parentViewState.view as ViewGroup
val viewState = getViewState(tag)
val viewState = getNullableViewState(tag)
if (viewState == null) {
ReactSoftExceptionLogger.logSoftException(
ReactSoftExceptionLogger.Categories.SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE,
ReactNoCrashSoftException("Unable to find viewState for tag: [$tag] for addViewAt"),
)
return
}
val view = viewState.view
checkNotNull(view) { "Unable to find view for viewState $viewState and tag $tag" }