mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
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 | | --- | --- | | <img src="https://raw.githubusercontent.com/jingjing2222/react-native-fabric-transform-repro/main/crashed.gif" alt="Baseline Android Fabric transform crash repro" width="320" /> | <img src="https://raw.githubusercontent.com/jingjing2222/react-native-fabric-transform-repro/main/patched.gif" alt="Patched Android Fabric transform result" width="320" /> | | [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
This commit is contained in:
1 parent
821045a24f
commit
cdfba520fa
2 files changed
+135
-12
No files matched your search
+37
-9
@@ -626,14 +626,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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1328,7 +1329,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<*, *>) {
|
||||
@@ -1346,14 +1351,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<String, Any> {
|
||||
private fun getAnimatedPropsMap(readableMap: ReadableMap): MutableMap<String, Any> {
|
||||
val outputMap = mutableMapOf<String, Any>()
|
||||
|
||||
if (
|
||||
@@ -1383,6 +1392,25 @@ internal constructor(
|
||||
return outputMap
|
||||
}
|
||||
|
||||
private fun clearAnimatedNullPropsMapKeys(
|
||||
readableMap: ReadableMap,
|
||||
outputMap: MutableMap<String, Any>,
|
||||
) {
|
||||
// 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 <ViewGroup> type
|
||||
private fun getViewGroupManager(viewState: ViewState): IViewGroupManager<View> {
|
||||
val viewManager =
|
||||
|
||||
+98
-3
@@ -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<ViewManager<*, *>>(ReactViewManager())),
|
||||
MountItemExecutor {},
|
||||
ViewManagerRegistry(listOf<ViewManager<*, *>>(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..<transforms.size()) {
|
||||
val transform = transforms.getMap(i) ?: continue
|
||||
if (transform.hasKey("translateY")) {
|
||||
view.translationY = transform.getDouble("translateY").toFloat()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Stored synchronous opacity should override a stale Fabric mount update. */
|
||||
@Test
|
||||
fun storeSynchronousProps_overridesStaleOpacityInUpdateProps() {
|
||||
@@ -137,6 +158,80 @@ class SurfaceMountingManagerSynchronousMountPropsTest {
|
||||
assertThat(smm.getView(tag).alpha).isEqualTo(0.2f)
|
||||
}
|
||||
|
||||
/** A null Fabric prop update should not clear the stored synchronous opacity override. */
|
||||
@Test
|
||||
fun updateProps_withNullOpacity_keepsStoredSynchronousProp() {
|
||||
val smm = startSurface()
|
||||
val tag = 42
|
||||
createAndAttachView(smm, tag)
|
||||
|
||||
smm.storeSynchronousMountPropsOverride(tag, JavaOnlyMap.of("opacity", 0.3))
|
||||
smm.updatePropsSynchronously(tag, JavaOnlyMap.of("opacity", 0.3))
|
||||
assertThat(smm.getView(tag).alpha).isEqualTo(0.3f)
|
||||
|
||||
smm.updateProps(tag, JavaOnlyMap.of("opacity", null))
|
||||
|
||||
assertThat(smm.getView(tag).alpha).isEqualTo(0.3f)
|
||||
}
|
||||
|
||||
/** A synchronous null update should clear the stored synchronous opacity override. */
|
||||
@Test
|
||||
fun updatePropsSynchronously_withNullOpacity_removesStoredSynchronousProp() {
|
||||
val smm = startSurface()
|
||||
val tag = 42
|
||||
createAndAttachView(smm, tag)
|
||||
|
||||
smm.storeSynchronousMountPropsOverride(tag, JavaOnlyMap.of("opacity", 0.3))
|
||||
smm.updatePropsSynchronously(tag, JavaOnlyMap.of("opacity", 0.3))
|
||||
assertThat(smm.getView(tag).alpha).isEqualTo(0.3f)
|
||||
|
||||
smm.storeSynchronousMountPropsOverride(tag, JavaOnlyMap.of("opacity", null))
|
||||
smm.updatePropsSynchronously(tag, JavaOnlyMap.of("opacity", null))
|
||||
assertThat(smm.getView(tag).alpha).isEqualTo(1.0f)
|
||||
|
||||
smm.updateProps(tag, JavaOnlyMap.of("opacity", null))
|
||||
|
||||
assertThat(smm.getView(tag).alpha).isEqualTo(1.0f)
|
||||
}
|
||||
|
||||
/** A null Fabric prop update should not clear the stored synchronous transform override. */
|
||||
@Test
|
||||
fun updateProps_withNullTransform_keepsStoredSynchronousProp() {
|
||||
val smm = startSurface()
|
||||
val tag = 42
|
||||
val transform = JavaOnlyArray.of(JavaOnlyMap.of("translateY", 40.0))
|
||||
createAndAttachView(smm, tag)
|
||||
|
||||
smm.storeSynchronousMountPropsOverride(tag, JavaOnlyMap.of("transform", transform))
|
||||
smm.updatePropsSynchronously(tag, JavaOnlyMap.of("transform", transform))
|
||||
assertThat(smm.getView(tag).translationY).isEqualTo(40.0f)
|
||||
|
||||
smm.updateProps(tag, JavaOnlyMap.of("transform", null))
|
||||
|
||||
assertThat(smm.getView(tag).translationY).isEqualTo(40.0f)
|
||||
}
|
||||
|
||||
/** A synchronous null update should clear the stored synchronous transform override. */
|
||||
@Test
|
||||
fun updatePropsSynchronously_withNullTransform_removesStoredSynchronousProp() {
|
||||
val smm = startSurface()
|
||||
val tag = 42
|
||||
val transform = JavaOnlyArray.of(JavaOnlyMap.of("translateY", 40.0))
|
||||
createAndAttachView(smm, tag)
|
||||
|
||||
smm.storeSynchronousMountPropsOverride(tag, JavaOnlyMap.of("transform", transform))
|
||||
smm.updatePropsSynchronously(tag, JavaOnlyMap.of("transform", transform))
|
||||
assertThat(smm.getView(tag).translationY).isEqualTo(40.0f)
|
||||
|
||||
smm.storeSynchronousMountPropsOverride(tag, JavaOnlyMap.of("transform", null))
|
||||
smm.updatePropsSynchronously(tag, JavaOnlyMap.of("transform", null))
|
||||
assertThat(smm.getView(tag).translationY).isEqualTo(0.0f)
|
||||
|
||||
smm.updateProps(tag, JavaOnlyMap.of("transform", null))
|
||||
|
||||
assertThat(smm.getView(tag).translationY).isEqualTo(0.0f)
|
||||
}
|
||||
|
||||
/**
|
||||
* When a view is deleted, stored synchronous props should be cleaned up. A recreated view with
|
||||
* the same tag should not be affected by the old stored props.
|
||||
|
||||
Reference in new issue
Block a user