Repository navigation
Fix "Cannot round NaN value" crash when predictive back gesture finishes with non-monotonic frame time - #1022
arkivanov-bot wants to merge 1 commit into
Conversation
…ishes with non-monotonic frame time SeekableTransitionState.animateTo produces NaN fraction if the frame time goes backwards while animating a transition with zero total duration (e.g. when a custom PredictiveBackAnimatable is used). Run the finishing transition animations with a frame clock that never goes back in time. See #1012.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. 📝 WalkthroughWalkthroughThe completion transition animations now use a frame clock that clamps decreasing timestamps. New predictive-back tests exercise this behavior with content transition animations enabled and disabled. ChangesPredictive-back frame-time handling
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~15 minutes Change: Bug fix Merge Risk: ⚪ Minimal · up to The predictive-back crash path is covered by the fix and regression test. No merge-blocking risk remains after normal checks. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@arkivanov-bot it's mentioned in the issue that it's a Compose bug, reported here: https://issuetracker.google.com/issues/540477169. It was marked as a duplicate of https://issuetracker.google.com/issues/555294641, which was marked as fixed. The actual fix seem to be this: https://android-review.googlesource.com/c/platform/frameworks/support/+/4276642. Please check if the fix has already been released. If there was a release, try updating and see if it was actually fixed. |
|
@arkivanov Checked release status of the upstream Compose fix. Upstream changeGerrit 4276642 (“Prevent NaN in SeekableTransitionState animation frame calculations”) is merged into Released?No — not in any published Maven release yet. Evidence:
So the fix is tip-of-tree only; next published AndroidX line that should carry it is expected to be What Decompose uses
Update attemptSkipped a dependency bump: there is no released AndroidX / CMP version that contains this fix to update to. Bumping to latest published CMP (e.g. 1.12.1 / 1.13.0-alpha01) would still ship the buggy RecommendationKeep the PR #1022 workaround (monotonic frame-clock wrapper in |
|
No, since the main issue in Compose is fixed, I will rather close the issue without adding any workaround to Decompose codebase. |
Some devices (observed on Huawei and Honor) occasionally deliver a Choreographer frame time earlier than the previous frame's. SeekableTransitionState does not tolerate this: while animating a transition whose total duration is zero (a scene change with no enter/exit animations), the negative frame delta produces a NaN fraction and the next seek crashes with "IllegalArgumentException: Cannot round NaN value" from SeekableTransitionState.seekToFraction. In NavigationDisplay this fires when a predictive-back gesture settles. NavigationDisplay now drives its SeekableTransitionState through a NonDecreasingFrameTime guard: a MonotonicFrameClock wrapper that clamps frame times to be non-decreasing. One instance is shared across the seek, animate and settle phases, so a regression across a phase boundary (a predictive-back seek handing over to the settle animation) is caught too. Same class of fix as arkivanov/Decompose#1022 for the identical crash; upstream report: https://issuetracker.google.com/issues/540477169 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Refs #1012 (I didn't use "Fixes" because this hasn't been verified on an affected Huawei/Honor device).
Root cause
The crash comes from
SeekableTransitionStatein animation-core. The relevant code is the same in 1.9.4, which this repo uses via JB Compose 1.9.3, and in 1.11.4, which the reporter uses (Transition.ktlines below are from 1.11.4):animateTo()creates aSeekingAnimationStatewithanimationSpecDuration = totalDurationNanos * (1 - fraction). When the transition has zero total duration, this is0.animateOneFrameLambda(L303) computesdeltaPlayTimeNanos = (frameTimeNanos - lastFrameTimeNanos) / durationScale.durationScaleis always> 0here.recalculateAnimationValue(L398):if (playTimeNanos >= durationNanos) value = 1f else value = lerp(start, 1f, playTimeNanos.toFloat() / durationNanos). WithdurationNanos == 0and a negative play time, that'slerp(0, 1, -Infinity), which isNaN.fraction = NaN, thenseekToFraction()(L733) calls(NaN * totalDurationNanos).roundToLong()and throwsIllegalArgumentException: Cannot round NaN value.So the crash needs two things at once:
PredictiveBackAnimatableis used: the item's animator isSimpleStackAnimator { anim.exitModifier/enterModifier }, which doesn't add animations to the transition. Unless the content has its own transition animations,finish()callsanimateTo()on zero-durationSeekableTransitionStates.Choreographer, since AOSP'sChoreographerexplicitly skips frames whose time goes backwards.Fix
AnimationHandler.finish()now runs the twoSeekableTransitionState.animateTo()calls with aMonotonicFrameClockwrapper that never reports a frame time lower than one it already reported. With a monotonic clock,deltaPlayTimeNanos >= 0, so a zero-duration animation completes withvalue = 1fand never produces NaN. When frame times are already monotonic, the wrapper returns them unchanged, so behaviour doesn't change.I considered snapping zero-duration transitions (
snapTo()instead ofanimateTo()), as suggested in the issue, and decided against it.StackAnimationScopeis anAnimatedVisibilityScope, so content can have transition-driven animations (transition.animateFloat,animateEnterExit, shared elements, etc.). Their duration isn't known until the new target state has been composed (and, for deferred animations, laid out). Checking the duration up front could wrongly skip real animations, and it would also need access to theTransitioninstance fromAnimationHandler.Verification
PredictiveBackNonMonotonicFrameTimeTest(JVM): it drives the fullDefaultStackAnimationpredictive back flow (customPredictiveBackAnimatable, start → progress → back) throughImageComposeScene, so the test controls frame times, and makes the frame time go backwards right afterback().IllegalArgumentException: Cannot round NaN value.and the stack is never popped. The stack trace matches the production one (Recomposer→BroadcastFrameClock.sendFrame→animateOneFrameLambda→seekToFraction→roundToLong).transition.animateFloat()= non-zero duration).:extensions-compose-experimental:jvmTest: all 276 tests pass.compileReleaseKotlinAndroid,compileKotlinJs,compileKotlinWasmJs(-Dsplit_targets),apiCheck. I didn't build the iOS/macOS targets (no macOS host).Caveats
SeekableTransitionStatebehaviour is still there (tracked in https://issuetracker.google.com/issues/540477169). This PR only stops Decompose from triggering it.Summary by CodeRabbit