@samitouri / QOS-React-2 / commits / 8bc527a4cf

Bugfix: Fix race condition between interleaved and non-interleaved updates (#24353)

* Regression test: Interleaved update race condition Demonstrates the bug reported in #24350. * Bugfix: Last update wins, even if interleaved "Interleaved" updates are updates that are scheduled while a render is already in progress. We put these on a special queue so that they don't get processed during the current render. Then we transfer them to the "real" queue after the render has finished. There was a race condition where an update is received after the render has finished but before the interleaved update queue had been transferred, causing the updates to be queued in the wrong order. The fix I chose is to check if the interleaved updates queue is empty before adding any update to the real queue. If it's not empty, then the new update must also be treated as interleaved.

Andrew Clark committed Apr 12, 2022 at 15:39 UTC 8bc527a4cf3e7e8b15ad0312cd49b438181103f2
5 files changed +78 -4
packages/react-reconciler/src/ReactFiberInterleavedUpdates.new.js
+4
@@ -28,6 +28,10 @@ export function pushInterleavedQueue(
28 }
29 }
30
31 +export function hasInterleavedUpdates() {
32 + return interleavedQueues !== null;
33 +}
34 +
35 export function enqueueInterleavedUpdates() {
36 // Transfer the interleaved updates onto the main queue. Each queue has a
37 // `pending` field and an `interleaved` field. When they are not null, they
packages/react-reconciler/src/ReactFiberInterleavedUpdates.old.js
+4
@@ -28,6 +28,10 @@ export function pushInterleavedQueue(
28 }
29 }
30
31 +export function hasInterleavedUpdates() {
32 + return interleavedQueues !== null;
33 +}
34 +
35 export function enqueueInterleavedUpdates() {
36 // Transfer the interleaved updates onto the main queue. Each queue has a
37 // `pending` field and an `interleaved` field. When they are not null, they
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+11 -2
@@ -195,7 +195,10 @@ import {
195 pop as popFromStack,
196 createCursor,
197 } from './ReactFiberStack.new';
198 -import {enqueueInterleavedUpdates} from './ReactFiberInterleavedUpdates.new';
198 +import {
199 + enqueueInterleavedUpdates,
200 + hasInterleavedUpdates,
201 +} from './ReactFiberInterleavedUpdates.new';
202
203 import {
204 markNestedUpdateScheduled,
@@ -723,7 +726,13 @@ export function isInterleavedUpdate(fiber: Fiber, lane: Lane) {
726 // TODO: Optimize slightly by comparing to root that fiber belongs to.
727 // Requires some refactoring. Not a big deal though since it's rare for
728 // concurrent apps to have more than a single root.
726 - workInProgressRoot !== null &&
729 + (workInProgressRoot !== null ||
730 + // If the interleaved updates queue hasn't been cleared yet, then
731 + // we should treat this as an interleaved update, too. This is also a
732 + // defensive coding measure in case a new update comes in between when
733 + // rendering has finished and when the interleaved updates are transferred
734 + // to the main queue.
735 + hasInterleavedUpdates() !== null) &&
736 (fiber.mode & ConcurrentMode) !== NoMode &&
737 // If this is a render phase update (i.e. UNSAFE_componentWillReceiveProps),
738 // then don't treat this as an interleaved update. This pattern is
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+11 -2
@@ -195,7 +195,10 @@ import {
195 pop as popFromStack,
196 createCursor,
197 } from './ReactFiberStack.old';
198 -import {enqueueInterleavedUpdates} from './ReactFiberInterleavedUpdates.old';
198 +import {
199 + enqueueInterleavedUpdates,
200 + hasInterleavedUpdates,
201 +} from './ReactFiberInterleavedUpdates.old';
202
203 import {
204 markNestedUpdateScheduled,
@@ -723,7 +726,13 @@ export function isInterleavedUpdate(fiber: Fiber, lane: Lane) {
726 // TODO: Optimize slightly by comparing to root that fiber belongs to.
727 // Requires some refactoring. Not a big deal though since it's rare for
728 // concurrent apps to have more than a single root.
726 - workInProgressRoot !== null &&
729 + (workInProgressRoot !== null ||
730 + // If the interleaved updates queue hasn't been cleared yet, then
731 + // we should treat this as an interleaved update, too. This is also a
732 + // defensive coding measure in case a new update comes in between when
733 + // rendering has finished and when the interleaved updates are transferred
734 + // to the main queue.
735 + hasInterleavedUpdates() !== null) &&
736 (fiber.mode & ConcurrentMode) !== NoMode &&
737 // If this is a render phase update (i.e. UNSAFE_componentWillReceiveProps),
738 // then don't treat this as an interleaved update. This pattern is
packages/react-reconciler/src/__tests__/ReactInterleavedUpdates-test.js
+48
@@ -140,4 +140,52 @@ describe('ReactInterleavedUpdates', () => {
140 expect(Scheduler).toHaveYielded([2, 2, 2]);
141 expect(root).toMatchRenderedOutput('222');
142 });
143 +
144 + test('regression for #24350: does not add to main update queue until interleaved update queue has been cleared', async () => {
145 + let setStep;
146 + function App() {
147 + const [step, _setState] = useState(0);
148 + setStep = _setState;
149 + return (
150 + <>
151 + <Text text={'A' + step} />
152 + <Text text={'B' + step} />
153 + <Text text={'C' + step} />
154 + </>
155 + );
156 + }
157 +
158 + const root = ReactNoop.createRoot();
159 + await act(async () => {
160 + root.render(<App />);
161 + });
162 + expect(Scheduler).toHaveYielded(['A0', 'B0', 'C0']);
163 + expect(root).toMatchRenderedOutput('A0B0C0');
164 +
165 + await act(async () => {
166 + // Start the render phase.
167 + startTransition(() => {
168 + setStep(1);
169 + });
170 + expect(Scheduler).toFlushAndYieldThrough(['A1', 'B1']);
171 +
172 + // Schedule an interleaved update. This gets placed on a special queue.
173 + startTransition(() => {
174 + setStep(2);
175 + });
176 +
177 + // Finish rendering the first update.
178 + expect(Scheduler).toFlushUntilNextPaint(['C1']);
179 +
180 + // Schedule another update. (In the regression case, this was treated
181 + // as a normal, non-interleaved update and it was inserted into the queue
182 + // before the interleaved one was processed.)
183 + startTransition(() => {
184 + setStep(3);
185 + });
186 + });
187 + // The last update should win.
188 + expect(Scheduler).toHaveYielded(['A3', 'B3', 'C3']);
189 + expect(root).toMatchRenderedOutput('A3B3C3');
190 + });
191 });