Commit d1bb1c5
authored
Fix memory leak after repeated setState bailouts (#25309)
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.1 parent 0cac4d5 commit d1bb1c5
File tree
4 files changed
+38
-0
lines changed- packages/react-reconciler/src
4 files changed
+38
-0
lines changedLines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
139 | 140 | | |
140 | 141 | | |
141 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
142 | 155 | | |
143 | 156 | | |
144 | 157 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
139 | 140 | | |
140 | 141 | | |
141 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
142 | 155 | | |
143 | 156 | | |
144 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1912 | 1912 | | |
1913 | 1913 | | |
1914 | 1914 | | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
1915 | 1918 | | |
1916 | 1919 | | |
1917 | 1920 | | |
| |||
2017 | 2020 | | |
2018 | 2021 | | |
2019 | 2022 | | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
2020 | 2026 | | |
2021 | 2027 | | |
2022 | 2028 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1912 | 1912 | | |
1913 | 1913 | | |
1914 | 1914 | | |
| 1915 | + | |
| 1916 | + | |
| 1917 | + | |
1915 | 1918 | | |
1916 | 1919 | | |
1917 | 1920 | | |
| |||
2017 | 2020 | | |
2018 | 2021 | | |
2019 | 2022 | | |
| 2023 | + | |
| 2024 | + | |
| 2025 | + | |
2020 | 2026 | | |
2021 | 2027 | | |
2022 | 2028 | | |
| |||
0 commit comments