From 4d36e57839d5e13584b13c14792d2d3e5182bcf2 Mon Sep 17 00:00:00 2001 From: jingjing2222 Date: Tue, 26 May 2026 09:01:52 -0700 Subject: [PATCH] Fix Android Fabric Native Animated null prop crash (#56913) Summary: This fixes an Android Fabric crash in the `overrideBySynchronousMountPropsAtMountingAndroid` path when Native Animated has stored a synchronous `transform` / `opacity` override and a later regular Fabric commit contains `null` for the same prop. | As-is | To-be | | --- | --- | | Baseline Android Fabric transform crash repro | Patched Android Fabric transform result | | [crashed.gif](https://github.com/jingjing2222/react-native-fabric-transform-repro/blob/main/crashed.gif) | [patched.gif](https://github.com/jingjing2222/react-native-fabric-transform-repro/blob/main/patched.gif) | Native Animated can synchronously update `transform` / `opacity` on the UI thread. A regular Fabric commit for the same view may arrive later with stale props, including `transform: null` or `opacity: null`. Those regular Fabric null values should not clear the stored synchronous override, because the override may still represent the fresher Native Animated value. Before this patch, the override merge path assumed that the incoming Fabric prop had the same non-null type as the stored override. For example, a stored `transform` override is an array, but a regular Fabric update may contain `transform: null`. That mismatch could crash in `SurfaceMountingManager.overridePropsReadableMap`. This patch: - Allows regular Fabric `null` updates to be overridden by stored synchronous `transform` / `opacity` values instead of crashing. - Keeps regular Fabric `transform: null` / `opacity: null` from clearing the stored synchronous override. - Scopes synchronous null clearing to `transform` / `opacity` only. - Leaves null values for other props untouched, since other view prop nulls may be meaningful. This PR intentionally does not change Java Native Animated `restoreDefaultValues()` behavior. That restore implementation can be handled separately because changing it may affect existing apps. Repro repository: https://github.com/jingjing2222/react-native-fabric-transform-repro ## Why this is correct A regular Fabric `null` update is not enough to prove user intent to clear the native animated value, because it can be the stale commit produced after Native Animated already wrote a fresher value on the UI thread. In that case, the stored synchronous override should continue to win. If the synchronous Native Animated path explicitly sends `transform: null` or `opacity: null`, this patch treats that as a clear signal for that stored override key. The clear behavior is intentionally scoped to the props currently stored by this override path. ```text regular Fabric null: possible stale commit -> stored synchronous override wins synchronous transform / opacity null: explicit clear -> stored override key is removed other null props: left untouched ``` ## Changelog: [ANDROID] [FIXED] - Fix Android Fabric Native Animated crash when regular Fabric null props collide with stored synchronous transform or opacity overrides. Pull Request resolved: https://github.com/facebook/react-native/pull/56913 Test Plan: Added regression coverage for stale regular Fabric null commits and scoped synchronous null clears: - `updateProps_withNullOpacity_keepsStoredSynchronousProp` - `updateProps_withNullTransform_keepsStoredSynchronousProp` - `updatePropsSynchronously_withNullOpacity_removesStoredSynchronousProp` - `updatePropsSynchronously_withNullTransform_removesStoredSynchronousProp` Ran: ```sh ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests com.facebook.react.fabric.SurfaceMountingManagerSynchronousMountPropsTest -Preact.internal.useHermesStable=true ``` Result: ```text BUILD SUCCESSFUL ``` Ran: ```sh ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests com.facebook.react.animated.NativeAnimatedNodeTraversalTest -Preact.internal.useHermesStable=true ``` Result: ```text BUILD SUCCESSFUL ``` Ran: ```sh ./gradlew :packages:react-native:ReactAndroid:ktfmtCheck -Preact.internal.useHermesStable=true ``` Result: ```text BUILD SUCCESSFUL ``` Reviewed By: javache Differential Revision: D105974289 Pulled By: zeyap fbshipit-source-id: 7688cfa6b89f2d107c7ae58356864835d41f6add --- .../fabric/mounting/SurfaceMountingManager.kt | 46 ++++++-- ...ountingManagerSynchronousMountPropsTest.kt | 101 +++++++++++++++++- 2 files changed, 135 insertions(+), 12 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 ad8f5ca188e..f02de8b3697 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 @@ -625,14 +625,15 @@ internal constructor( public fun storeSynchronousMountPropsOverride(reactTag: Int, props: ReadableMap): Unit { if (ReactNativeFeatureFlags.overrideBySynchronousMountPropsAtMountingAndroid()) { - val propsMap = getMapFromPropsReadableMap(props) - var synchronousMountProps = tagToSynchronousMountProps[reactTag] - if (synchronousMountProps != null) { - synchronousMountProps.putAll(propsMap) + val propsMap = getAnimatedPropsMap(props) + val synchronousMountProps = tagToSynchronousMountProps[reactTag] ?: mutableMapOf() + clearAnimatedNullPropsMapKeys(props, synchronousMountProps) + synchronousMountProps.putAll(propsMap) + if (synchronousMountProps.isEmpty()) { + tagToSynchronousMountProps.remove(reactTag) } else { - synchronousMountProps = propsMap + tagToSynchronousMountProps[reactTag] = synchronousMountProps } - tagToSynchronousMountProps[reactTag] = synchronousMountProps } } @@ -1332,7 +1333,11 @@ internal constructor( for ((propKey, propValue) in patchMap) { if (outputReadableMap.hasKey(propKey)) { if (propKey == PROP_TRANSFORM) { - assert(outputReadableMap.getType(propKey) == ReadableType.Array && propValue is List<*>) + val outputType = outputReadableMap.getType(propKey) + assert( + (outputType == ReadableType.Array || outputType == ReadableType.Null) && + propValue is List<*> + ) val array = WritableNativeArray() for (item in propValue as List<*>) { if (item is Map<*, *>) { @@ -1350,14 +1355,18 @@ internal constructor( } outputReadableMap.putArray(propKey, array) } else if (propKey == PROP_OPACITY) { - assert(outputReadableMap.getType(propKey) == ReadableType.Number && propValue is Number) + val outputType = outputReadableMap.getType(propKey) + assert( + (outputType == ReadableType.Number || outputType == ReadableType.Null) && + propValue is Number + ) outputReadableMap.putDouble(propKey, (propValue as Number).toDouble()) } } } } - private fun getMapFromPropsReadableMap(readableMap: ReadableMap): MutableMap { + private fun getAnimatedPropsMap(readableMap: ReadableMap): MutableMap { val outputMap = mutableMapOf() if ( @@ -1387,6 +1396,25 @@ internal constructor( return outputMap } + private fun clearAnimatedNullPropsMapKeys( + readableMap: ReadableMap, + outputMap: MutableMap, + ) { + // Native Animated uses synchronous null updates to restore animated-managed props. + // Keep this scoped to props stored by this override path today. + if ( + readableMap.hasKey(PROP_TRANSFORM) && + readableMap.getType(PROP_TRANSFORM) == ReadableType.Null + ) { + outputMap.remove(PROP_TRANSFORM) + } + if ( + readableMap.hasKey(PROP_OPACITY) && readableMap.getType(PROP_OPACITY) == ReadableType.Null + ) { + outputMap.remove(PROP_OPACITY) + } + } + // prevents unchecked conversion warn of the type private fun getViewGroupManager(viewState: ViewState): IViewGroupManager { val viewManager = diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/SurfaceMountingManagerSynchronousMountPropsTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/SurfaceMountingManagerSynchronousMountPropsTest.kt index 2c0787d17c8..52067918c54 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/SurfaceMountingManagerSynchronousMountPropsTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/SurfaceMountingManagerSynchronousMountPropsTest.kt @@ -10,15 +10,17 @@ package com.facebook.react.fabric import com.facebook.react.ReactRootView +import com.facebook.react.bridge.JavaOnlyArray import com.facebook.react.bridge.JavaOnlyMap import com.facebook.react.bridge.ReactTestHelper +import com.facebook.react.bridge.ReadableArray import com.facebook.react.fabric.mounting.MountingManager -import com.facebook.react.fabric.mounting.MountingManager.MountItemExecutor import com.facebook.react.fabric.mounting.SurfaceMountingManager import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests import com.facebook.react.uimanager.ThemedReactContext import com.facebook.react.uimanager.ViewManager import com.facebook.react.uimanager.ViewManagerRegistry +import com.facebook.react.views.view.ReactViewGroup import com.facebook.react.views.view.ReactViewManager import com.facebook.testutils.shadows.ShadowNativeLoader import com.facebook.testutils.shadows.ShadowNativeMap @@ -67,8 +69,8 @@ class SurfaceMountingManagerSynchronousMountPropsTest { themedReactContext = ThemedReactContext(reactContext, reactContext, null, -1) mountingManager = MountingManager( - ViewManagerRegistry(listOf>(ReactViewManager())), - MountItemExecutor {}, + ViewManagerRegistry(listOf>(TestReactViewManager())), + {}, ) } @@ -83,6 +85,25 @@ class SurfaceMountingManagerSynchronousMountPropsTest { smm.addViewAt(surfaceId, tag, 0) } + private class TestReactViewManager : ReactViewManager() { + override fun setTransformProperty( + view: ReactViewGroup, + transforms: ReadableArray?, + transformOrigin: ReadableArray?, + ) { + view.translationY = 0.0f + if (transforms == null) { + return + } + for (i in 0..