From 45de7109fe93db8052e9ba06e53807f89cbd7fd9 Mon Sep 17 00:00:00 2001 From: Minsik Kim Date: Fri, 27 Feb 2026 13:35:00 -0800 Subject: [PATCH] Fix crash in tracking spring animations when toValue node is detached (#55734) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../react/animated/TrackingAnimatedNode.kt | 6 ++- .../NativeAnimatedNodeTraversalTest.kt | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TrackingAnimatedNode.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TrackingAnimatedNode.kt index 1a355743fda..dbc491a10b3 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TrackingAnimatedNode.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/animated/TrackingAnimatedNode.kt @@ -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) } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt index dfb2afefec9..c4f6eac45c7 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/animated/NativeAnimatedNodeTraversalTest.kt @@ -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 */