Skip to content

Commit 4db7a10

Browse files
hsourceRiccardo Cipolleschi
authored and
Riccardo Cipolleschi
committed
Prevent crash in runAnimationStep on OnePlus and Oppo devices (#37487)
Summary: We've been encountering a crash in `runAnimationStep` with "Calculated frame index should never be lower than 0" #35766 with OnePlus/Oppo devices as well, but don't have one on hand to test. This just works around the issue: if the time is before the start time of an animation, we shouldn't do anything anyways, so we just log a message instead of throwing while in production. We still throw in debug mode though for easier debugging. ### Hypothesis of the root cause Based on stacktrace in #35766 (which is the same one we see) Normally, this should happen 1. Choreographer.java constructs a FrameDisplayEventReceiver 2. FrameDisplayEventReceiver.onVSync gets called, which sets the `mTimestampNanos` 3. FrameDisplayEventReceiver.run gets called, which then eventually calls our `doFrame` callback with `mTimestampNanos`. This then causes `FrameBasedAnimationDriver.runAnimationStep` to be called with the same timestamp I suspect what's happening on OnePlus devices is that the `onVSync` call either doesn't happen or happens rarely enough that the `mTimestampNanos` when `run` is called is sometime in the past ### Fix 1. Add logging so we get the parameters to debug more if we end up getting this error 2. In production, just ignore past times instead of throwing an Error ## Changelog: Pick one each for the category and type tags: [ANDROID] [FIXED] - Prevent crash on OnePlus/Oppo devices in runAnimationStep Pull Request resolved: #37487 Test Plan: Ran our app using patched version and verified no issues showed up when using it Reviewed By: cipolleschi Differential Revision: D46102968 Pulled By: cortinico fbshipit-source-id: bcb36a0c2aed0afdb8e7e68b141a3db4eb02695a
1 parent 56807fa commit 4db7a10

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/animated/FrameBasedAnimationDriver.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
package com.facebook.react.animated;
99

10+
import com.facebook.common.logging.FLog;
1011
import com.facebook.react.bridge.ReadableArray;
1112
import com.facebook.react.bridge.ReadableMap;
1213
import com.facebook.react.bridge.ReadableType;
14+
import com.facebook.react.common.ReactConstants;
15+
import com.facebook.react.common.build.ReactBuildConfig;
1316

1417
/**
1518
* Implementation of {@link AnimationDriver} which provides a support for simple time-based
@@ -70,7 +73,17 @@ public void runAnimationStep(long frameTimeNanos) {
7073
long timeFromStartMillis = (frameTimeNanos - mStartFrameTimeNanos) / 1000000;
7174
int frameIndex = (int) Math.round(timeFromStartMillis / FRAME_TIME_MILLIS);
7275
if (frameIndex < 0) {
73-
throw new IllegalStateException("Calculated frame index should never be lower than 0");
76+
String message =
77+
"Calculated frame index should never be lower than 0. Called with frameTimeNanos "
78+
+ frameTimeNanos
79+
+ " and mStartFrameTimeNanos "
80+
+ mStartFrameTimeNanos;
81+
if (ReactBuildConfig.DEBUG) {
82+
throw new IllegalStateException(message);
83+
} else {
84+
FLog.w(ReactConstants.TAG, message);
85+
return;
86+
}
7487
} else if (mHasFinished) {
7588
// nothing to do here
7689
return;

0 commit comments

Comments
 (0)