Skip to content

Android: a view command dispatched from a UI-thread animation's frame callback runs in the next frame #60441

Backlog

Description

@YevheniiKotyrlo

Description

I found this while keeping a focused text field above the soft keyboard on Android, with a worklet that calls Reanimated's scrollTo on the field's ScrollView in each frame of the keyboard's animation: the scroll trailed the keyboard.

On Android, a view command dispatched on the UI thread from a frame callback that runs after React Native's mount pass — the NATIVE_ANIMATED_MODULE queue UI-thread animations use — runs in the next frame, not in the frame that dispatched it. A view moved in the same callback is at its new place when that frame draws, while the ScrollView is still at the offset the frame before asked for.

FabricUIManager.dispatchCommand only queues the command (FabricUIManager.java#L1372-L1390):

      mMountItemDispatcher.addViewCommandMountItem(
          MountItemFactory.createDispatchCommandMountItem(
              surfaceId, reactTag, commandId, commandArgs));

The queue is dispatched by DispatchUIFrameCallback (FabricUIManager.java#L1737), posted to ReactChoreographer's DISPATCH_UI queue (FabricUIManager.java#L1673), which runs before NATIVE_ANIMATED_MODULE in each frame (ReactChoreographer.kt#L26-L39, #L47-L66). So a command dispatched from a NATIVE_ANIMATED_MODULE callback, the queue react-native-worklets runs its frames in (AnimationFrameQueue.kt#L42), waits for the next frame's DISPATCH_UI. A mount item scheduled on the UI thread does not wait: scheduleMountItem dispatches it at once (FabricUIManager.java#L1028-L1038).

Reanimated's scrollTo reaches this method through SchedulerDelegate::schedulerDidDispatchCommand (ReanimatedModuleProxy.cpp#L1262, FabricUIManagerBinding.cpp#L823-L832, FabricMountingManager.cpp#L1283-L1300). The reproducer calls FabricUIManager.dispatchCommand from Kotlin, so it needs no library.

A ScrollView whose scroll indicator is hidden also draws a scrollTo command's new offset one frame late, which I reported in #58186 (comment).

Steps to reproduce

  1. Clone https://github.com/YevheniiKotyrlo/rn-ui-thread-view-command-frame-late and attach one Android device or emulator.
  2. Run ./reproduce.sh. It builds the release APK, installs and starts it. MainActivity.kt scrolls a ScrollView from a frame callback posted to ReactChoreographer's NATIVE_ANIMATED_MODULE queue, 8 dp further each frame for 20 frames, and moves a sibling view by the same offset in the same callback: first with View.scrollTo, then with FabricUIManager.dispatchCommand(surfaceId, reactTag, "scrollTo", [0, y, false]). For each frame it logs the scrollY it asked for and the ScrollView's scrollY in that frame's OnPreDrawListener.
  3. Read the log the script prints.

Expected: each frame of the command phase draws at the scrollY it asked for, as each frame of the View.scrollTo phase does.

Seen: each frame of the command phase draws at the scrollY the frame before asked for.

React Native Version

0.90.0-nightly-20261201-13d48c3d8

Affected Platforms

Runtime - Android, Build - Windows

Output of npx @react-native-community/cli info

System:
  OS: Windows 11 10.0.26300
  CPU: "(32) x64 AMD Ryzen 9 9950X3D 16-Core Processor          "
  Memory: 96.26 GB / 255.67 GB
Binaries:
  Node:
    version: 26.13.1
    path: C:\Program Files\nodejs\node.EXE
  Yarn:
    version: 1.22.22
    path: ~\AppData\Roaming\npm\yarn.CMD
  npm:
    version: 12.2.1
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
  Windows SDK:
    AllowDevelopmentWithoutDevLicense: Enabled
    Versions:
      - 10.0.19041.0
      - 10.0.26100.0
[…]
Languages:
  Java:
    version: 17.0.20.1
    path: C:\Program Files\Microsoft\jdk-17.0.20.101-hotspot\bin\javac.EXE
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli":
    installed: 21.0.0
    wanted: 21.0.0
  react:
    installed: 19.3.0
    wanted: 19.3.0
  react-native:
    installed: 0.90.0-nightly-20261201-13d48c3d8
    wanted: 0.90.0-nightly-20261201-13d48c3d8
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

[…]
device: sdk_gphone16k_x86_64, Android 17 (API 37), x86_64
react-native 0.90.0-nightly-20261201-13d48c3d8
[…]
== the app scrolls the ScrollView 20 frames with View.scrollTo, then 20 frames with the scrollTo command
[…]
direct frame 1: asked for scrollY 21, scrollY at this frame's draw 21, marker translationY -21
direct frame 2: asked for scrollY 42, scrollY at this frame's draw 42, marker translationY -42
[…]
direct frame 20: asked for scrollY 420, scrollY at this frame's draw 420, marker translationY -420
command frame 1: asked for scrollY 21, scrollY at this frame's draw 0, marker translationY -21
command frame 2: asked for scrollY 42, scrollY at this frame's draw 21, marker translationY -42
command frame 3: asked for scrollY 63, scrollY at this frame's draw 42, marker translationY -63
[…]
command frame 20: asked for scrollY 420, scrollY at this frame's draw 399, marker translationY -420
done
direct: 20 of 20 frames drew at the scrollY they asked for, 0 at the scrollY the frame before asked for
command: 0 of 20 frames drew at the scrollY they asked for, 20 at the scrollY the frame before asked for
SUMMARY: 20 of 20 frames that dispatched the scrollTo command on the UI thread drew at the scrollY the frame before asked for; View.scrollTo in the same callback drew at the scrollY it asked for in 20 of 20

MANDATORY Reproducer

https://github.com/YevheniiKotyrlo/rn-ui-thread-view-command-frame-late

Screenshots and Videos

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions