fix(runtime): release each test file's Metro graph to prevent OOM#145
Merged
V3RON merged 3 commits intoJul 4, 2026
Merged
Conversation
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.
|
@mfazekas is attempting to deploy a commit to the Callstack Team on Vercel. A member of the Team first needs to authorize it. |
V3RON
approved these changes
Jul 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (inIncrementalBundler'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 untilJavaScript heap out of memory. It's per-file, not per-render: even emptyrender(<View/>)files leak, while manyits in one file don't.Fix: after a test file finishes, ask Metro to drop that file's graph by sending an HTTP
DELETEto the same bundle URL the file was fetched from. That is Metro's standard graph-release path (Serverroutesreq.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:
initializeGraphon every.bundlerequest, independent ofwatch/HMR.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
packages/runtime/src/bundler/bundle.test.ts: verifiesreleaseModuleDELETEs the exact same URLfetchModuleGETs (graph-id match) and never throws on failure.packages/runtimesuite: no new failures vs. themainbaseline.render(<View/>)files under--max-old-space-size) OOMs onmainand 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.