Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak after repeated setState bailouts #25309

Merged
merged 1 commit into from
Sep 22, 2022

Commits on Sep 21, 2022

  1. Fix memory leak after repeated setState bailouts

    There's a global queue (`concurrentQueues` in the
    ReactFiberConcurrentUpdates module) that is cleared at the beginning of
    each render phase.
    
    However, in the case of an eager `setState` bailout where the state is
    updated to same value as the current one, we add the update to the queue
    without scheduling a render. So the render phase never removes it from
    the queue. This can lead to a memory leak if it happens repeatedly
    without any other updates.
    
    There's only one place where this ever happens, so the fix was pretty
    straightforward.
    
    Currently there's no great way to test this from a Jest test, so I
    confirmed locally by checking in an existing test whether the array gets
    reset. @sompylasar had an interesting suggestion for how to catch these
    in the future: in the development build (perhaps behind a flag), use a
    Babel plugin to instrument all module-level variables. Then periodically
    sweep to confirm if something has leaked. The logic is that if there's
    no React work scheduled, and a module-level variable points to an
    object, it very likely indicates a memory leak.
    acdlite committed Sep 21, 2022
    Configuration menu
    Copy the full SHA
    984fcb0 View commit details
    Browse the repository at this point in the history