Skip to content

fix(runtime): release each test file's Metro graph to prevent OOM#145

Merged
V3RON merged 3 commits into
callstackincubator:mainfrom
mfazekas:fix/metro-per-file-graph-leak
Jul 4, 2026
Merged

fix(runtime): release each test file's Metro graph to prevent OOM#145
V3RON merged 3 commits into
callstackincubator:mainfrom
mfazekas:fix/metro-per-file-graph-leak

Conversation

@mfazekas

@mfazekas mfazekas commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes an unbounded memory leak that OOMs the Harness node runner on suites with many test files. Each test file is fetched from Metro as its own bundle entry point (packages/runtime/src/bundler/bundle.ts), and Metro keeps a full dependency graph in memory per distinct entry (in IncrementalBundler's revision cache, for delta updates), freeing it only when the server shuts down. So a run retained one graph — ~tens of MB — per test file, growing ~linearly with file count until JavaScript heap out of memory. It's per-file, not per-render: even empty render(<View/>) files leak, while many its in one file don't.

Fix: after a test file finishes, ask Metro to drop that file's graph by sending an HTTP DELETE to the same bundle URL the file was fetched from. That is Metro's standard graph-release path (Server routes req.method === "DELETE"IncrementalBundler.endGraph, which releases the DeltaBundler graph and both revision-cache entries) — the same mechanism React Native's own delta client uses on reload. Reusing the exact fetch URL guarantees the graph id matches. Verified identical in Metro 0.82.5 and 0.83.2, so it's version-stable and needs no Metro internals. Release is best-effort (a failed DELETE only costs memory, never correctness).

Notes:

  • Disabling HMR would not fix this — the graph is cached in initializeGraph on every .bundle request, independent of watch/HMR.
  • Platform-agnostic (same per-file fetch path on iOS/Android/web).

Related Issue

Fixes #144. Repro branch: https://github.com/mfazekas/react-native-harness/tree/repro/per-file-metro-graph-leak

Context

The retained graphs are only kept for delta/HMR updates, which the Harness per-file flow never uses (each file is a fresh full fetch), so dropping a file's graph once it has run is safe. The main entry-point bundle's graph is untouched (only the per-test-file module entry is released).

Testing

  • Added packages/runtime/src/bundler/bundle.test.ts: verifies releaseModule DELETEs the exact same URL fetchModule GETs (graph-id match) and never throws on failure.
  • Full packages/runtime suite: no new failures vs. the main baseline.
  • End-to-end: the repro branch (40 empty render(<View/>) files under --max-old-space-size) OOMs on main and is expected to keep RSS flat with this change. E2E verification on the iOS runner is best done in CI / on a real suite (it can't be unit-tested); happy to wire the empty-files repro into the playground e2e job as a permanent regression guard if desired.

Each test file is fetched from Metro as its own bundle entry point. Metro
keeps a full dependency graph in memory per distinct entry (for delta/HMR
updates) and only frees it when the server shuts down, so a run retained one
graph per test file — ~tens of MB each — growing unbounded across the suite
until the runner hit 'JavaScript heap out of memory'. This is per-file, not
per-render: even empty render(<View/>) files leak, while many tests in one
file do not.

After a file finishes, ask Metro to drop that file's graph by sending an HTTP
DELETE to the same bundle URL (Metro's standard graph-release path, stable
across 0.82/0.83). Memory now stays flat regardless of file count.
@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown

@mfazekas is attempting to deploy a commit to the Callstack Team on Vercel.

A member of the Team first needs to authorize it.

@V3RON V3RON marked this pull request as ready for review July 3, 2026 10:40
@V3RON V3RON merged commit e04df16 into callstackincubator:main Jul 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Per-test-file memory leak: Metro dependency graph retained per file → runner OOM

2 participants