From 60efeca225a4cd87c647084157591d52c7cb57d8 Mon Sep 17 00:00:00 2001 From: Samer Rayyan Date: Thu, 30 Apr 2026 11:04:31 -0700 Subject: [PATCH] Fix RetryableMountingLayerException crash in SurfaceMountingManager.addViewAt Summary: ## Problem The `addViewAt` method in `SurfaceMountingManager` crashes with a `RetryableMountingLayerException` when trying to find a `ViewState` for a child tag that doesn't exist in the `tagToViewState` map. **Error:** `Unable to find viewState for tag 290. Surface stopped: false` **Logview:** [252c85116a7ab5c4ec93ef3c3373cf9d](https://www.internalfb.com/logview/system_vros_crashes/252c85116a7ab5c4ec93ef3c3373cf9d) ## Root Cause The `addViewAt` method uses `getViewState()` which throws `RetryableMountingLayerException` when a tag is not found. This can happen due to race conditions where a view's state is removed (e.g., during surface cleanup or view deletion) before the mount item that references it is executed. ## Fix Replaced `getViewState()` calls with `getNullableViewState()` + null checks + soft exception logging + early return for both the parent and child tag lookups in `addViewAt`. This matches the existing pattern already used by `removeViewAt`, which handles the same scenario gracefully. The fix: - Uses `getNullableViewState(parentTag)` instead of `getViewState(parentTag)` for the parent view state lookup - Uses `getNullableViewState(tag)` instead of `getViewState(tag)` for the child view state lookup - Adds null check for the child view itself - Logs soft exceptions with the `SURFACE_MOUNTING_MANAGER_MISSING_VIEWSTATE` category for monitoring - Returns early instead of crashing, allowing the surface to continue operating Changelog: [Android][Fixed] - Fixed crash in SurfaceMountingManager.addViewAt when viewState is missing for a tag Differential Revision: D99364621 --- .../facebook/react/fabric/mounting/SurfaceMountingManager.kt | 3 +-- 1 file changed, 1 insertion(+), 2 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 b535c10ce5a..b05196052d5 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 @@ -307,8 +307,7 @@ internal constructor( ) return } - val view = viewState.view - checkNotNull(view) { "Unable to find view for viewState $viewState and tag $tag" } + val view = viewState.view ?: return // Display children before inserting if (SHOW_CHANGED_VIEW_HIERARCHIES) {