Skip to content

[DevTools] Fix Fabric host-instance map leak when public instances are created lazily - #37758

Open
boriskozak wants to merge 2 commits into
react:mainfrom
boriskozak:react-37752-fix
Open

boriskozak wants to merge 2 commits into
react:mainfrom
boriskozak:react-37752-fix

Conversation

@boriskozak

Copy link
Copy Markdown

Summary

In React Native's Fabric renderer, a host instance is a {node, canonical} wrapper whose canonical.publicInstance is created lazily. The DevTools backend keyed publicInstanceToDevToolsInstanceMap by getPublicInstance(hostInstance), which returns the wrapper before lazy creation and the public instance after. So when an acquire ran before the public instance existed and the release ran after, the two used different keys, the release missed, and unmounted instances (with their Fibers) were retained indefinitely.

This change keys the map by the stable canonical object instead, through a new getHostInstanceKey() helper in DevToolsNativeHost.js. Public instances passed back in via getElementIDForHostInstance, getSuspenseNodeIDForHostInstance, and getNearestMountedDOMNode are normalized through __internalInstanceHandle.stateNode.canonical to the same key. Paper (_nativeTag) and DOM (identity) keying are unchanged.

Fixes #37752.

How did you test this change?

  • Reproduced the reported acquire/release key mismatch against current main with the real getPublicInstance code, simulating RN's lazy canonical.publicInstance creation: the release lookup missed before the fix and hits after it.
  • Added packages/react-devtools-shared/src/__tests__/hostInstanceMapKey-test.js covering the mismatch repro, stable canonical keying before/after lazy public instance creation, public-instance lookup normalization, and unchanged Paper/DOM key behavior.

Note: I could not run yarn test locally because dependency installation fails on this machine's network, so I verified against the real module code directly; the new test is included for CI.

@meta-cla meta-cla Bot added the CLA Signed label Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

The build for this commit did not complete, so there is no size report. See the workflow run for details.

Generated by sizebot against 2a50fc2

@Elehiggle

Copy link
Copy Markdown

At head 0c1dbd438112f5f1b5660157d67b08a791b88e0c, the first added test still inserts and looks up through getPublicInstance, which this PR intentionally leaves unchanged. After canonical.publicInstance is assigned, that lookup returns undefined rather than devtools-instance. The new getHostInstanceKey helper returns the same canonical key before and after that assignment. Could the test use the new helper for acquire/release, or first assert the old-helper miss and then test the new helper? I checked the two function bodies from that exact head; I did not run the full PR test suite.

…exercise getHostInstanceKey

The first test inserted and looked up through getPublicInstance, which
this PR leaves unchanged, so after canonical.publicInstance is assigned
the lookup returns undefined rather than the stored instance. Assert
that miss explicitly, then key acquire/release by getHostInstanceKey
and assert it returns the same canonical key before and after the
lazy public instance assignment, so release finds and removes the
acquire entry.
@boriskozak

Copy link
Copy Markdown
Author

thanks, good catch... reworked the test to do both halves of that. it keys acquire/release by getHostInstanceKey now, asserts the getPublicInstance lookup comes back undefined after canonical.publicInstance gets assigned, and asserts the helper returns the same canonical key before and after, so release finds the entry and removes it. targeted test passes (3/3), flow is clean too

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] Fabric host-instance map misses releases when public instances are created lazily

2 participants