Skip to content

Stale closure inside useAnimatedScrollHandler  #5360

Closed

Description

@gaearon

Description

I suspect this is related to #5345, but not sure if the same issue. This time I do have a repro.

This bug feels extremely worrying — it's like the most basic expectations don't hold. Am I doing something wrong? We use this API quite a lot so I'm getting nervous about whether it's safe to rely on it.

Steps to reproduce

Try to scroll these two flat lists independently:

import * as React from 'react'
import {View} from 'react-native'
import Animated, {useAnimatedScrollHandler} from 'react-native-reanimated'

export function PagerWithHeader() {
  const [x, setX] = React.useState(0)
  React.useEffect(() => {
    let id = setInterval(() => setX(x => x + 1), 1000)
    return () => clearInterval(id)
  }, [])

  const onScroll = useAnimatedScrollHandler({
    onScroll(e) {
      console.log(x)
    },
  })

  return (
    <View style={{flex: 1, flexDirection: 'row'}}>
      <Animated.FlatList
        onScroll={onScroll}
        style={{flex: 1, backgroundColor: 'red'}}
        contentContainerStyle={{
          paddingTop: 1000,
        }}
      />
      <Animated.FlatList
        onScroll={onScroll}
        style={{flex: 1, backgroundColor: 'blue'}}
        contentContainerStyle={{
          paddingTop: 1000,
        }}
      />
    </View>
  )
}

Android and iOS

Scrolling the first one will output two logs every time:

 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  179
 LOG  0
 LOG  180
 LOG  0
 LOG  180
 LOG  0

One of the logs shows the counter being stale.

Scrolling the second one does not have any effect at all—the event does not get fired.

Web

On the web, the event gets fired for both views—but the state inside is always stale.

0
0
0
0
0

In fact, on the web the closure is stale even when passed to only one handler (try deleting one of the flat lists).

Snack

I tried reproducing it in a Snack but couldn't get this working. However, copy pasting the code above into my App.js demonstrates the bug.

GitHub repro

git clone https://github.com/bluesky-social/social-app.git
cd social-app

git checkout reanimated-stuck-onscroll-repro
yarn
yarn start --clear

The related code is in App.native.tsx.

Snack or a link to a repository

https://github.com/bluesky-social/social-app/tree/reanimated-stuck-onscroll-repro

Reanimated version

3.4.2

React Native version

0.72.5

Platforms

Android, iOS, Web

JavaScript runtime

Hermes

Workflow

None

Architecture

Paper (Old Architecture)

Build type

None

Device

None

Device model

No response

Acknowledgements

Yes

Activity

  1. added
    Platform: iOSThis issue is specific to iOS
    Platform: WebThis issue is specific to web
    Repro providedA reproduction with a snippet of code, snack or repo is provided
    on Nov 10, 2023
  2. changed the title [-]Stale closure inside useAnimatedScrollHandler when passed to more than one handler[/-] [+]Stale closure inside useAnimatedScrollHandler [/+] on Nov 10, 2023
  3. self-assigned this
    on Dec 15, 2023
  4. tjzel commented on Dec 17, 2023

    @tjzel
    Collaborator

    Hi @gaearon, it's the same issue.

    What's happening here is the following:

    • useAnimatedScrollHandler has its dependencies changed. Therefore it flags its worklet handler as to be rebuild.
    • Rebuilding happens on component re-render. In this case, the first component sees that its worklet handler has to be rebuild, calls the rebuild function then reattaches it. Then it disabled the rebuild flag.
    • The next component on re-render sees that the handler doesn't need to be rebuild thus doesn't reattach it with the new worklet.

    I gave some more information about using useAnimatedScrollHandler with multiple components here.

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

Metadata

Metadata

Assignees

Labels

Needs reviewIssue is ready to be reviewed by a maintainerPlatform: AndroidThis issue is specific to AndroidPlatform: WebThis issue is specific to webPlatform: iOSThis issue is specific to iOSRepro providedA reproduction with a snippet of code, snack or repo is provided

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions