Repository navigation
Description
Summary
React DevTools can retain unmounted React Native Fabric host instances indefinitely in publicInstanceToDevToolsInstanceMap. This is reproducible in an iOS Debug app using Hermes and Fabric, including when no DevTools frontend is attached.
Root cause
The backend tracks a host instance by getPublicInstance(hostInstance) during both acquire and release:
DevToolsNativeHost.js,getPublicInstancefiber/renderer.js,aquireHostInstanceandreleaseHostInstance
Fabric lazily initializes canonical.publicInstance in React Native's ReactFabric-dev.js:getPublicInstance. Before that happens, the backend's helper falls back to using the {node, canonical} host wrapper as the key. After lazy initialization, a subsequent release uses the public instance instead, so Map.get() misses and the old key continues to retain the DevTools instance and Fiber subtree. Persistent mode also clones wrappers while sharing canonical, so wrapper identity is not a stable substitute.
Minimal repro
- Run a React Native Fabric Debug app with React DevTools enabled.
- Mount a
<View>without first reading its public instance. DevTools acquires the host instance whilecanonical.publicInstanceisnull. - After mount, attach a ref and call
ref.current.measure(...)(or otherwise cause Fabric to lazily create its public instance). - Unmount the view.
- Observe that
publicInstanceToDevToolsInstanceMapstill contains the entry keyed by the former{node, canonical}wrapper, retaining the unmounted DevTools instance/Fiber.
Evidence
In an iOS Debug Hermes session on React Native Fabric, one lesson Continue→Next transition yielded 141 gets, 133 deletes, and 8 missed releases (5 wrapper-key misses and 3 public-instance-key misses). Across repeated cycles, we found 1,065 keys whose Fabric canonical was absent from every mounted Fiber root. Removing only those stale entries shrank the map by 1,065 and reduced heap-graph self bytes by about 22 MB. These heap measurements were after forced GC; Runtime console entries were cleared before snapshots and the pending initial-operation queue was flushed once when no frontend was attached.
Suggested fix
Use the stable Fabric canonical object as the key for host-instance acquire and release. Normalize a public instance used by getElementIDForHostInstance/getNearestMountedDOMNode back to the same key via publicInstance.__internalInstanceHandle.stateNode.canonical. Keep current behavior for Paper and DOM. A renderer-injected identity callback would avoid hard-coding Fabric internals if the reconciler interface should own this distinction.
The exact issue has been checked in react/react and react/react-native; we commented on the closest related existing report, PR #30831, rather than duplicate its host-instance-map history. This report covers the distinct lazy-publicInstance key mismatch.
Activity