Fix memory leak in dev mode caused by fast-set-immediate (#89779)
## Summary Fixes dev-time memory retention in `fast-set-immediate` caused by module re-evaluation chaining patched globals. ## Root Cause In dev/HMR, `fast-set-immediate.external` can be re-evaluated multiple times. On each evaluation, it captured `globalThis.setImmediate`, `globalThis.clearImmediate`, and `process.nextTick` as “originals”. If those were already patched, new patched closures referenced older patched closures, retaining old module graphs. I have 2GB local heapdump from a few minutes before the local dev server crashed OOM that looks like this because of it: <img width="632" height="253" alt="Screenshot 2026-02-10 at 18 49 24" src="https://github.com/user-attachments/assets/8162b042-ae5a-4289-9166-b11d6374a57a" /> ## What Changed - Added a process-wide sentinel of base/original timer functions on `globalThis`: - `Symbol.for('next.fast-set-immediate.originals')` - `originalSetImmediate`, `originalClearImmediate`, and `originalNextTick` now always resolve from that base sentinel, not from currently patched globals. - Kept the existing patch behavior/API surface: - `install()` still runs at module load - `DANGEROUSLY_runPendingImmediatesAfterCurrentTask`, `expectNoPendingImmediates`, and `unpatchedSetImmediate` exports remain available ## Tests `module re-evaluation` test now verifies `unpatchedSetImmediate` remains bound to the same base/original function across repeated `jest.resetModules()` reloads, and is not rebound to the previously patched global.
Showing
Please
register
or
sign in
to comment