Fix crash in tracking spring animations when toValue node is detached (#55734)

Summary:
This PR fixes an Android Native Animated crash in tracking + spring animations when the tracked `toValue` node is detached.

In `TrackingAnimatedNode.update`, if the tracked node could not be resolved, we used to write `toValue = null` into `animationConfig`. `SpringAnimation.resetConfig` expects `toValue` to be a number, so this could crash with `NullPointerException: null cannot be cast to non-null type kotlin.Number`

This can happen in real apps during graph teardown (for example unmount/re-render/navigation transitions), where tracking may still update for a transient frame after the tracked node has been detached.

Fix:
- Do not propagate null `toValue` from `TrackingAnimatedNode`.
- If the tracked node is missing, fallback to the driven value node’s current value.
- If both nodes are unavailable, return safely.

This PR also adds a regression test.

## Changelog:

[ANDROID] [FIXED] - Prevent Native Animated tracking spring crash when tracked `toValue` node is detached.

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

Test Plan:
Added regression test `NativeAnimatedNodeTraversalTest.testTrackingSpringDoesNotCrashWhenToValueNodeIsDetached`
- On a test-only commit (without the fix), the new test fails with: `NullPointerException: null cannot be cast to non-null type kotlin.Number`
- On this PR (with the fix), test pass.

Reviewed By: christophpurrer

Differential Revision: D94697059

Pulled By: zeyap

fbshipit-source-id: c8f1f619c34ce7887d6f9bfb42a035d41d6a063a
This commit is contained in:
Minsik Kim authored and meta-codesync[bot] committed 2026-02-27 13:35:00 -08:00
1 parent ee5b08904b
commit 45de7109fe
2 files changed
+43 -1

No files matched your search

@@ -25,7 +25,11 @@ internal class TrackingAnimatedNode(
if (valAnimatedNode != null) {
animationConfig.putDouble("toValue", valAnimatedNode.getValue())
} else {
animationConfig.putNull("toValue")
val drivenNode = nativeAnimatedNodesManager.getNodeById(valueNode) as? ValueAnimatedNode
if (drivenNode == null) {
return
}
animationConfig.putDouble("toValue", drivenNode.getValue())
}
nativeAnimatedNodesManager.startAnimatingNode(animationId, valueNode, animationConfig, null)
}
@@ -1370,6 +1370,44 @@ class NativeAnimatedNodeTraversalTest {
assertThat(previousValue).isEqualTo(1.5)
}
@Test
fun testTrackingSpringDoesNotCrashWhenToValueNodeIsDetached() {
val springConfig: JavaOnlyMap =
JavaOnlyMap.of(
"type",
"spring",
"stiffness",
230.2,
"damping",
22.0,
"mass",
1.0,
"initialVelocity",
0.0,
"restSpeedThreshold",
0.001,
"restDisplacementThreshold",
0.001,
"overshootClamping",
false,
)
createAnimatedGraphWithTrackingNode(springConfig)
nativeAnimatedNodesManager.setAnimatedNodeValue(1, 1.0)
nativeAnimatedNodesManager.runUpdates(nextFrameTime())
nativeAnimatedNodesManager.disconnectAnimatedNodes(1, 2)
nativeAnimatedNodesManager.dropAnimatedNode(1)
nativeAnimatedNodesManager.runUpdates(nextFrameTime())
val trackedNode = nativeAnimatedNodesManager.getNodeById(3) as? ValueAnimatedNode
assertThat(trackedNode).isNotNull
val trackedValue = checkNotNull(trackedNode).getValue()
assertThat(trackedValue.isNaN()).isFalse
}
companion object {
private const val FRAME_LEN_NANOS: Long = 1000000000L / 60L
private const val INITIAL_FRAME_TIME_NANOS: Long = 14599233201256L /* random */