-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Account for another DevTools + Fast Refresh edge case #21523
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -717,7 +717,7 @@ export function attach( | |
? getFiberIDUnsafe(parentFiber) || '<no-id>' | ||
: ''; | ||
|
||
console.log( | ||
console.groupCollapsed( | ||
`[renderer] %c${name} %c${displayName} (${maybeID}) %c${ | ||
parentFiber ? `${parentDisplayName} (${maybeParentID})` : '' | ||
} %c${extraString}`, | ||
|
@@ -726,6 +726,13 @@ export function attach( | |
'color: purple;', | ||
'color: black;', | ||
); | ||
console.log( | ||
new Error().stack | ||
.split('\n') | ||
.slice(1) | ||
.join('\n'), | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't necessary as part of the PR but I found it helpful when debugging things. |
||
console.groupEnd(); | ||
} | ||
}; | ||
|
||
|
@@ -996,7 +1003,9 @@ export function attach( | |
} | ||
} | ||
|
||
let didGenerateID = false; | ||
if (id === null) { | ||
didGenerateID = true; | ||
id = getUID(); | ||
} | ||
|
||
|
@@ -1019,6 +1028,17 @@ export function attach( | |
} | ||
} | ||
|
||
if (__DEBUG__) { | ||
if (didGenerateID) { | ||
debug( | ||
'getOrGenerateFiberID()', | ||
fiber, | ||
fiber.return, | ||
'Generated a new UID', | ||
); | ||
} | ||
} | ||
|
||
return refinedID; | ||
} | ||
|
||
|
@@ -1050,19 +1070,61 @@ export function attach( | |
// Removes a Fiber (and its alternate) from the Maps used to track their id. | ||
// This method should always be called when a Fiber is unmounting. | ||
function untrackFiberID(fiber: Fiber) { | ||
const fiberID = getFiberIDUnsafe(fiber); | ||
if (fiberID !== null) { | ||
idToArbitraryFiberMap.delete(fiberID); | ||
if (__DEBUG__) { | ||
debug('untrackFiberID()', fiber, fiber.return, 'schedule after delay'); | ||
} | ||
|
||
fiberToIDMap.delete(fiber); | ||
// Untrack Fibers after a slight delay in order to support a Fast Refresh edge case: | ||
// 1. Component type is updated and Fast Refresh schedules an update+remount. | ||
// 2. flushPendingErrorsAndWarningsAfterDelay() runs, sees the old Fiber is no longer mounted | ||
// (it's been disconnected by Fast Refresh), and calls untrackFiberID() to clear it from the Map. | ||
// 3. React flushes pending passive effects before it runs the next render, | ||
// which logs an error or warning, which causes a new ID to be generated for this Fiber. | ||
// 4. DevTools now tries to unmount the old Component with the new ID. | ||
// | ||
// The underlying problem here is the premature clearing of the Fiber ID, | ||
// but DevTools has no way to detect that a given Fiber has been scheduled for Fast Refresh. | ||
// (The "_debugNeedsRemount" flag won't necessarily be set.) | ||
// | ||
// The best we can do is to delay untracking by a small amount, | ||
// and give React time to process the Fast Refresh delay. | ||
|
||
const {alternate} = fiber; | ||
if (alternate !== null) { | ||
fiberToIDMap.delete(alternate); | ||
untrackFibersSet.add(fiber); | ||
|
||
if (untrackFibersTimeoutID === null) { | ||
untrackFibersTimeoutID = setTimeout(untrackFibers, 1000); | ||
} | ||
} | ||
|
||
const untrackFibersSet: Set<Fiber> = new Set(); | ||
let untrackFibersTimeoutID: TimeoutID | null = null; | ||
|
||
function untrackFibers() { | ||
if (untrackFibersTimeoutID !== null) { | ||
clearTimeout(untrackFibersTimeoutID); | ||
untrackFibersTimeoutID = null; | ||
} | ||
|
||
untrackFibersSet.forEach(fiber => { | ||
const fiberID = getFiberIDUnsafe(fiber); | ||
if (fiberID !== null) { | ||
idToArbitraryFiberMap.delete(fiberID); | ||
|
||
// Also clear any errors/warnings associated with this fiber. | ||
clearErrorsForFiberID(fiberID); | ||
clearWarningsForFiberID(fiberID); | ||
} | ||
|
||
fiberToIDMap.delete(fiber); | ||
|
||
const {alternate} = fiber; | ||
if (alternate !== null) { | ||
fiberToIDMap.delete(alternate); | ||
} | ||
}); | ||
untrackFibersSet.clear(); | ||
} | ||
|
||
function getChangeDescription( | ||
prevFiber: Fiber | null, | ||
nextFiber: Fiber, | ||
|
@@ -1607,13 +1669,13 @@ export function attach( | |
} | ||
|
||
function recordMount(fiber: Fiber, parentFiber: Fiber | null) { | ||
const isRoot = fiber.tag === HostRoot; | ||
const id = getOrGenerateFiberID(fiber); | ||
|
||
if (__DEBUG__) { | ||
debug('recordMount()', fiber, parentFiber); | ||
} | ||
|
||
const isRoot = fiber.tag === HostRoot; | ||
const id = getOrGenerateFiberID(fiber); | ||
|
||
const hasOwnerMetadata = fiber.hasOwnProperty('_debugOwner'); | ||
const isProfilingSupported = fiber.hasOwnProperty('treeBaseDuration'); | ||
|
||
|
@@ -1745,6 +1807,9 @@ export function attach( | |
// This reduces the chance of stack overflow for wide trees (e.g. lists with many items). | ||
let fiber: Fiber | null = firstChild; | ||
while (fiber !== null) { | ||
// Generate an ID even for filtered Fibers, in case it's needed later (e.g. for Profiling). | ||
getOrGenerateFiberID(fiber); | ||
|
||
if (__DEBUG__) { | ||
debug('mountFiberRecursively()', fiber, parentFiber); | ||
} | ||
|
@@ -1758,9 +1823,6 @@ export function attach( | |
const shouldIncludeInTree = !shouldFilterFiber(fiber); | ||
if (shouldIncludeInTree) { | ||
recordMount(fiber, parentFiber); | ||
} else { | ||
// Generate an ID even for filtered Fibers, in case it's needed later (e.g. for Profiling). | ||
getOrGenerateFiberID(fiber); | ||
} | ||
|
||
if (traceUpdatesEnabled) { | ||
|
@@ -2005,12 +2067,12 @@ export function attach( | |
parentFiber: Fiber | null, | ||
traceNearestHostComponentUpdate: boolean, | ||
): boolean { | ||
const id = getOrGenerateFiberID(nextFiber); | ||
|
||
if (__DEBUG__) { | ||
debug('updateFiberRecursively()', nextFiber, parentFiber); | ||
} | ||
|
||
const id = getOrGenerateFiberID(nextFiber); | ||
|
||
if (traceUpdatesEnabled) { | ||
const elementType = getElementTypeForFiber(nextFiber); | ||
if (traceNearestHostComponentUpdate) { | ||
|
@@ -2319,6 +2381,10 @@ export function attach( | |
const current = root.current; | ||
const alternate = current.alternate; | ||
|
||
// Flush any pending Fibers that we are untracking before processing the new commit. | ||
// If we don't do this, we might end up double-deleting Fibers in some cases (like Legacy Suspense). | ||
untrackFibers(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Store stress test for legacy Suspense caught this issue 👍🏼 |
||
|
||
currentRootID = getOrGenerateFiberID(current); | ||
|
||
// Before the traversals, remember to start tracking | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to catch the additional error case (fixed by this PR) in a test.