From 46604caa2d764cdf785fc5b2d3877037eacb5dfc Mon Sep 17 00:00:00 2001 From: leeguooooo Date: Mon, 29 Dec 2025 11:57:45 +0900 Subject: [PATCH] Fix lazy thenable suspension race --- .../src/ReactFiberThenable.js | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberThenable.js b/packages/react-reconciler/src/ReactFiberThenable.js index 643be63ffa1..6ee7cc6c505 100644 --- a/packages/react-reconciler/src/ReactFiberThenable.js +++ b/packages/react-reconciler/src/ReactFiberThenable.js @@ -294,22 +294,26 @@ export function suspendCommit(): void { throw SuspenseyCommitException; } +function initLazy(lazyType: LazyComponentType): T { + if (__DEV__) { + return callLazyInitInDEV(lazyType); + } + const payload = lazyType._payload; + const init = lazyType._init; + return init(payload); +} + export function resolveLazy(lazyType: LazyComponentType): T { try { - if (__DEV__) { - return callLazyInitInDEV(lazyType); - } - const payload = lazyType._payload; - const init = lazyType._init; - return init(payload); + return initLazy(lazyType); } catch (x) { if (x !== null && typeof x === 'object' && typeof x.then === 'function') { - // This lazy Suspended. Treat this as if we called use() to unwrap it. - suspendedThenable = x; - if (__DEV__) { - needsToResetSuspendedThenableDEV = true; - } - throw SuspenseException; + // This lazy suspended. Treat this as if we called use() to unwrap it, + // so we attach the same thenable bookkeeping immediately. + const thenableState = createThenableState(); + trackUsedThenable(thenableState, (x: any), 0); + // If the thenable resolved synchronously, try again. + return initLazy(lazyType); } throw x; }