Skip to content

[DevTools] Release host instances deleted from a still-mounted component - #37847

Open
LubabaKhalid wants to merge 1 commit into
react:mainfrom
LubabaKhalid:devtools-release-deleted-host-instances
Open

LubabaKhalid wants to merge 1 commit into
react:mainfrom
LubabaKhalid:devtools-release-deleted-host-instances

Conversation

@LubabaKhalid

Copy link
Copy Markdown

Summary

Fixes #37802.

With the React DevTools extension installed, DOM nodes that React removes from a component that stays mounted are kept alive by DevTools until the page reloads, even when DevTools is never opened. Examples are a component that swaps a keyed <span>, or one that conditionally shows one.

publicInstanceToDevToolsInstanceMap gets an entry for every host fiber on mount. Host fibers are filtered by default, so they have no DevTools instance of their own and are never in remainingReconcilingChildren. Their entries are only released from untrackFiber, which runs when the nearest tracked instance unmounts and only walks that fiber's current children. A host fiber deleted while its owner stays mounted is never visited, so its entry keeps the detached node and its subtree.

This change releases the filtered fibers in nextFiber.deletions while reconciling the parent, using the existing untrackFiber, which also covers filtered descendants of the deleted subtree. Tracked instances in a deleted subtree are still unmounted through the remaining set as before. The release is skipped when prevFiber === nextFiber, so a stale deletions array from an earlier commit is never read.

How did you test this change?

I added a test to store-test.js. It replaces a keyed <div><span /></div> in a mounted component, then removes it, and checks that agent.getIDForHostInstance no longer resolves the removed nodes. Without the fix it fails: the removed <div> still resolves to the mounted component.

  • yarn build-for-devtools && yarn test --build --project devtools --release-channel=experimental: everything passes except the two ignoreList-test.js cases, which shell out to yarn build:chrome and fail in my local setup for unrelated reasons.
  • yarn flow dom-node passes, and ESLint and Prettier pass on the changed files.

Host fibers are filtered by default, so they don't have a DevTools instance
in the remaining set. When React deleted one while its nearest tracked
instance stayed mounted, its entry in publicInstanceToDevToolsInstanceMap
was never released, keeping the detached DOM node (and its subtree) alive
until the page was reloaded. This happens with the extension installed even
when DevTools is closed.

Release filtered fibers listed in the parent's deletions while reconciling
it, the same way untrackFiber already does for filtered children of an
unmounted instance.
@meta-cla

meta-cla Bot commented Oct 14, 2026

Copy link
Copy Markdown

Hi @LubabaKhalid!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@LubabaKhalid

Copy link
Copy Markdown
Author

@hoxyq @eps1lon could you take a look when you have a moment? This fixes DevTools holding on to removed DOM nodes while their component stays mounted, which grows memory on long-lived pages even with DevTools closed.

@github-actions

github-actions Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

No size report: this pull request only touches packages/react-devtools, which does not affect production bundle size.

Generated by sizebot against 3d47157

@LubabaKhalid

Copy link
Copy Markdown
Author

Hi @LubabaKhalid!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

Its done. Thanks

@meta-cla

meta-cla Bot commented Oct 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed label Oct 14, 2026
@meta-cla

meta-cla Bot commented Oct 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@Nimky

Nimky commented Nov 24, 2026

Copy link
Copy Markdown

Hello @eps1lon, @huntie, @mondaychen, @jstejada,
I'd like to bump this PR — I'm not the author, but an affected user — to ask for a review and a merge decision. It fixes #37802.

Status:
CLA is signed; all CI checks are green (245 checks across 6 suites, latest run 2026-11-02); the branch is mergeable; no reviewer has been requested yet. The diff is +61/−0 across two files: the fix itself plus a regression test that fails on main. The PR has been open since Oct 14.

Impact:
This is not a niche edge case. A large number of React users have the DevTools extension installed, and the leak reproduces even when the DevTools panel is never opened. In long-lived SPAs where users work for hours without reloading, memory grows without bound. In heap snapshots from production sessions, a single object graph occupying ~75% of a ~1 GB heap is retained solely through window.REACT_DEVTOOLS_GLOBAL_HOOK:
scope → context in get() → REACT_DEVTOOLS_GLOBAL_HOOK → global_object → NativeContext → GC roots
While the owning component stays mounted, these detached nodes are never collected — in practice, until the page is reloaded — so the longer a session lasts, the worse it gets.

Verification:
I built the extension locally from this branch and tested it in Chrome on the same workload. The leak is gone: the object graph previously retained through the hook is now collected. That indicates the fix works in practice, not only in the added test.

Two asks:
Review and merge decision. I'm happy to provide additional heap measurements or put together a minimal reproduction if that helps speed up the review.
Store release. Once merged, please include it in the next DevTools extension release (Chrome / Edge / Firefox). The extension only reaches users when a new build is published to the stores, so a merge alone won't fix this in production until the next release cycle.
Thank you for maintaining DevTools!
img

@huntie
huntie requested a review from hoxyq November 24, 2026 12:48

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[DevTools Bug]: DOM nodes removed from a still-mounted component stay in publicInstanceToDevToolsInstanceMap until reload (8.0.0, DevTools closed)

2 participants