Skip to content

Bug: memory leak in useReducer #21692

Closed as not planned
Closed as not planned
@onnlucky

Description

@onnlucky

React version: 17.0.2

When using React.useReducer() and a new action is pushed, but the reducer returns the current state, the operation is queued in a pending queue. But if the component isn't re-rendered for other reasons, it will never start work on that pending queue. This can potentially rack up a huge list of pending.next.next.next.next...

Steps To Reproduce

  1. https://codesandbox.io/s/unruffled-moore-wp5wq?file=/src/App.js
  2. click increment a few times
  3. place a breakpoint in react-dom.development.js line 16072, click increment, that should hit the break point, then expand queue.pending.next.next.next and see they are unique actions. (
    const pending = queue.pending;
    )

Link to code example: https://codesandbox.io/s/unruffled-moore-wp5wq?file=/src/App.js

The current behavior

There is no change in behavior, but it is potentially racking up an enormous list of pending tasks, costing memory.

The expected behavior

Flat memory usage. Reducer doesn't run twice on the same action.

This is very much related to #15198 because later the reducer might run, and basically reduce the same actions again. Which is unexpected. And if using side-effects, doesn't produce the expected results.

Other related bugs: #17953 #15088

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution: StaleAutomatically closed due to inactivityStatus: UnconfirmedA potential issue that we haven't yet confirmed as a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions