fix: useDeferredValue gets stuck with stale value when used with use() and Suspense#35932
Open
fresh3nough wants to merge 2 commits intofacebook:mainfrom
Open
fix: useDeferredValue gets stuck with stale value when used with use() and Suspense#35932fresh3nough wants to merge 2 commits intofacebook:mainfrom
fresh3nough wants to merge 2 commits intofacebook:mainfrom
Conversation
…) and Suspense (facebook#35821) When a deferred render suspends on a promise created by useMemo, each retry starts from the committed fiber state where the deps are stale. This causes useMemo to re-execute and create a new pending promise every retry, leading to an infinite suspend-retry loop. The thenableState that would allow trackUsedThenable to reuse the previous promise is cleared on unwind and not preserved across render attempts. For deferred lane renders (TransitionDeferredLanes), instead of immediately unwinding when SuspendedAndReadyToContinue finds the thenable unresolved, transition to SuspendedOnData and register an onResolution listener. This keeps the work-in-progress hooks intact so that when the promise resolves, replaySuspendedUnitOfWork can replay with the same (now-resolved) thenable via the preserved thenableState. The scheduler properly skips the root while suspended on data (isWorkLoopSuspendedOnData), and new urgent updates correctly interrupt the waiting render via scheduleUpdateOnFiber.
eps1lon
reviewed
Feb 28, 2026
packages/react-reconciler/src/__tests__/ReactDeferredValue-test.js
Outdated
Show resolved
Hide resolved
Author
|
Addressed the review feedback: moved the initial promise creation out of the useState initializer and into the test scope. This matches the original issue pattern where promises are created externally (e.g. from server actions) and passed as values to useState, rather than being created during render. Also aligns with the existing idiom in ReactUse-test.js. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #35821
useDeferredValuegets stuck returning a stale value in production builds when combined withuseMemo,use(), andSuspense. The deferred value never catches up after a state change.Root Cause
When a deferred render suspends on a promise created by
useMemo, each retry attempt starts from the committed fiber state where the hook deps are stale. This causesuseMemoto re-execute and create a new pending promise on every retry, leading to an infinite suspend-retry loop:useDeferredValuereturns new value,useMemosees changed deps, creates new promise P1,use(P1)suspendsSuspendedOnImmediate->SuspendedAndReadyToContinue), P1 hasn't resolved yet (production renders are fast, ~5ms yield isn't enough)resetHooksOnUnwindclearsthenableState, P1's ping is registeredprepareFreshStack->createWorkInProgress(root.current)) -- committed memo deps are still stale, souseMemocreates P2. Goto step 3.The
thenableStatethat would allowtrackUsedThenableto reuse the previous promise is only preserved during replays (same render attempt), not across retries.Why dev vs prod differs: In dev mode, rendering is slower (extra checks/warnings), giving more time during the
SuspendedOnImmediateyield for the promise to resolve as a microtask. If it resolves during the yield,isThenableResolvedreturns true andreplaySuspendedUnitOfWorkreplays with the preservedthenableState.Fix
For deferred lane renders (
TransitionDeferredLanes), whenSuspendedAndReadyToContinuefinds the thenable still unresolved, instead of immediately unwinding the stack:SuspendedOnDatastateonResolutionlistener on the thenableThis preserves the work-in-progress hooks (including the
thenableStatewith the pending promise). When the promise resolves:onResolutioncallback changes the reason toSuspendedAndReadyToContinueand schedules the rootisThenableResolvedreturns truereplaySuspendedUnitOfWorkreplays with the preservedthenableState, reusing the now-resolved promiseThis is safe for deferred renders because:
isWorkLoopSuspendedOnData)scheduleUpdateOnFiberTest Plan
useDeferredValue with a promise catches up after rapid updatesReactDeferredValue,ReactUse,ReactSuspenseWithNoopRenderer,ReactCPUSuspense, andReactTransitiontests pass (138+ tests)Reproduction steps from issue
In production Next.js builds, typing in the input causes Results to show stale data that never updates.