Skip to content

[DevTools] Fabric host-instance map misses releases when public instances are created lazily #37752

Open

Description

@ajoslin

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:

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

  1. Run a React Native Fabric Debug app with React DevTools enabled.
  2. Mount a <View> without first reading its public instance. DevTools acquires the host instance while canonical.publicInstance is null.
  3. After mount, attach a ref and call ref.current.measure(...) (or otherwise cause Fabric to lazily create its public instance).
  4. Unmount the view.
  5. Observe that publicInstanceToDevToolsInstanceMap still 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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions