Skip to content

Commit 8bc527a

Browse files
authored
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.
1 parent f7cf077 commit 8bc527a

5 files changed

+78
-4
lines changed

packages/react-reconciler/src/ReactFiberInterleavedUpdates.new.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export function pushInterleavedQueue(
2828
}
2929
}
3030

31+
export function hasInterleavedUpdates() {
32+
return interleavedQueues !== null;
33+
}
34+
3135
export function enqueueInterleavedUpdates() {
3236
// Transfer the interleaved updates onto the main queue. Each queue has a
3337
// `pending` field and an `interleaved` field. When they are not null, they

packages/react-reconciler/src/ReactFiberInterleavedUpdates.old.js

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ export function pushInterleavedQueue(
2828
}
2929
}
3030

31+
export function hasInterleavedUpdates() {
32+
return interleavedQueues !== null;
33+
}
34+
3135
export function enqueueInterleavedUpdates() {
3236
// Transfer the interleaved updates onto the main queue. Each queue has a
3337
// `pending` field and an `interleaved` field. When they are not null, they

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ import {
195195
pop as popFromStack,
196196
createCursor,
197197
} from './ReactFiberStack.new';
198-
import {enqueueInterleavedUpdates} from './ReactFiberInterleavedUpdates.new';
198+
import {
199+
enqueueInterleavedUpdates,
200+
hasInterleavedUpdates,
201+
} from './ReactFiberInterleavedUpdates.new';
199202

200203
import {
201204
markNestedUpdateScheduled,
@@ -723,7 +726,13 @@ export function isInterleavedUpdate(fiber: Fiber, lane: Lane) {
723726
// TODO: Optimize slightly by comparing to root that fiber belongs to.
724727
// Requires some refactoring. Not a big deal though since it's rare for
725728
// 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) &&
727736
(fiber.mode & ConcurrentMode) !== NoMode &&
728737
// If this is a render phase update (i.e. UNSAFE_componentWillReceiveProps),
729738
// then don't treat this as an interleaved update. This pattern is

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ import {
195195
pop as popFromStack,
196196
createCursor,
197197
} from './ReactFiberStack.old';
198-
import {enqueueInterleavedUpdates} from './ReactFiberInterleavedUpdates.old';
198+
import {
199+
enqueueInterleavedUpdates,
200+
hasInterleavedUpdates,
201+
} from './ReactFiberInterleavedUpdates.old';
199202

200203
import {
201204
markNestedUpdateScheduled,
@@ -723,7 +726,13 @@ export function isInterleavedUpdate(fiber: Fiber, lane: Lane) {
723726
// TODO: Optimize slightly by comparing to root that fiber belongs to.
724727
// Requires some refactoring. Not a big deal though since it's rare for
725728
// 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) &&
727736
(fiber.mode & ConcurrentMode) !== NoMode &&
728737
// If this is a render phase update (i.e. UNSAFE_componentWillReceiveProps),
729738
// then don't treat this as an interleaved update. This pattern is

packages/react-reconciler/src/__tests__/ReactInterleavedUpdates-test.js

+48
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,52 @@ describe('ReactInterleavedUpdates', () => {
140140
expect(Scheduler).toHaveYielded([2, 2, 2]);
141141
expect(root).toMatchRenderedOutput('222');
142142
});
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+
});
143191
});

0 commit comments

Comments
 (0)